Skip to content

Commit 92d12ec

Browse files
committed
Remove conditional for OrderedDict.
All supported Python versions have OrderedDict.
1 parent 0925408 commit 92d12ec

1 file changed

Lines changed: 24 additions & 46 deletions

File tree

sqlparse/utils.py

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,39 @@
55
'''
66

77
import re
8+
from collections import OrderedDict
89

9-
try:
10-
from collections import OrderedDict
11-
except ImportError:
12-
OrderedDict = None
1310

11+
class Cache(OrderedDict):
12+
"""Cache with LRU algorithm using an OrderedDict as basis
13+
"""
14+
def __init__(self, maxsize=100):
15+
OrderedDict.__init__(self)
1416

15-
if OrderedDict:
16-
class Cache(OrderedDict):
17-
"""Cache with LRU algorithm using an OrderedDict as basis
18-
"""
19-
def __init__(self, maxsize=100):
20-
OrderedDict.__init__(self)
21-
22-
self._maxsize = maxsize
23-
24-
def __getitem__(self, key, *args, **kwargs):
25-
# Get the key and remove it from the cache, or raise KeyError
26-
value = OrderedDict.__getitem__(self, key)
27-
del self[key]
28-
29-
# Insert the (key, value) pair on the front of the cache
30-
OrderedDict.__setitem__(self, key, value)
31-
32-
# Return the value from the cache
33-
return value
34-
35-
def __setitem__(self, key, value, *args, **kwargs):
36-
# Key was inserted before, remove it so we put it at front later
37-
if key in self:
38-
del self[key]
17+
self._maxsize = maxsize
3918

40-
# Too much items on the cache, remove the least recent used
41-
elif len(self) >= self._maxsize:
42-
self.popitem(False)
19+
def __getitem__(self, key, *args, **kwargs):
20+
# Get the key and remove it from the cache, or raise KeyError
21+
value = OrderedDict.__getitem__(self, key)
22+
del self[key]
4323

44-
# Insert the (key, value) pair on the front of the cache
45-
OrderedDict.__setitem__(self, key, value, *args, **kwargs)
24+
# Insert the (key, value) pair on the front of the cache
25+
OrderedDict.__setitem__(self, key, value)
4626

47-
else:
48-
class Cache(dict):
49-
"""Cache that reset when gets full
50-
"""
51-
def __init__(self, maxsize=100):
52-
dict.__init__(self)
27+
# Return the value from the cache
28+
return value
5329

54-
self._maxsize = maxsize
30+
def __setitem__(self, key, value, *args, **kwargs):
31+
# Key was inserted before, remove it so we put it at front later
32+
if key in self:
33+
del self[key]
5534

56-
def __setitem__(self, key, value, *args, **kwargs):
57-
# Reset the cache if we have too much cached entries and start over
58-
if len(self) >= self._maxsize:
59-
self.clear()
35+
# Too much items on the cache, remove the least recent used
36+
elif len(self) >= self._maxsize:
37+
self.popitem(False)
6038

61-
# Insert the (key, value) pair on the front of the cache
62-
dict.__setitem__(self, key, value, *args, **kwargs)
39+
# Insert the (key, value) pair on the front of the cache
40+
OrderedDict.__setitem__(self, key, value, *args, **kwargs)
6341

6442

6543
def memoize_generator(func):

0 commit comments

Comments
 (0)