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']})