changeset 6644:e8cbb2188354

compat: merge OrderedDict base class which only is used once with dict - then inline _dict_impl as dict.
author domruf <dominikruf@gmail.com>
date Wed, 10 May 2017 21:43:53 +0200
parents 4897dffd46b6
children ce818fb1e264
files kallithea/lib/compat.py
diffstat 1 files changed, 46 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/compat.py	Tue May 09 22:16:23 2017 +0200
+++ b/kallithea/lib/compat.py	Wed May 10 21:43:53 2017 +0200
@@ -77,16 +77,13 @@
 _nil = _Nil()
 
 
-class _odict(object):
+class OrderedDict(dict):
     """Ordered dict data structure, with O(1) complexity for dict operations
     that modify one element.
 
     Overwriting values doesn't change their original sequential order.
     """
 
-    def _dict_impl(self):
-        return None
-
     def __init__(self, data=(), **kwds):
         """This doesn't accept keyword initialization as normal dicts to avoid
         a trap - inside a function or method the keyword args are accessible
@@ -96,7 +93,7 @@
         if kwds:
             raise TypeError("__init__() of ordered dict takes no keyword "
                             "arguments to avoid an ordering trap.")
-        self._dict_impl().__init__(self)
+        dict.__init__(self)
         # If you give a normal dict, then the order of elements is undefined
         if hasattr(data, "iteritems"):
             for key, val in data.iteritems():
@@ -107,57 +104,53 @@
 
     # Double-linked list header
     def _get_lh(self):
-        dict_impl = self._dict_impl()
         if not hasattr(self, '_lh'):
-            dict_impl.__setattr__(self, '_lh', _nil)
-        return dict_impl.__getattribute__(self, '_lh')
+            dict.__setattr__(self, '_lh', _nil)
+        return dict.__getattribute__(self, '_lh')
 
     def _set_lh(self, val):
-        self._dict_impl().__setattr__(self, '_lh', val)
+        dict.__setattr__(self, '_lh', val)
 
     lh = property(_get_lh, _set_lh)
 
     # Double-linked list tail
     def _get_lt(self):
-        dict_impl = self._dict_impl()
         if not hasattr(self, '_lt'):
-            dict_impl.__setattr__(self, '_lt', _nil)
-        return dict_impl.__getattribute__(self, '_lt')
+            dict.__setattr__(self, '_lt', _nil)
+        return dict.__getattribute__(self, '_lt')
 
     def _set_lt(self, val):
-        self._dict_impl().__setattr__(self, '_lt', val)
+        dict.__setattr__(self, '_lt', val)
 
     lt = property(_get_lt, _set_lt)
 
     def __getitem__(self, key):
-        return self._dict_impl().__getitem__(self, key)[1]
+        return dict.__getitem__(self, key)[1]
 
     def __setitem__(self, key, val):
-        dict_impl = self._dict_impl()
         try:
-            dict_impl.__getitem__(self, key)[1] = val
+            dict.__getitem__(self, key)[1] = val
         except KeyError:
-            new = [dict_impl.__getattribute__(self, 'lt'), val, _nil]
-            dict_impl.__setitem__(self, key, new)
-            if dict_impl.__getattribute__(self, 'lt') == _nil:
-                dict_impl.__setattr__(self, 'lh', key)
+            new = [dict.__getattribute__(self, 'lt'), val, _nil]
+            dict.__setitem__(self, key, new)
+            if dict.__getattribute__(self, 'lt') == _nil:
+                dict.__setattr__(self, 'lh', key)
             else:
-                dict_impl.__getitem__(
-                    self, dict_impl.__getattribute__(self, 'lt'))[2] = key
-            dict_impl.__setattr__(self, 'lt', key)
+                dict.__getitem__(
+                    self, dict.__getattribute__(self, 'lt'))[2] = key
+            dict.__setattr__(self, 'lt', key)
 
     def __delitem__(self, key):
-        dict_impl = self._dict_impl()
-        pred, _, succ = self._dict_impl().__getitem__(self, key)
+        pred, _, succ = dict.__getitem__(self, key)
         if pred == _nil:
-            dict_impl.__setattr__(self, 'lh', succ)
+            dict.__setattr__(self, 'lh', succ)
         else:
-            dict_impl.__getitem__(self, pred)[2] = succ
+            dict.__getitem__(self, pred)[2] = succ
         if succ == _nil:
-            dict_impl.__setattr__(self, 'lt', pred)
+            dict.__setattr__(self, 'lt', pred)
         else:
-            dict_impl.__getitem__(self, succ)[0] = pred
-        dict_impl.__delitem__(self, key)
+            dict.__getitem__(self, succ)[0] = pred
+        dict.__delitem__(self, key)
 
     def __contains__(self, key):
         return key in self.keys()
@@ -178,16 +171,15 @@
 
     def get(self, k, x=None):
         if k in self:
-            return self._dict_impl().__getitem__(self, k)[1]
+            return dict.__getitem__(self, k)[1]
         else:
             return x
 
     def __iter__(self):
-        dict_impl = self._dict_impl()
-        curr_key = dict_impl.__getattribute__(self, 'lh')
+        curr_key = dict.__getattribute__(self, 'lh')
         while curr_key != _nil:
             yield curr_key
-            curr_key = dict_impl.__getitem__(self, curr_key)[2]
+            curr_key = dict.__getitem__(self, curr_key)[2]
 
     iterkeys = __iter__
 
@@ -195,20 +187,18 @@
         return list(self.iterkeys())
 
     def itervalues(self):
-        dict_impl = self._dict_impl()
-        curr_key = dict_impl.__getattribute__(self, 'lh')
+        curr_key = dict.__getattribute__(self, 'lh')
         while curr_key != _nil:
-            _, val, curr_key = dict_impl.__getitem__(self, curr_key)
+            _, val, curr_key = dict.__getitem__(self, curr_key)
             yield val
 
     def values(self):
         return list(self.itervalues())
 
     def iteritems(self):
-        dict_impl = self._dict_impl()
-        curr_key = dict_impl.__getattribute__(self, 'lh')
+        curr_key = dict.__getattribute__(self, 'lh')
         while curr_key != _nil:
-            _, val, next_key = dict_impl.__getitem__(self, curr_key)
+            _, val, next_key = dict.__getitem__(self, curr_key)
             yield curr_key, val
             curr_key = next_key
 
@@ -229,10 +219,9 @@
         self.__init__(items)
 
     def clear(self):
-        dict_impl = self._dict_impl()
-        dict_impl.clear(self)
-        dict_impl.__setattr__(self, 'lh', _nil)
-        dict_impl.__setattr__(self, 'lt', _nil)
+        dict.clear(self)
+        dict.__setattr__(self, 'lh', _nil)
+        dict.__setattr__(self, 'lt', _nil)
 
     def copy(self):
         return self.__class__(self)
@@ -265,8 +254,7 @@
 
     def popitem(self):
         try:
-            dict_impl = self._dict_impl()
-            key = dict_impl.__getattribute__(self, 'lt')
+            key = dict.__getattribute__(self, 'lt')
             return key, self.pop(key)
         except KeyError:
             raise KeyError("'popitem(): ordered dictionary is empty'")
@@ -274,11 +262,10 @@
     def riterkeys(self):
         """To iterate on keys in reversed order.
         """
-        dict_impl = self._dict_impl()
-        curr_key = dict_impl.__getattribute__(self, 'lt')
+        curr_key = dict.__getattribute__(self, 'lt')
         while curr_key != _nil:
             yield curr_key
-            curr_key = dict_impl.__getitem__(self, curr_key)[0]
+            curr_key = dict.__getitem__(self, curr_key)[0]
 
     __reversed__ = riterkeys
 
@@ -290,10 +277,9 @@
     def ritervalues(self):
         """To iterate on values in reversed order.
         """
-        dict_impl = self._dict_impl()
-        curr_key = dict_impl.__getattribute__(self, 'lt')
+        curr_key = dict.__getattribute__(self, 'lt')
         while curr_key != _nil:
-            curr_key, val, _ = dict_impl.__getitem__(self, curr_key)
+            curr_key, val, _ = dict.__getitem__(self, curr_key)
             yield val
 
     def rvalues(self):
@@ -304,10 +290,9 @@
     def riteritems(self):
         """To iterate on (key, value) in reversed order.
         """
-        dict_impl = self._dict_impl()
-        curr_key = dict_impl.__getattribute__(self, 'lt')
+        curr_key = dict.__getattribute__(self, 'lt')
         while curr_key != _nil:
-            pred_key, val, _ = dict_impl.__getitem__(self, curr_key)
+            pred_key, val, _ = dict.__getitem__(self, curr_key)
             yield curr_key, val
             curr_key = pred_key
 
@@ -318,34 +303,27 @@
 
     def firstkey(self):
         if self:
-            return self._dict_impl().__getattribute__(self, 'lh')
+            return dict.__getattribute__(self, 'lh')
         else:
             raise KeyError("'firstkey(): ordered dictionary is empty'")
 
     def lastkey(self):
         if self:
-            return self._dict_impl().__getattribute__(self, 'lt')
+            return dict.__getattribute__(self, 'lt')
         else:
             raise KeyError("'lastkey(): ordered dictionary is empty'")
 
     def as_dict(self):
-        return self._dict_impl()(self.items())
+        return dict(self.items())
 
     def _repr(self):
         """_repr(): low level repr of the whole data contained in the odict.
         Useful for debugging.
         """
-        dict_impl = self._dict_impl()
         form = "odict low level repr lh,lt,data: %r, %r, %s"
-        return form % (dict_impl.__getattribute__(self, 'lh'),
-                       dict_impl.__getattribute__(self, 'lt'),
-                       dict_impl.__repr__(self))
-
-
-class OrderedDict(_odict, dict):
-
-    def _dict_impl(self):
-        return dict
+        return form % (dict.__getattribute__(self, 'lh'),
+                       dict.__getattribute__(self, 'lt'),
+                       dict.__repr__(self))
 
 
 #==============================================================================