Skip to content

Commit 7aad5db

Browse files
committed
move memoized to util
1 parent fbdea57 commit 7aad5db

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

pyevmasm/evmasm.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,6 @@
11
from builtins import map, next, chr, range, object
22
from binascii import hexlify, unhexlify
3-
import collections
4-
import functools
5-
6-
7-
class memoized(object):
8-
'''Decorator. Caches a function's return value each time it is called.
9-
If called later with the same arguments, the cached value is returned
10-
(not reevaluated).
11-
'''
12-
13-
def __init__(self, func):
14-
self.func = func
15-
self.cache = {}
16-
17-
def __call__(self, *args, **kwargs):
18-
key = args + tuple(sorted(kwargs.items()))
19-
if not isinstance(key, collections.Hashable):
20-
# uncacheable. a list, for instance.
21-
# better to not cache than blow up.
22-
return self.func(*args, **kwargs)
23-
if key in self.cache:
24-
return self.cache[key]
25-
else:
26-
value = self.func(*args, **kwargs)
27-
self.cache[key] = value
28-
return value
29-
30-
def __repr__(self):
31-
'''Return the function's docstring.'''
32-
return self.func.__doc__
33-
34-
def __get__(self, obj, objtype):
35-
'''Support instance methods.'''
36-
return functools.partial(self.__call__, obj)
3+
from .util import memoized
374

385

396
class EVMAsm(object):

pyevmasm/util.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import collections
2+
import functools
3+
4+
from future.builtins import object
5+
6+
7+
class memoized(object):
8+
'''Decorator. Caches a function's return value each time it is called.
9+
If called later with the same arguments, the cached value is returned
10+
(not reevaluated).
11+
'''
12+
13+
def __init__(self, func):
14+
self.func = func
15+
self.cache = {}
16+
17+
def __call__(self, *args, **kwargs):
18+
key = args + tuple(sorted(kwargs.items()))
19+
if not isinstance(key, collections.Hashable):
20+
# uncacheable. a list, for instance.
21+
# better to not cache than blow up.
22+
return self.func(*args, **kwargs)
23+
if key in self.cache:
24+
return self.cache[key]
25+
else:
26+
value = self.func(*args, **kwargs)
27+
self.cache[key] = value
28+
return value
29+
30+
def __repr__(self):
31+
'''Return the function's docstring.'''
32+
return self.func.__doc__
33+
34+
def __get__(self, obj, objtype):
35+
'''Support instance methods.'''
36+
return functools.partial(self.__call__, obj)

0 commit comments

Comments
 (0)