Author: mtredinnick
Date: 2007-10-21 19:52:54 -0500 (Sun, 21 Oct 2007)
New Revision: 6593
Modified:
django/trunk/django/utils/datastructures.py
django/trunk/tests/regressiontests/datastructures/tests.py
Log:
Fixed #5183 -- Added __deepcopy__, pop() and popitem() to SortedDict. Based on
a patch from David Blewett.
Modified: django/trunk/django/utils/datastructures.py
===================================================================
--- django/trunk/django/utils/datastructures.py 2007-10-21 23:52:08 UTC (rev
6592)
+++ django/trunk/django/utils/datastructures.py 2007-10-22 00:52:54 UTC (rev
6593)
@@ -62,6 +62,13 @@
else:
self.keyOrder = [key for key, value in data]
+ def __deepcopy__(self,memo):
+ from copy import deepcopy
+ obj = self.__class__()
+ for k, v in self.items():
+ obj[k] = deepcopy(v, memo)
+ return obj
+
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
if key not in self.keyOrder:
@@ -75,6 +82,20 @@
for k in self.keyOrder:
yield k
+ def pop(self, k, *args):
+ result = dict.pop(self, k, *args)
+ try:
+ self.keyOrder.remove(k)
+ except ValueError:
+ # Key wasn't in the dictionary in the first place. No problem.
+ pass
+ return result
+
+ def popitem(self):
+ result = dict.popitem(self)
+ self.keyOrder.remove(result[0])
+ return result
+
def items(self):
return zip(self.keyOrder, self.values())
Modified: django/trunk/tests/regressiontests/datastructures/tests.py
===================================================================
--- django/trunk/tests/regressiontests/datastructures/tests.py 2007-10-21
23:52:08 UTC (rev 6592)
+++ django/trunk/tests/regressiontests/datastructures/tests.py 2007-10-22
00:52:54 UTC (rev 6593)
@@ -54,7 +54,18 @@
True
>>> print repr(d)
{'one': 'not one', 'two': 'two', 'three': 'three'}
+>>> d.pop('one', 'missing')
+'not one'
+>>> d.pop('one', 'missing')
+'missing'
+We don't know which item will be popped in popitem(), so we'll just check that
+the number of keys has decreased.
+>>> l = len(d)
+>>> _ = d.popitem()
+>>> l - len(d)
+1
+
Init from sequence of tuples
>>> d = SortedDict((
... (1, "one"),
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---