From b3d20ade47aadce5bd02ff33ea7dc0e19cf004cb Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Mon, 22 Feb 2010 23:38:18 +0000 Subject: [PATCH] Fixed #12820. Implemented other dict methods for MergeDict. Thanks, Gisle Aas. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12498 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/utils/datastructures.py | 30 ++++++++++++++++--- tests/regressiontests/datastructures/tests.py | 11 +++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index 119bb8b20a..92b1559042 100644 --- a/AUTHORS +++ b/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 ajs alang@bright-green.com Alcides Fonseca diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index d6fc9dc56f..3cbbe27b91 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -37,11 +37,32 @@ class MergeDict(object): 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 @@ class MergeDict(object): return False __contains__ = has_key + __iter__ = iterkeys def copy(self): """Returns a copy of this object.""" diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py index b7885e65a4..a858e2469f 100644 --- a/tests/regressiontests/datastructures/tests.py +++ b/tests/regressiontests/datastructures/tests.py @@ -34,6 +34,17 @@ MergeDict can merge MultiValueDicts >>> 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']})