Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #3993 -- Added some useful dictionary methods to SortedDict, pl…

…us an insert() method. Patch from Paul Collier.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6350 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ec7d80b9dd6182735545433665d9af46605264d2 1 parent 8ccf202
Malcolm Tredinnick malcolmt authored
Showing with 21 additions and 0 deletions.
  1. +1 −0  AUTHORS
  2. +20 −0 django/utils/datastructures.py
1  AUTHORS
View
@@ -83,6 +83,7 @@ answer newbie questions, and generally made Django that much better:
Russell Cloran <russell@rucus.net>
colin@owlfish.com
crankycoder@gmail.com
+ Paul Collier <paul@paul-collier.com>
Pete Crosier <pete.crosier@gmail.com>
Matt Croydon <http://www.postneo.com/>
flavio.curella@gmail.com
20 django/utils/datastructures.py
View
@@ -72,12 +72,23 @@ def __iter__(self):
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 @@ def value_for_index(self, index):
"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.
Please sign in to comment.
Something went wrong with that request. Please try again.