giscanner: use collections.OrderedDict when available
authorDieter Verfaillie <dieterv@optionexplicit.be>
Thu, 5 Jul 2012 19:30:01 +0000 (21:30 +0200)
committerDieter Verfaillie <dieterv@optionexplicit.be>
Wed, 28 Nov 2012 20:31:22 +0000 (21:31 +0100)
Starting with Python 2.7 we can use the batteries included
collections.OrderedDict class. However, configure.ac claims
we still support Python 2.5 and 2.6, so don't remove our
custom odict implementation just yet...

https://bugzilla.gnome.org/show_bug.cgi?id=688897

giscanner/odict.py

index df703cb..fa164c3 100644 (file)
 
 """odict - an ordered dictionary"""
 
-from UserDict import DictMixin
+try:
+    # Starting with Python 2.7 we can use collections.OrderedDict
+    from collections import OrderedDict as odict
+except ImportError:
+    # But we still support Python 2.5 and 2.6
+    from UserDict import DictMixin
 
 
-class odict(DictMixin):
+    class odict(DictMixin):
 
-    def __init__(self):
-        self._items = {}
-        self._keys = []
+        def __init__(self):
+            self._items = {}
+            self._keys = []
 
-    def __setitem__(self, key, value):
-        if key not in self._items:
-            self._keys.append(key)
-        self._items[key] = value
+        def __setitem__(self, key, value):
+            if key not in self._items:
+                self._keys.append(key)
+            self._items[key] = value
 
-    def __getitem__(self, key):
-        return self._items[key]
+        def __getitem__(self, key):
+            return self._items[key]
 
-    def __delitem__(self, key):
-        del self._items[key]
-        self._keys.remove(key)
+        def __delitem__(self, key):
+            del self._items[key]
+            self._keys.remove(key)
 
-    def keys(self):
-        return self._keys[:]
+        def keys(self):
+            return self._keys[:]