Ticket #11874: sorted-dict.2.diff

File sorted-dict.2.diff, 3.3 KB (added by Alex Gaynor, 15 years ago)
  • django/utils/datastructures.py

    diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
    index 85cdd44..2b586d7 100644
    a b  
     1from copy import deepcopy
     2
     3
    14class MergeDict(object):
    25    """
    36    A simple class for creating new "virtual" dictionaries that actually look
    class SortedDict(dict):  
    7275                    self.keyOrder.append(key)
    7376
    7477    def __deepcopy__(self, memo):
    75         from copy import deepcopy
    7678        return self.__class__([(key, deepcopy(value, memo))
    7779                               for key, value in self.iteritems()])
    7880
    7981    def __setitem__(self, key, value):
    80         super(SortedDict, self).__setitem__(key, value)
    81         if key not in self.keyOrder:
     82        if key not in self:
    8283            self.keyOrder.append(key)
     84        super(SortedDict, self).__setitem__(key, value)
    8385
    8486    def __delitem__(self, key):
    8587        super(SortedDict, self).__delitem__(key)
    8688        self.keyOrder.remove(key)
    8789
    8890    def __iter__(self):
    89         for k in self.keyOrder:
    90             yield k
     91        return iter(self.keyOrder)
    9192
    9293    def pop(self, k, *args):
    9394        result = super(SortedDict, self).pop(k, *args)
    class SortedDict(dict):  
    108109
    109110    def iteritems(self):
    110111        for key in self.keyOrder:
    111             yield key, super(SortedDict, self).__getitem__(key)
     112            yield key, self[key]
    112113
    113114    def keys(self):
    114115        return self.keyOrder[:]
    class SortedDict(dict):  
    117118        return iter(self.keyOrder)
    118119
    119120    def values(self):
    120         return map(super(SortedDict, self).__getitem__, self.keyOrder)
     121        return map(self.__getitem__, self.keyOrder)
    121122
    122123    def itervalues(self):
    123124        for key in self.keyOrder:
    124             yield super(SortedDict, self).__getitem__(key)
     125            yield self[key]
    125126
    126127    def update(self, dict_):
    127         for k, v in dict_.items():
    128             self.__setitem__(k, v)
     128        for k, v in dict_.iteritems():
     129            self[k] = v
    129130
    130131    def setdefault(self, key, default):
    131         if key not in self.keyOrder:
     132        if key not in self:
    132133            self.keyOrder.append(key)
    133134        return super(SortedDict, self).setdefault(key, default)
    134135
    class MultiValueDict(dict):  
    222223            dict.__setitem__(result, copy.deepcopy(key, memo),
    223224                             copy.deepcopy(value, memo))
    224225        return result
    225    
     226
    226227    def __getstate__(self):
    227228        obj_dict = self.__dict__.copy()
    228229        obj_dict['_data'] = dict([(k, self.getlist(k)) for k in self])
    229230        return obj_dict
    230    
     231
    231232    def __setstate__(self, obj_dict):
    232233        data = obj_dict.pop('_data', {})
    233234        for k, v in data.items():
    234235            self.setlist(k, v)
    235236        self.__dict__.update(obj_dict)
    236        
     237
    237238    def get(self, key, default=None):
    238239        """
    239240        Returns the last data value for the passed key. If key doesn't exist
    class MultiValueDict(dict):  
    301302    def values(self):
    302303        """Returns a list of the last value on every key list."""
    303304        return [self[key] for key in self.keys()]
    304        
     305
    305306    def itervalues(self):
    306307        """Yield the last value on every key list."""
    307308        for key in self.iterkeys():
    308309            yield self[key]
    309    
     310
    310311    def copy(self):
    311312        """Returns a copy of this object."""
    312313        return self.__deepcopy__()
Back to Top