mirror of
https://github.com/ARM-software/devlib.git
synced 2025-01-31 02:00:45 +00:00
devlib/utils/misc: improve memoize decorator
Using the wrapt module we can improve the memoize decorator. In fact, it allows to preserve the attributes of the memoized function, such as signature, docstring, path to the file where the function is implemented, and so on. Signed-off-by: Michele Di Giorgio <michele.digiorgio@arm.com>
This commit is contained in:
parent
ee521f64e6
commit
539e9b34b9
@ -33,6 +33,7 @@ from operator import itemgetter
|
|||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
|
import wrapt
|
||||||
|
|
||||||
# ABI --> architectures list
|
# ABI --> architectures list
|
||||||
ABI_MAP = {
|
ABI_MAP = {
|
||||||
@ -536,17 +537,18 @@ def mask_to_list(mask):
|
|||||||
__memo_cache = {}
|
__memo_cache = {}
|
||||||
|
|
||||||
|
|
||||||
def memoized(func):
|
@wrapt.decorator
|
||||||
|
def memoized(wrapped, instance, args, kwargs):
|
||||||
"""A decorator for memoizing functions and methods."""
|
"""A decorator for memoizing functions and methods."""
|
||||||
func_id = repr(func)
|
func_id = repr(wrapped)
|
||||||
|
|
||||||
def memoize_wrapper(*args, **kwargs):
|
def memoize_wrapper(*args, **kwargs):
|
||||||
id_string = func_id + ','.join([str(id(a)) for a in args])
|
id_string = func_id + ','.join([str(id(a)) for a in args])
|
||||||
id_string += ','.join('{}={}'.format(k, v)
|
id_string += ','.join('{}={}'.format(k, v)
|
||||||
for k, v in kwargs.iteritems())
|
for k, v in kwargs.iteritems())
|
||||||
if id_string not in __memo_cache:
|
if id_string not in __memo_cache:
|
||||||
__memo_cache[id_string] = func(*args, **kwargs)
|
__memo_cache[id_string] = wrapped(*args, **kwargs)
|
||||||
return __memo_cache[id_string]
|
return __memo_cache[id_string]
|
||||||
|
|
||||||
return memoize_wrapper
|
return memoize_wrapper(*args, **kwargs)
|
||||||
|
|
||||||
|
1
setup.py
1
setup.py
@ -69,6 +69,7 @@ params = dict(
|
|||||||
'python-dateutil', # converting between UTC and local time.
|
'python-dateutil', # converting between UTC and local time.
|
||||||
'pexpect>=3.3', # Send/recieve to/from device
|
'pexpect>=3.3', # Send/recieve to/from device
|
||||||
'pyserial', # Serial port interface
|
'pyserial', # Serial port interface
|
||||||
|
'wrapt', # Basic for construction of decorator functions
|
||||||
],
|
],
|
||||||
extras_require={
|
extras_require={
|
||||||
'daq': ['daqpower'],
|
'daq': ['daqpower'],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user