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

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

Revised patch based on IRC feedback from Alex_Gaynor (provides iter{iterms,keys,values}) and use tuple unpacking instead of indexing)

  • django/utils/datastructures.py

    From e41ea451c1cbe37585ccafdae6f2275a8d70e421 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                |   30 +++++++++++++++++++++---
     tests/regressiontests/datastructures/tests.py |   11 +++++++++
     2 files changed, 37 insertions(+), 4 deletions(-)
    
    diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
    index d6fc9dc..8c05e7d 100644
    a b class MergeDict(object): 
    3737                return dict_.getlist(key)
    3838        return []
    3939
    40     def items(self):
    41         item_list = []
     40    def iteritems(self):
     41        seen = set()
    4242        for dict_ in self.dicts:
    43             item_list.extend(dict_.items())
    44         return item_list
     43            for item in dict_.items():
     44                k, v = item
     45                if k in seen:
     46                    continue
     47                seen.add(k)
     48                yield item
     49
     50    def iterkeys(self):
     51        for k, v in self.iteritems():
     52            yield k
     53
     54    def itervalues(self):
     55        for k, v in self.iteritems():
     56            yield v
     57
     58    def items(self):
     59        return list(self.iteritems())
     60
     61    def keys(self):
     62        return list(self.iterkeys())
     63
     64    def values(self):
     65        return list(self.itervalues())
    4566
    4667    def has_key(self, key):
    4768        for dict_ in self.dicts:
    class MergeDict(object): 
    5071        return False
    5172
    5273    __contains__ = has_key
     74    __iter__ = iterkeys
    5375
    5476    def copy(self):
    5577        """Returns a copy of this object."""
  • 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