Skip to content

Commit

Permalink
Remove conditional for OrderedDict.
Browse files Browse the repository at this point in the history
All supported Python versions have OrderedDict.
  • Loading branch information
andialbrecht committed Oct 26, 2015
1 parent 0925408 commit 92d12ec
Showing 1 changed file with 24 additions and 46 deletions.
70 changes: 24 additions & 46 deletions sqlparse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,39 @@
'''

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):
Expand Down

0 comments on commit 92d12ec

Please sign in to comment.