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
This commit is contained in:
Joseph Kocherhans 2010-02-22 23:38:18 +00:00
parent 49d6a82261
commit b3d20ade47
3 changed files with 38 additions and 4 deletions

View File

@ -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 people who have submitted patches, reported bugs, added translations, helped
answer newbie questions, and generally made Django that much better: answer newbie questions, and generally made Django that much better:
Gisle Aas <gisle@aas.no>
ajs <adi@sieker.info> ajs <adi@sieker.info>
alang@bright-green.com alang@bright-green.com
Alcides Fonseca Alcides Fonseca

View File

@ -37,11 +37,32 @@ class MergeDict(object):
return dict_.getlist(key) return dict_.getlist(key)
return [] return []
def items(self): def iteritems(self):
item_list = [] seen = set()
for dict_ in self.dicts: for dict_ in self.dicts:
item_list.extend(dict_.items()) for item in dict_.iteritems():
return item_list 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): def has_key(self, key):
for dict_ in self.dicts: for dict_ in self.dicts:
@ -50,6 +71,7 @@ class MergeDict(object):
return False return False
__contains__ = has_key __contains__ = has_key
__iter__ = iterkeys
def copy(self): def copy(self):
"""Returns a copy of this object.""" """Returns a copy of this object."""

View File

@ -34,6 +34,17 @@ MergeDict can merge MultiValueDicts
>>> mm.getlist('undefined') >>> 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 ########################################################## ### MultiValueDict ##########################################################
>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']}) >>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})