Browse code

add SortedDict.__getslice__()

It's nice to think of SortedDicts as sorted lists. Which means one
should be sliceable.

Matt Domsch authored on 2014/04/11 09:52:58
Showing 1 changed files
... ...
@@ -47,6 +47,12 @@ class SortedDict(dict):
47 47
     def __iter__(self):
48 48
         return SortedDictIterator(self, self.keys())
49 49
 
50
+    def __getslice__(self, i=0, j=-1):
51
+        keys = self.keys()[i:j]
52
+        r = SortedDict(ignore_case = self.ignore_case)
53
+        for k in keys:
54
+            r[k] = self[k]
55
+        return r
50 56
 
51 57
 
52 58
 if __name__ == "__main__":