Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[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
@jkocherhans jkocherhans authored
View
1  AUTHORS
@@ -26,6 +26,7 @@ And here is an inevitably incomplete list of MUCH-APPRECIATED CONTRIBUTORS --
people who have submitted patches, reported bugs, added translations, helped
answer newbie questions, and generally made Django that much better:
+ Gisle Aas <gisle@aas.no>
ajs <adi@sieker.info>
alang@bright-green.com
Marty Alchin <gulopine@gamemusic.org>
View
30 django/utils/datastructures.py
@@ -37,11 +37,32 @@ def getlist(self, key):
return dict_.getlist(key)
return []
- def items(self):
- item_list = []
+ def iteritems(self):
+ seen = set()
for dict_ in self.dicts:
- item_list.extend(dict_.items())
- return item_list
+ for item in dict_.iteritems():
+ k, v = item
+ if k in seen:
+ continue
+ seen.add(k)
+ yield item
+
+ def iterkeys(self):
+ for k, v in self.iteritems():
+ yield k
+
+ def itervalues(self):
+ for k, v in self.iteritems():
+ yield v
+
+ def items(self):
+ return list(self.iteritems())
+
+ def keys(self):
+ return list(self.iterkeys())
+
+ def values(self):
+ return list(self.itervalues())
def has_key(self, key):
for dict_ in self.dicts:
@@ -50,6 +71,7 @@ def has_key(self, key):
return False
__contains__ = has_key
+ __iter__ = iterkeys
def copy(self):
"""Returns a copy of this object."""
View
11 tests/regressiontests/datastructures/tests.py
@@ -34,6 +34,17 @@
>>> mm.getlist('undefined')
[]
+>>> sorted(mm.keys())
+['key1', 'key2', 'key4']
+>>> len(mm.values())
+3
+>>> "value1" in mm.values()
+True
+>>> sorted(mm.items(), key=lambda k: k[0])
+[('key1', 'value1'), ('key2', 'value3'), ('key4', 'value6')]
+>>> [(k,mm.getlist(k)) for k in sorted(mm)]
+[('key1', ['value1']), ('key2', ['value2', 'value3']), ('key4', ['value5', 'value6'])]
+
### MultiValueDict ##########################################################
>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
Please sign in to comment.
Something went wrong with that request. Please try again.