[Checkins] SVN: z3c.dobbin/trunk/ Implemented remaining list
methods.
Malthe Borch
mborch at gmail.com
Sat Jun 21 20:43:26 EDT 2008
Log message for revision 87644:
Implemented remaining list methods.
Changed:
U z3c.dobbin/trunk/CHANGES.txt
U z3c.dobbin/trunk/src/z3c/dobbin/README.txt
U z3c.dobbin/trunk/src/z3c/dobbin/collections.py
-=-
Modified: z3c.dobbin/trunk/CHANGES.txt
===================================================================
--- z3c.dobbin/trunk/CHANGES.txt 2008-06-22 00:41:42 UTC (rev 87643)
+++ z3c.dobbin/trunk/CHANGES.txt 2008-06-22 00:43:26 UTC (rev 87644)
@@ -4,6 +4,9 @@
0.3 dev
-------
+- Implemented rest of list methods.
+ [malthe]
+
- Refactoring of table bootstrapping; internal tables now using a
naming convention less likely to clash with existing tables.
[malthe]
Modified: z3c.dobbin/trunk/src/z3c/dobbin/README.txt
===================================================================
--- z3c.dobbin/trunk/src/z3c/dobbin/README.txt 2008-06-22 00:41:42 UTC (rev 87643)
+++ z3c.dobbin/trunk/src/z3c/dobbin/README.txt 2008-06-22 00:43:26 UTC (rev 87644)
@@ -327,49 +327,84 @@
>>> collection = lookup(collection.uuid)
- >>> vinyl = create(IVinyl)
- >>> vinyl.artist = u"Kool & the Gang"
- >>> vinyl.title = u"Music Is the Message"
- >>> vinyl.rpm = 33
-
- >>> collection.records.append(vinyl)
+ >>> kool = create(IVinyl)
+ >>> kool.artist = u"Kool & the Gang"
+ >>> kool.title = u"Music Is the Message"
+ >>> kool.rpm = 33
+
+ >>> collection.records.append(kool)
>>> [record.artist for record in collection.records]
[u'Diana Ross and The Supremes', u'Kool & the Gang']
>>> session.flush()
>>> session.update(collection)
-
+
We can remove items.
- >>> collection.records.remove(vinyl)
+ >>> collection.records.remove(kool)
>>> len(collection.records) == 1
True
And extend.
- >>> collection.records.extend((vinyl,))
+ >>> collection.records.extend((kool,))
>>> len(collection.records) == 2
True
Items can appear twice in the list.
- >>> collection.records.append(vinyl)
+ >>> collection.records.append(kool)
>>> len(collection.records) == 3
True
We can add concrete instances to collections.
- >>> vinyl = Vinyl()
- >>> collection.records.append(vinyl)
+ >>> marvin = Vinyl()
+ >>> marvin.artist = u"Marvin Gaye"
+ >>> marvin.title = u"Let's get it on"
+ >>> marvin.rpm = 33
+
+ >>> collection.records.append(marvin)
>>> len(collection.records) == 4
True
And remove them, too.
- >>> collection.records.remove(vinyl)
+ >>> collection.records.remove(marvin)
>>> len(collection.records) == 3
True
+The standard list methods are available.
+
+ >>> collection.records = [marvin, vinyl]
+ >>> collection.records.sort(key=lambda record: record.artist)
+ >>> collection.records
+ [<Vinyl Diana Ross and The Supremes: Taking Care of Business (@ 45 RPM)>,
+ <Vinyl Marvin Gaye: Let's get it on (@ 33 RPM)>]
+
+ >>> collection.records.reverse()
+ >>> collection.records
+ [<Vinyl Marvin Gaye: Let's get it on (@ 33 RPM)>,
+ <Vinyl Diana Ross and The Supremes: Taking Care of Business (@ 45 RPM)>]
+
+ >>> collection.records.index(vinyl)
+ 1
+
+ >>> collection.records.pop()
+ <Vinyl Diana Ross and The Supremes: Taking Care of Business (@ 45 RPM)>
+
+ >>> collection.records.insert(0, vinyl)
+ >>> collection.records
+ [<Vinyl Diana Ross and The Supremes: Taking Care of Business (@ 45 RPM)>,
+ <Vinyl Marvin Gaye: Let's get it on (@ 33 RPM)>]
+
+ >>> collection.records.count(vinyl)
+ 1
+
+ >>> collection.records[1] = vinyl
+ >>> collection.records.count(vinyl)
+ 2
+
For good measure, let's create a new instance without adding any
elements to its list.
Modified: z3c.dobbin/trunk/src/z3c/dobbin/collections.py
===================================================================
--- z3c.dobbin/trunk/src/z3c/dobbin/collections.py 2008-06-22 00:41:42 UTC (rev 87643)
+++ z3c.dobbin/trunk/src/z3c/dobbin/collections.py 2008-06-22 00:43:26 UTC (rev 87644)
@@ -55,7 +55,7 @@
return obj
def __setitem__(self, index, value):
- return NotImplementedError("Object does not support item assignment.")
+ return TypeError("Object does not support item assignment.")
def __len__(self):
return len(self.data)
@@ -65,7 +65,7 @@
class OrderedList(Tuple):
__Security_checker__ = NamesChecker(
- ('append', 'remove'))
+ ('append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'))
@orm.collections.collection.appender
def _appender(self, item):
@@ -113,14 +113,78 @@
map(self.append, items)
def count(self, value):
- return NotImplementedError("Count-method not implemented.")
+ return list(self).count(value)
+ def index(self, value, **kwargs):
+ for index in range(len(self)):
+ if self[index] == value:
+ return index
+
+ raise ValueError("%s not found in list." % value)
+
+ @orm.collections.collection.internally_instrumented
+ def insert(self, index, item):
+ stack = self.data[index:]
+ del self.data[index:]
+ self.append(item)
+ for relation in stack:
+ relation.order += 1
+ self.data.append(relation)
+
+ @orm.collections.collection.internally_instrumented
+ def pop(self, index=-1, _sa_initiator=None):
+ relation = self.data[index]
+ obj = relation.target
+
+ self.adapter.fire_remove_event(relation, _sa_initiator)
+ del self.data[index]
+
+ stack = self.data[index:]
+ for relation in stack:
+ relation.order -= 1
+
+ return obj
+
+ def reverse(self):
+ self.data.reverse()
+ for index in range(len(self.data)):
+ self.data[index].order = index
+
+ def sort(self, **kwargs):
+ data = list(self)
+ data_relation_mapping = zip(data, self.data)
+
+ mapping = {}
+ for item, relation in data_relation_mapping:
+ relations = mapping.setdefault(item, [])
+ relations.append(relation)
+
+ data.sort(**kwargs)
+ del self.data[:]
+
+ for item in data:
+ relation = mapping[item].pop()
+ relation.order = len(self.data)
+ self.data.append(relation)
+
def __repr__(self):
return repr(list(self))
- def __setitem__(self, index, value):
- return NotImplementedError("Setting items at an index is not implemented.")
+ @orm.collections.collection.internally_instrumented
+ def __setitem__(self, index, value, _sa_initiator=None):
+ # remove previous
+ relation = self.data[index]
+ self.adapter.fire_remove_event(relation, _sa_initiator)
+ # add new
+ self.append(value)
+ relation = self.data[-1]
+ del self.data[-1]
+
+ # replace previous
+ relation.order = index
+ self.data[index] = relation
+
class Dict(dict):
__Security_checker__ = NamesChecker(
('clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'))
More information about the Checkins
mailing list