Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5183 -- Added __deepcopy__, pop() and popitem() to SortedDict.…

… Based on

a patch from David Blewett.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6593 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit dbd1cb9083db8a0206a4b951813f4c8291afa824 1 parent 375a6d7
Malcolm Tredinnick authored October 22, 2007
21  django/utils/datastructures.py
@@ -62,6 +62,13 @@ def __init__(self, data=None):
62 62
         else:
63 63
             self.keyOrder = [key for key, value in data]
64 64
 
  65
+    def __deepcopy__(self,memo):
  66
+        from copy import deepcopy
  67
+        obj = self.__class__()
  68
+        for k, v in self.items():
  69
+            obj[k] = deepcopy(v, memo)
  70
+        return obj
  71
+
65 72
     def __setitem__(self, key, value):
66 73
         dict.__setitem__(self, key, value)
67 74
         if key not in self.keyOrder:
@@ -75,6 +82,20 @@ def __iter__(self):
75 82
         for k in self.keyOrder:
76 83
             yield k
77 84
 
  85
+    def pop(self, k, *args):
  86
+        result = dict.pop(self, k, *args)
  87
+        try:
  88
+            self.keyOrder.remove(k)
  89
+        except ValueError:
  90
+            # Key wasn't in the dictionary in the first place. No problem.
  91
+            pass
  92
+        return result
  93
+
  94
+    def popitem(self):
  95
+        result = dict.popitem(self)
  96
+        self.keyOrder.remove(result[0])
  97
+        return result
  98
+
78 99
     def items(self):
79 100
         return zip(self.keyOrder, self.values())
80 101
 
11  tests/regressiontests/datastructures/tests.py
@@ -54,6 +54,17 @@
54 54
 True
55 55
 >>> print repr(d)
56 56
 {'one': 'not one', 'two': 'two', 'three': 'three'}
  57
+>>> d.pop('one', 'missing')
  58
+'not one'
  59
+>>> d.pop('one', 'missing')
  60
+'missing'
  61
+
  62
+We don't know which item will be popped in popitem(), so we'll just check that
  63
+the number of keys has decreased.
  64
+>>> l = len(d)
  65
+>>> _ = d.popitem()
  66
+>>> l - len(d)
  67
+1
57 68
 
58 69
 Init from sequence of tuples
59 70
 >>> d = SortedDict((

0 notes on commit dbd1cb9

Please sign in to comment.
Something went wrong with that request. Please try again.