Skip to content

Commit 279e985

Browse files
committed
add docs
1 parent 7b5a231 commit 279e985

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

memoization.py

+62
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@
77

88

99
def cached(max_items=None):
10+
"""
11+
@cached decorator wrapper.
12+
:param max_items: The max items can be held in memoization cache
13+
* NOT RECOMMENDED *
14+
This argument, if given, can dramatically slow down the performance.
15+
:return: decorator
16+
"""
1017
def decorator(func):
18+
"""
19+
@cached decorator.
20+
:param func: The decorated function
21+
:return: wrapper
22+
"""
1123
# type checks
1224
if not _is_function(func):
1325
raise TypeError('Unable to do memoization on non-function object ' + str(func))
@@ -23,6 +35,9 @@ def decorator(func):
2335

2436
@wraps(func)
2537
def wrapper(*args, **kwargs):
38+
"""
39+
The actual executed function when a function is decorated with @cached.
40+
"""
2641
input_args = _hashable_args(args, kwargs)
2742
function_id = id(func)
2843
specified_cache = _cache[function_id]
@@ -41,6 +56,12 @@ def wrapper(*args, **kwargs):
4156

4257

4358
def clean(safe_access_count=1, func=None):
59+
"""
60+
Remove the cached items, which are accessed fewer than a given value, of a certain function.
61+
:param safe_access_count: The access times that are safe from cleaning process
62+
:param func: The certain function that needs cleaning, if not given, cleaning process will be executed
63+
for all functions
64+
"""
4465
def del_unit(func_id, cache_value_items):
4566
for input_args, cache_unit in cache_value_items:
4667
if cache_unit['access_count'] < safe_access_count:
@@ -54,6 +75,11 @@ def del_unit(func_id, cache_value_items):
5475

5576

5677
def clear(func=None):
78+
"""
79+
Remove all cached items of a certain function or all functions.
80+
:param func: The certain function that will be cleared, if not given, clearing process will be executed
81+
for all functions
82+
"""
5783
if func is None:
5884
for item in _cache.values():
5985
item.clear()
@@ -62,6 +88,11 @@ def clear(func=None):
6288

6389

6490
def size(func=None):
91+
"""
92+
Get the cache size of a certain function or all functions.
93+
:param func: The certain function that needs size calculation
94+
:return: The cache size
95+
"""
6596
if func is None:
6697
return reduce(lambda accumulation, cache_value: len(accumulation) + len(cache_value)
6798
if isinstance(accumulation, dict) else accumulation + len(cache_value),
@@ -71,35 +102,66 @@ def size(func=None):
71102

72103

73104
def _size_explicit(func_id):
105+
"""
106+
Get the cache size by function id.
107+
:param func_id: The given function id
108+
:return: The cache size
109+
"""
74110
return len(_cache[func_id])
75111

76112

77113
def _hashable_args(args, kwargs):
114+
"""
115+
Turn arguments in any shape into a hashable string.
116+
:param args: args
117+
:param kwargs: kwargs
118+
:return: a hashable string
119+
"""
78120
kwargs_str = ''
79121
for key, value in kwargs:
80122
kwargs_str += key + '=' + value + ';'
81123
return str(args) + kwargs_str
82124

83125

84126
def _is_function(obj):
127+
"""
128+
Check if a object is function.
129+
:param obj: The object to be checked
130+
:return: True if it's a function, False otherwise
131+
"""
85132
return hasattr(obj, '__call__')
86133

87134

88135
def _retrieve_undecorated_function(func):
136+
"""
137+
Retrieve the original (undecorated) function by a given decorated one.
138+
:param func: The decorated function
139+
:return: The original function
140+
"""
89141
if func.func_closure is None:
90142
raise TypeError('Unable to retrieve the undecorated function: The function '
91143
+ func.__name__ + ' is not decorated')
92144
return func.func_closure[0].cell_contents
93145

94146

95147
def _error_unrecognized_function(func):
148+
"""
149+
Raise an error caused by an unrecognized function.
150+
:param func:
151+
:return:
152+
"""
96153
if not _is_function(func):
97154
raise TypeError(str(func) + ' is not a function')
98155
else:
99156
raise NameError('Function <' + func.__name__ + '> not found')
100157

101158

102159
def _retrieve_safe_function_id(func):
160+
"""
161+
Retrieve the id of the original (undecorated) function by a given decorated one.
162+
:param func: The decorated function
163+
:return: The id of the original function
164+
"""
103165
function_id = id(_retrieve_undecorated_function(func))
104166
if function_id not in _cache.keys(): # panic
105167
_error_unrecognized_function(func)

0 commit comments

Comments
 (0)