Fixed #11874 -- Optimizations and cleanups for SortedDict. Thanks to Alex Gaynor and Armin Ronacher for the report and patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11815 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
7cb8892fdb
commit
333615accb
|
@ -1,3 +1,6 @@
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
|
||||||
class MergeDict(object):
|
class MergeDict(object):
|
||||||
"""
|
"""
|
||||||
A simple class for creating new "virtual" dictionaries that actually look
|
A simple class for creating new "virtual" dictionaries that actually look
|
||||||
|
@ -72,22 +75,20 @@ class SortedDict(dict):
|
||||||
self.keyOrder.append(key)
|
self.keyOrder.append(key)
|
||||||
|
|
||||||
def __deepcopy__(self, memo):
|
def __deepcopy__(self, memo):
|
||||||
from copy import deepcopy
|
|
||||||
return self.__class__([(key, deepcopy(value, memo))
|
return self.__class__([(key, deepcopy(value, memo))
|
||||||
for key, value in self.iteritems()])
|
for key, value in self.iteritems()])
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
super(SortedDict, self).__setitem__(key, value)
|
if key not in self:
|
||||||
if key not in self.keyOrder:
|
|
||||||
self.keyOrder.append(key)
|
self.keyOrder.append(key)
|
||||||
|
super(SortedDict, self).__setitem__(key, value)
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
super(SortedDict, self).__delitem__(key)
|
super(SortedDict, self).__delitem__(key)
|
||||||
self.keyOrder.remove(key)
|
self.keyOrder.remove(key)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for k in self.keyOrder:
|
return iter(self.keyOrder)
|
||||||
yield k
|
|
||||||
|
|
||||||
def pop(self, k, *args):
|
def pop(self, k, *args):
|
||||||
result = super(SortedDict, self).pop(k, *args)
|
result = super(SortedDict, self).pop(k, *args)
|
||||||
|
@ -108,7 +109,7 @@ class SortedDict(dict):
|
||||||
|
|
||||||
def iteritems(self):
|
def iteritems(self):
|
||||||
for key in self.keyOrder:
|
for key in self.keyOrder:
|
||||||
yield key, super(SortedDict, self).__getitem__(key)
|
yield key, self[key]
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
return self.keyOrder[:]
|
return self.keyOrder[:]
|
||||||
|
@ -117,18 +118,18 @@ class SortedDict(dict):
|
||||||
return iter(self.keyOrder)
|
return iter(self.keyOrder)
|
||||||
|
|
||||||
def values(self):
|
def values(self):
|
||||||
return map(super(SortedDict, self).__getitem__, self.keyOrder)
|
return map(self.__getitem__, self.keyOrder)
|
||||||
|
|
||||||
def itervalues(self):
|
def itervalues(self):
|
||||||
for key in self.keyOrder:
|
for key in self.keyOrder:
|
||||||
yield super(SortedDict, self).__getitem__(key)
|
yield self[key]
|
||||||
|
|
||||||
def update(self, dict_):
|
def update(self, dict_):
|
||||||
for k, v in dict_.items():
|
for k, v in dict_.iteritems():
|
||||||
self.__setitem__(k, v)
|
self[k] = v
|
||||||
|
|
||||||
def setdefault(self, key, default):
|
def setdefault(self, key, default):
|
||||||
if key not in self.keyOrder:
|
if key not in self:
|
||||||
self.keyOrder.append(key)
|
self.keyOrder.append(key)
|
||||||
return super(SortedDict, self).setdefault(key, default)
|
return super(SortedDict, self).setdefault(key, default)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue