Ticket #12820: 0001-Ticket-12820-fix-iteration-over-MergeDict.patch

File 0001-Ticket-12820-fix-iteration-over-MergeDict.patch, 2.1 KB (added by gisle, 15 years ago)

Updated patch that also fixes MergeDict.items()

  • django/utils/datastructures.py

    From 2d06c971fa121ee1bcf433e82eefae92084d46c1 Mon Sep 17 00:00:00 2001
    From: Gisle Aas <gisle@aas.no>
    Date: Mon, 22 Feb 2010 11:25:57 -0500
    Subject: [PATCH] Ticket #12820: fix iteration over MergeDict
    
    ---
     django/utils/datastructures.py                |   16 +++++++++++++++-
     tests/regressiontests/datastructures/tests.py |   11 +++++++++++
     2 files changed, 26 insertions(+), 1 deletions(-)
    
    diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
    index d6fc9dc..c5a9fdc 100644
    a b class MergeDict(object):  
    3939
    4040    def items(self):
    4141        item_list = []
     42        seen = set()
    4243        for dict_ in self.dicts:
    43             item_list.extend(dict_.items())
     44            for item in dict_.items():
     45                if item[0] in seen:
     46                    continue
     47                item_list.append(item)
     48                seen.add(item[0])
    4449        return item_list
    4550
     51    def keys(self):
     52        return [item[0] for item in self.items()]
     53
     54    def values(self):
     55        return [item[1] for item in self.items()]
     56
     57    def __iter__(self):
     58        return iter(self.keys())
     59
    4660    def has_key(self, key):
    4761        for dict_ in self.dicts:
    4862            if key in dict_:
  • tests/regressiontests/datastructures/tests.py

    diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py
    index b7885e6..a858e24 100644
    a b MergeDict can merge MultiValueDicts  
    3434>>> mm.getlist('undefined')
    3535[]
    3636
     37>>> sorted(mm.keys())
     38['key1', 'key2', 'key4']
     39>>> len(mm.values())
     403
     41>>> "value1" in mm.values()
     42True
     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
    3748### MultiValueDict ##########################################################
    3849
    3950>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
Back to Top