Ticket #11874: sorted-dict.diff

File sorted-dict.diff, 3.0 KB (added by Alex, 6 years ago)
  • django/utils/datastructures.py

    diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
    index 85cdd44..b310184 100644
    a b class SortedDict(dict): 
    7777                               for key, value in self.iteritems()])
    7878
    7979    def __setitem__(self, key, value):
    80         super(SortedDict, self).__setitem__(key, value)
    81         if key not in self.keyOrder:
     80        if key not in self:
    8281            self.keyOrder.append(key)
     82        super(SortedDict, self).__setitem__(key, value)
    8383
    8484    def __delitem__(self, key):
    8585        super(SortedDict, self).__delitem__(key)
    8686        self.keyOrder.remove(key)
    8787
    8888    def __iter__(self):
    89         for k in self.keyOrder:
    90             yield k
     89        return iter(self.keyOrder)
    9190
    9291    def pop(self, k, *args):
    9392        result = super(SortedDict, self).pop(k, *args)
    class SortedDict(dict): 
    108107
    109108    def iteritems(self):
    110109        for key in self.keyOrder:
    111             yield key, super(SortedDict, self).__getitem__(key)
     110            yield key, self[key]
    112111
    113112    def keys(self):
    114113        return self.keyOrder[:]
    class SortedDict(dict): 
    117116        return iter(self.keyOrder)
    118117
    119118    def values(self):
    120         return map(super(SortedDict, self).__getitem__, self.keyOrder)
     119        return map(self.__getitem__, self.keyOrder)
    121120
    122121    def itervalues(self):
    123122        for key in self.keyOrder:
    124             yield super(SortedDict, self).__getitem__(key)
     123            yield self[key]
    125124
    126125    def update(self, dict_):
    127         for k, v in dict_.items():
    128             self.__setitem__(k, v)
     126        for k, v in dict_.iteritems():
     127            self[k] = v
    129128
    130129    def setdefault(self, key, default):
    131         if key not in self.keyOrder:
     130        if key not in self:
    132131            self.keyOrder.append(key)
    133132        return super(SortedDict, self).setdefault(key, default)
    134133
    class MultiValueDict(dict): 
    222221            dict.__setitem__(result, copy.deepcopy(key, memo),
    223222                             copy.deepcopy(value, memo))
    224223        return result
    225    
     224
    226225    def __getstate__(self):
    227226        obj_dict = self.__dict__.copy()
    228227        obj_dict['_data'] = dict([(k, self.getlist(k)) for k in self])
    229228        return obj_dict
    230    
     229
    231230    def __setstate__(self, obj_dict):
    232231        data = obj_dict.pop('_data', {})
    233232        for k, v in data.items():
    234233            self.setlist(k, v)
    235234        self.__dict__.update(obj_dict)
    236        
     235
    237236    def get(self, key, default=None):
    238237        """
    239238        Returns the last data value for the passed key. If key doesn't exist
    class MultiValueDict(dict): 
    301300    def values(self):
    302301        """Returns a list of the last value on every key list."""
    303302        return [self[key] for key in self.keys()]
    304        
     303
    305304    def itervalues(self):
    306305        """Yield the last value on every key list."""
    307306        for key in self.iterkeys():
    308307            yield self[key]
    309    
     308
    310309    def copy(self):
    311310        """Returns a copy of this object."""
    312311        return self.__deepcopy__()
Back to Top