7
7
8
8
9
9
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
+ """
10
17
def decorator (func ):
18
+ """
19
+ @cached decorator.
20
+ :param func: The decorated function
21
+ :return: wrapper
22
+ """
11
23
# type checks
12
24
if not _is_function (func ):
13
25
raise TypeError ('Unable to do memoization on non-function object ' + str (func ))
@@ -23,6 +35,9 @@ def decorator(func):
23
35
24
36
@wraps (func )
25
37
def wrapper (* args , ** kwargs ):
38
+ """
39
+ The actual executed function when a function is decorated with @cached.
40
+ """
26
41
input_args = _hashable_args (args , kwargs )
27
42
function_id = id (func )
28
43
specified_cache = _cache [function_id ]
@@ -41,6 +56,12 @@ def wrapper(*args, **kwargs):
41
56
42
57
43
58
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
+ """
44
65
def del_unit (func_id , cache_value_items ):
45
66
for input_args , cache_unit in cache_value_items :
46
67
if cache_unit ['access_count' ] < safe_access_count :
@@ -54,6 +75,11 @@ def del_unit(func_id, cache_value_items):
54
75
55
76
56
77
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
+ """
57
83
if func is None :
58
84
for item in _cache .values ():
59
85
item .clear ()
@@ -62,6 +88,11 @@ def clear(func=None):
62
88
63
89
64
90
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
+ """
65
96
if func is None :
66
97
return reduce (lambda accumulation , cache_value : len (accumulation ) + len (cache_value )
67
98
if isinstance (accumulation , dict ) else accumulation + len (cache_value ),
@@ -71,35 +102,66 @@ def size(func=None):
71
102
72
103
73
104
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
+ """
74
110
return len (_cache [func_id ])
75
111
76
112
77
113
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
+ """
78
120
kwargs_str = ''
79
121
for key , value in kwargs :
80
122
kwargs_str += key + '=' + value + ';'
81
123
return str (args ) + kwargs_str
82
124
83
125
84
126
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
+ """
85
132
return hasattr (obj , '__call__' )
86
133
87
134
88
135
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
+ """
89
141
if func .func_closure is None :
90
142
raise TypeError ('Unable to retrieve the undecorated function: The function '
91
143
+ func .__name__ + ' is not decorated' )
92
144
return func .func_closure [0 ].cell_contents
93
145
94
146
95
147
def _error_unrecognized_function (func ):
148
+ """
149
+ Raise an error caused by an unrecognized function.
150
+ :param func:
151
+ :return:
152
+ """
96
153
if not _is_function (func ):
97
154
raise TypeError (str (func ) + ' is not a function' )
98
155
else :
99
156
raise NameError ('Function <' + func .__name__ + '> not found' )
100
157
101
158
102
159
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
+ """
103
165
function_id = id (_retrieve_undecorated_function (func ))
104
166
if function_id not in _cache .keys (): # panic
105
167
_error_unrecognized_function (func )
0 commit comments