Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #12820. Implemented other dict methods for MergeDict. B…

…ackport of [12498] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12501 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 16efc1a92e30e5f8a9ecc89edf0002db2425b3b4 1 parent bd79677
Joseph Kocherhans authored February 23, 2010
1  AUTHORS
@@ -26,6 +26,7 @@ And here is an inevitably incomplete list of MUCH-APPRECIATED CONTRIBUTORS --
26 26
 people who have submitted patches, reported bugs, added translations, helped
27 27
 answer newbie questions, and generally made Django that much better:
28 28
 
  29
+    Gisle Aas <gisle@aas.no>
29 30
     ajs <adi@sieker.info>
30 31
     alang@bright-green.com
31 32
     Marty Alchin <gulopine@gamemusic.org>
30  django/utils/datastructures.py
@@ -37,11 +37,32 @@ def getlist(self, key):
37 37
                 return dict_.getlist(key)
38 38
         return []
39 39
 
40  
-    def items(self):
41  
-        item_list = []
  40
+    def iteritems(self):
  41
+        seen = set()
42 42
         for dict_ in self.dicts:
43  
-            item_list.extend(dict_.items())
44  
-        return item_list
  43
+            for item in dict_.iteritems():
  44
+                k, v = item
  45
+                if k in seen:
  46
+                    continue
  47
+                seen.add(k)
  48
+                yield item
  49
+
  50
+    def iterkeys(self):
  51
+        for k, v in self.iteritems():
  52
+            yield k
  53
+
  54
+    def itervalues(self):
  55
+        for k, v in self.iteritems():
  56
+            yield v
  57
+
  58
+    def items(self):
  59
+        return list(self.iteritems())
  60
+
  61
+    def keys(self):
  62
+        return list(self.iterkeys())
  63
+
  64
+    def values(self):
  65
+        return list(self.itervalues())
45 66
 
46 67
     def has_key(self, key):
47 68
         for dict_ in self.dicts:
@@ -50,6 +71,7 @@ def has_key(self, key):
50 71
         return False
51 72
 
52 73
     __contains__ = has_key
  74
+    __iter__ = iterkeys
53 75
 
54 76
     def copy(self):
55 77
         """Returns a copy of this object."""
11  tests/regressiontests/datastructures/tests.py
@@ -34,6 +34,17 @@
34 34
 >>> mm.getlist('undefined')
35 35
 []
36 36
 
  37
+>>> sorted(mm.keys())
  38
+['key1', 'key2', 'key4']
  39
+>>> len(mm.values())
  40
+3
  41
+>>> "value1" in mm.values()
  42
+True
  43
+>>> sorted(mm.items(), key=lambda k: k[0])
  44
+[('key1', 'value1'), ('key2', 'value3'), ('key4', 'value6')]
  45
+>>> [(k,mm.getlist(k)) for k in sorted(mm)]
  46
+[('key1', ['value1']), ('key2', ['value2', 'value3']), ('key4', ['value5', 'value6'])]
  47
+
37 48
 ### MultiValueDict ##########################################################
38 49
 
39 50
 >>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})

0 notes on commit 16efc1a

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