From ec7d80b9dd6182735545433665d9af46605264d2 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 16 Sep 2007 03:27:38 +0000 Subject: [PATCH] Fixed #3993 -- Added some useful dictionary methods to SortedDict, plus an insert() method. Patch from Paul Collier. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6350 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/utils/datastructures.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/AUTHORS b/AUTHORS index 9d47f2c876..94659c88fa 100644 --- a/AUTHORS +++ b/AUTHORS @@ -83,6 +83,7 @@ answer newbie questions, and generally made Django that much better: Russell Cloran colin@owlfish.com crankycoder@gmail.com + Paul Collier Pete Crosier Matt Croydon flavio.curella@gmail.com diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index ac890d5da6..40e99c3962 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -72,12 +72,23 @@ class SortedDict(dict): def items(self): return zip(self.keyOrder, self.values()) + def iteritems(self): + for key in self.keyOrder: + yield key, dict.__getitem__(self, key) + def keys(self): return self.keyOrder[:] + def iterkeys(self): + return iter(self.keyOrder) + def values(self): return [dict.__getitem__(self, k) for k in self.keyOrder] + def itervalues(self): + for key in self.keyOrder: + yield dict.__getitem__(self, key) + def update(self, dict): for k, v in dict.items(): self.__setitem__(k, v) @@ -91,6 +102,15 @@ class SortedDict(dict): "Returns the value of the item at the given zero-based index." return self[self.keyOrder[index]] + def insert(self, index, key, value): + "Inserts the key, value pair before the item with the given index." + if key in self.keyOrder: + n = self.keyOrder.index(key) + del self.keyOrder[n] + if n < index: index -= 1 + self.keyOrder.insert(index, key) + dict.__setitem__(self, key, value) + def copy(self): "Returns a copy of this object." # This way of initializing the copy means it works for subclasses, too.