Permalink
Browse files

Merge pull request #444 from mitar/patch-2

Allow reversed iteration over SortedDict.
  • Loading branch information...
2 parents 6b0d93d + 2811e54 commit 6b3d2bc9810153506620c7794a7a87ade29e3a97 @alex alex committed Oct 21, 2012
Showing with 9 additions and 0 deletions.
  1. +3 −0 django/utils/datastructures.py
  2. +6 −0 tests/regressiontests/utils/datastructures.py
@@ -160,6 +160,9 @@ def __delitem__(self, key):
def __iter__(self):
return iter(self.keyOrder)
+ def __reversed__(self):
+ return reversed(self.keyOrder)
+
def pop(self, k, *args):
result = super(SortedDict, self).pop(k, *args)
try:
@@ -128,6 +128,12 @@ def test_clear(self):
self.assertEqual(self.d1, {})
self.assertEqual(self.d1.keyOrder, [])
+ def test_reversed(self):
+ self.assertEqual(list(self.d1), [7, 1, 9])
+ self.assertEqual(list(self.d2), [1, 9, 0, 7])
+ self.assertEqual(list(reversed(self.d1)), [9, 1, 7])
+ self.assertEqual(list(reversed(self.d2)), [7, 0, 9, 1])
+
def test_insert(self):
d = SortedDict()
with warnings.catch_warnings(record=True) as w:

0 comments on commit 6b3d2bc

Please sign in to comment.