diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-05-04 09:55:38 +0200 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-05-04 09:55:38 +0200 |
commit | 8189c1ee21be899f090eef7211c12e10a11a9f53 (patch) | |
tree | d9c7b79c8a54d75a045694458ea66ac3c4f81e17 | |
parent | 746e780f6ff679616441cf2ee9d8d2cc5e570307 (diff) | |
download | pyexiv2-8189c1ee21be899f090eef7211c12e10a11a9f53.tar.gz |
NotifyingList: implementation of reverse, sort, __iadd__, __imul__.
-rw-r--r-- | src/pyexiv2.py | 18 | ||||
-rw-r--r-- | unittest/notifying_list.py | 58 |
2 files changed, 68 insertions, 8 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index d39edfa..ca0506f 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -281,6 +281,8 @@ class NotifyingList(list): # file:///usr/share/doc/python2.5/html/lib/typesseq-mutable.html # http://docs.python.org/reference/datamodel.html#additional-methods-for-emulation-of-sequence-types + # FIXME: support negatives indexes where relevant + def __init__(self, items=[]): super(NotifyingList, self).__init__(items) self._listeners = set() @@ -332,16 +334,24 @@ class NotifyingList(list): self._notify_listeners('items_deleted', index, index + 1) def reverse(self): - raise NotImplementedError() + super(NotifyingList, self).reverse() + self._notify_listeners('reordered') def sort(self, cmp=None, key=None, reverse=False): - raise NotImplementedError() + super(NotifyingList, self).sort(cmp, key, reverse) + self._notify_listeners('reordered') def __iadd__(self, other): - raise NotImplementedError() + index = len(self) + self = super(NotifyingList, self).__iadd__(other) + self._notify_listeners('items_inserted', index, other) + return self def __imul__(self, coefficient): - raise NotImplementedError() + index = len(self) + self = super(NotifyingList, self).__imul__(coefficient) + self._notify_listeners('items_inserted', index, self[index:]) + return self def __setslice__(self, i, j, sequence): raise NotImplementedError() diff --git a/unittest/notifying_list.py b/unittest/notifying_list.py index 07dec11..32e5d94 100644 --- a/unittest/notifying_list.py +++ b/unittest/notifying_list.py @@ -65,7 +65,8 @@ class TestNotifyingList(unittest.TestCase): def test_listener_interface(self): self.values.register_listener(ListenerInterface()) - self.failUnlessRaises(NotImplementedError, self.values.__setitem__, 3, 13) + self.failUnlessRaises(NotImplementedError, + self.values.__setitem__, 3, 13) self.failUnlessRaises(NotImplementedError, self.values.__delitem__, 5) self.failUnlessRaises(NotImplementedError, self.values.append, 17) self.failUnlessRaises(NotImplementedError, self.values.extend, [11, 22]) @@ -114,13 +115,15 @@ class TestNotifyingList(unittest.TestCase): self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11, 22]) for listener in listeners: self.failUnlessEqual(listener.notifications, 4) - self.failUnlessEqual(listener.last, ('items_inserted', (7, [11, 22]))) + self.failUnlessEqual(listener.last, + ('items_inserted', (7, [11, 22]))) self.failUnlessRaises(TypeError, self.values.extend, 26) self.failUnlessEqual(self.values, [5, 7, 9, 13, 57, 2, 17, 11, 22]) for listener in listeners: self.failUnlessEqual(listener.notifications, 4) - self.failUnlessEqual(listener.last, ('items_inserted', (7, [11, 22]))) + self.failUnlessEqual(listener.last, + ('items_inserted', (7, [11, 22]))) self.values.insert(4, 24) self.failUnlessEqual(self.values, [5, 7, 9, 13, 24, 57, 2, 17, 11, 22]) @@ -158,4 +161,51 @@ class TestNotifyingList(unittest.TestCase): self.failUnlessEqual(listener.notifications, 8) self.failUnlessEqual(listener.last, ('items_deleted', (2, 3))) - # TODO: test all operations (slicing, ...) + self.values.reverse() + self.failUnlessEqual(self.values, [11, 17, 2, 57, 13, 7, 5]) + for listener in listeners: + self.failUnlessEqual(listener.notifications, 9) + self.failUnlessEqual(listener.last, ('reordered', ())) + + self.values.sort() + self.failUnlessEqual(self.values, [2, 5, 7, 11, 13, 17, 57]) + for listener in listeners: + self.failUnlessEqual(listener.notifications, 10) + self.failUnlessEqual(listener.last, ('reordered', ())) + + self.values.sort(cmp=lambda x, y: y - x) + self.failUnlessEqual(self.values, [57, 17, 13, 11, 7, 5, 2]) + for listener in listeners: + self.failUnlessEqual(listener.notifications, 11) + self.failUnlessEqual(listener.last, ('reordered', ())) + + self.values.sort(key=lambda x: x * x) + self.failUnlessEqual(self.values, [2, 5, 7, 11, 13, 17, 57]) + for listener in listeners: + self.failUnlessEqual(listener.notifications, 12) + self.failUnlessEqual(listener.last, ('reordered', ())) + + self.values.sort(reverse=True) + self.failUnlessEqual(self.values, [57, 17, 13, 11, 7, 5, 2]) + for listener in listeners: + self.failUnlessEqual(listener.notifications, 13) + self.failUnlessEqual(listener.last, ('reordered', ())) + + self.values += [44, 31, 19] + self.failUnlessEqual(self.values, [57, 17, 13, 11, 7, 5, 2, 44, 31, 19]) + for listener in listeners: + self.failUnlessEqual(listener.notifications, 14) + self.failUnlessEqual(listener.last, + ('items_inserted', (7, [44, 31, 19]))) + + self.values *= 3 + self.failUnlessEqual(self.values, + [57, 17, 13, 11, 7, 5, 2, 44, 31, 19, + 57, 17, 13, 11, 7, 5, 2, 44, 31, 19, + 57, 17, 13, 11, 7, 5, 2, 44, 31, 19]) + for listener in listeners: + self.failUnlessEqual(listener.notifications, 15) + self.failUnlessEqual(listener.last, + ('items_inserted', + (10, [57, 17, 13, 11, 7, 5, 2, 44, 31, 19, + 57, 17, 13, 11, 7, 5, 2, 44, 31, 19]))) |