summaryrefslogtreecommitdiff
path: root/sqlparse/utils.py
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2015-10-26 20:22:46 +0100
committerAndi Albrecht <albrecht.andi@gmail.com>2015-10-26 20:22:46 +0100
commit92d12ec8c6d62e952d5a8d194db508a24c429ac9 (patch)
tree083e266ee51f17c7ff9ffa9b3fb5de28dd6496a1 /sqlparse/utils.py
parent0925408c3c0ad46e5d0e4f12645493e43e9a4fb4 (diff)
downloadsqlparse-92d12ec8c6d62e952d5a8d194db508a24c429ac9.tar.gz
Remove conditional for OrderedDict.
All supported Python versions have OrderedDict.
Diffstat (limited to 'sqlparse/utils.py')
-rw-r--r--sqlparse/utils.py70
1 files changed, 24 insertions, 46 deletions
diff --git a/sqlparse/utils.py b/sqlparse/utils.py
index ae45679..7db9a96 100644
--- a/sqlparse/utils.py
+++ b/sqlparse/utils.py
@@ -5,61 +5,39 @@ Created on 17/05/2012
'''
import re
+from collections import OrderedDict
-try:
- from collections import OrderedDict
-except ImportError:
- OrderedDict = None
+class Cache(OrderedDict):
+ """Cache with LRU algorithm using an OrderedDict as basis
+ """
+ def __init__(self, maxsize=100):
+ OrderedDict.__init__(self)
-if OrderedDict:
- class Cache(OrderedDict):
- """Cache with LRU algorithm using an OrderedDict as basis
- """
- def __init__(self, maxsize=100):
- OrderedDict.__init__(self)
-
- self._maxsize = maxsize
-
- def __getitem__(self, key, *args, **kwargs):
- # Get the key and remove it from the cache, or raise KeyError
- value = OrderedDict.__getitem__(self, key)
- del self[key]
-
- # Insert the (key, value) pair on the front of the cache
- OrderedDict.__setitem__(self, key, value)
-
- # Return the value from the cache
- return value
-
- def __setitem__(self, key, value, *args, **kwargs):
- # Key was inserted before, remove it so we put it at front later
- if key in self:
- del self[key]
+ self._maxsize = maxsize
- # Too much items on the cache, remove the least recent used
- elif len(self) >= self._maxsize:
- self.popitem(False)
+ def __getitem__(self, key, *args, **kwargs):
+ # Get the key and remove it from the cache, or raise KeyError
+ value = OrderedDict.__getitem__(self, key)
+ del self[key]
- # Insert the (key, value) pair on the front of the cache
- OrderedDict.__setitem__(self, key, value, *args, **kwargs)
+ # Insert the (key, value) pair on the front of the cache
+ OrderedDict.__setitem__(self, key, value)
-else:
- class Cache(dict):
- """Cache that reset when gets full
- """
- def __init__(self, maxsize=100):
- dict.__init__(self)
+ # Return the value from the cache
+ return value
- self._maxsize = maxsize
+ def __setitem__(self, key, value, *args, **kwargs):
+ # Key was inserted before, remove it so we put it at front later
+ if key in self:
+ del self[key]
- def __setitem__(self, key, value, *args, **kwargs):
- # Reset the cache if we have too much cached entries and start over
- if len(self) >= self._maxsize:
- self.clear()
+ # Too much items on the cache, remove the least recent used
+ elif len(self) >= self._maxsize:
+ self.popitem(False)
- # Insert the (key, value) pair on the front of the cache
- dict.__setitem__(self, key, value, *args, **kwargs)
+ # Insert the (key, value) pair on the front of the cache
+ OrderedDict.__setitem__(self, key, value, *args, **kwargs)
def memoize_generator(func):