Skip to content

Commit

Permalink
Some changes to SortedDict to make it faster under py2
Browse files Browse the repository at this point in the history
Refs #19276
  • Loading branch information
akaariai committed Nov 13, 2012
1 parent 00ff69a commit fa18b0a
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions django/utils/datastructures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import copy
import warnings
from types import GeneratorType
from django.utils import six


Expand Down Expand Up @@ -120,27 +119,23 @@ def __new__(cls, *args, **kwargs):
return instance

def __init__(self, data=None):
if data is None:
data = {}
elif isinstance(data, GeneratorType):
# Unfortunately we need to be able to read a generator twice. Once
# to get the data into self with our super().__init__ call and a
# second time to setup keyOrder correctly
data = list(data)
super(SortedDict, self).__init__(data)
if isinstance(data, dict):
self.keyOrder = list(data)
if data is None or isinstance(data, dict):
data = data or []
super(SortedDict, self).__init__(data)
self.keyOrder = list(data) if data else []
else:
self.keyOrder = []
seen = set()
super(SortedDict, self).__init__()
super_set = super(SortedDict, self).__setitem__
for key, value in data:
if key not in seen:
# Take the ordering from first key
if key not in self:
self.keyOrder.append(key)
seen.add(key)
# But override with last value in data (dict() does this)
super_set(key, value)

def __deepcopy__(self, memo):
return self.__class__([(key, copy.deepcopy(value, memo))
for key, value in six.iteritems(self)])
for key, value in self.items()])

def __copy__(self):
# The Python's default copy implementation will alter the state
Expand Down Expand Up @@ -199,13 +194,13 @@ def _itervalues(self):
itervalues = _itervalues

def items(self):
return list(self.iteritems())
return [(k, self[k]) for k in self.keyOrder]

def keys(self):
return list(self.iterkeys())
return self.keyOrder[:]

def values(self):
return list(self.itervalues())
return [self[k] for k in self.keyOrder]

def update(self, dict_):
for k, v in six.iteritems(dict_):
Expand Down

0 comments on commit fa18b0a

Please sign in to comment.