Skip to content

Commit 64d0ad3

Browse files
committed
modify cache_contains_* api (alive_only defaults to True); update test cases
1 parent bb47828 commit 64d0ad3

File tree

6 files changed

+45
-30
lines changed

6 files changed

+45
-30
lines changed

memoization/caching/fifo_cache.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def cache_is_full():
116116
"""Return True if the cache is full"""
117117
return full
118118

119-
def cache_contains_argument(function_arguments, alive_only=False):
119+
def cache_contains_argument(function_arguments, alive_only=True):
120120
"""
121121
Return True if the cache contains a cached item with the specified function call arguments
122122
@@ -138,7 +138,7 @@ def cache_contains_argument(function_arguments, alive_only=False):
138138
can be represented by:
139139
{'a': 4, 'b': 5, 'c': 6}
140140
141-
:param alive_only: Whether to check alive cache item only (default to False).
141+
:param alive_only: Whether to check alive cache item only (default to True).
142142
143143
:return: True if the desired cached item is present, False otherwise.
144144
"""
@@ -162,14 +162,14 @@ def cache_contains_argument(function_arguments, alive_only=False):
162162
return values_toolkit.is_cache_value_valid(node[_VALUE]) if alive_only else True
163163
return False
164164

165-
def cache_contains_key(key, alive_only=False):
165+
def cache_contains_key(key, alive_only=True):
166166
"""
167167
Return True if the cache contains a cache item with the specified key. This function is only recommended to use
168168
when you provide a custom key maker; otherwise, use cache_contains_argument() instead.
169169
170170
:param key: A key built by the default key maker or a custom key maker.
171171
172-
:param alive_only: Whether to check alive cache item only (default to False).
172+
:param alive_only: Whether to check alive cache item only (default to True).
173173
174174
:return: True if the desired cached item is present, False otherwise.
175175
"""
@@ -179,14 +179,14 @@ def cache_contains_key(key, alive_only=False):
179179
return values_toolkit.is_cache_value_valid(node[_VALUE]) if alive_only else True
180180
return False
181181

182-
def cache_contains_result(return_value, alive_only=False):
182+
def cache_contains_result(return_value, alive_only=True):
183183
"""
184184
Return True if the cache contains a cache item with the specified user function return value. O(n) time
185185
complexity.
186186
187187
:param return_value: A return value coming from the user function.
188188
189-
:param alive_only: Whether to check alive cache item only (default to False).
189+
:param alive_only: Whether to check alive cache item only (default to True).
190190
191191
:return: True if the desired cached item is present, False otherwise.
192192
"""

memoization/caching/lfu_cache.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def cache_is_full():
8787
"""Return True if the cache is full"""
8888
return cache.__len__() >= max_size
8989

90-
def cache_contains_argument(function_arguments, alive_only=False):
90+
def cache_contains_argument(function_arguments, alive_only=True):
9191
"""
9292
Return True if the cache contains a cached item with the specified function call arguments
9393
@@ -109,7 +109,7 @@ def cache_contains_argument(function_arguments, alive_only=False):
109109
can be represented by:
110110
{'a': 4, 'b': 5, 'c': 6}
111111
112-
:param alive_only: Whether to check alive cache item only (default to False).
112+
:param alive_only: Whether to check alive cache item only (default to True).
113113
114114
:return: True if the desired cached item is present, False otherwise.
115115
"""
@@ -133,14 +133,14 @@ def cache_contains_argument(function_arguments, alive_only=False):
133133
return values_toolkit.is_cache_value_valid(cache_node.value) if alive_only else True
134134
return False
135135

136-
def cache_contains_key(key, alive_only=False):
136+
def cache_contains_key(key, alive_only=True):
137137
"""
138138
Return True if the cache contains a cache item with the specified key. This function is only recommended to use
139139
when you provide a custom key maker; otherwise, use cache_contains_argument() instead.
140140
141141
:param key: A key built by the default key maker or a custom key maker.
142142
143-
:param alive_only: Whether to check alive cache item only (default to False).
143+
:param alive_only: Whether to check alive cache item only (default to True).
144144
145145
:return: True if the desired cached item is present, False otherwise.
146146
"""
@@ -150,14 +150,14 @@ def cache_contains_key(key, alive_only=False):
150150
return values_toolkit.is_cache_value_valid(cache_node.value) if alive_only else True
151151
return False
152152

153-
def cache_contains_result(return_value, alive_only=False):
153+
def cache_contains_result(return_value, alive_only=True):
154154
"""
155155
Return True if the cache contains a cache item with the specified user function return value. O(n) time
156156
complexity.
157157
158158
:param return_value: A return value coming from the user function.
159159
160-
:param alive_only: Whether to check alive cache item only (default to False).
160+
:param alive_only: Whether to check alive cache item only (default to True).
161161
162162
:return: True if the desired cached item is present, False otherwise.
163163
"""

memoization/caching/lru_cache.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def cache_is_full():
125125
"""Return True if the cache is full"""
126126
return full
127127

128-
def cache_contains_argument(function_arguments, alive_only=False):
128+
def cache_contains_argument(function_arguments, alive_only=True):
129129
"""
130130
Return True if the cache contains a cached item with the specified function call arguments
131131
@@ -147,7 +147,7 @@ def cache_contains_argument(function_arguments, alive_only=False):
147147
can be represented by:
148148
{'a': 4, 'b': 5, 'c': 6}
149149
150-
:param alive_only: Whether to check alive cache item only (default to False).
150+
:param alive_only: Whether to check alive cache item only (default to True).
151151
152152
:return: True if the desired cached item is present, False otherwise.
153153
"""
@@ -171,14 +171,14 @@ def cache_contains_argument(function_arguments, alive_only=False):
171171
return values_toolkit.is_cache_value_valid(node[_VALUE]) if alive_only else True
172172
return False
173173

174-
def cache_contains_key(key, alive_only=False):
174+
def cache_contains_key(key, alive_only=True):
175175
"""
176176
Return True if the cache contains a cache item with the specified key. This function is only recommended to use
177177
when you provide a custom key maker; otherwise, use cache_contains_argument() instead.
178178
179179
:param key: A key built by the default key maker or a custom key maker.
180180
181-
:param alive_only: Whether to check alive cache item only (default to False).
181+
:param alive_only: Whether to check alive cache item only (default to True).
182182
183183
:return: True if the desired cached item is present, False otherwise.
184184
"""
@@ -188,14 +188,14 @@ def cache_contains_key(key, alive_only=False):
188188
return values_toolkit.is_cache_value_valid(node[_VALUE]) if alive_only else True
189189
return False
190190

191-
def cache_contains_result(return_value, alive_only=False):
191+
def cache_contains_result(return_value, alive_only=True):
192192
"""
193193
Return True if the cache contains a cache item with the specified user function return value. O(n) time
194194
complexity.
195195
196196
:param return_value: A return value coming from the user function.
197197
198-
:param alive_only: Whether to check alive cache item only (default to False).
198+
:param alive_only: Whether to check alive cache item only (default to True).
199199
200200
:return: True if the desired cached item is present, False otherwise.
201201
"""

memoization/caching/plain_cache.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def cache_is_full():
7272
"""Return True if the cache is full"""
7373
return False
7474

75-
def cache_contains_argument(function_arguments, alive_only=False):
75+
def cache_contains_argument(function_arguments, alive_only=True):
7676
"""
7777
Return True if the cache contains a cached item with the specified function call arguments
7878
@@ -94,7 +94,7 @@ def cache_contains_argument(function_arguments, alive_only=False):
9494
can be represented by:
9595
{'a': 4, 'b': 5, 'c': 6}
9696
97-
:param alive_only: Whether to check alive cache item only (default to False).
97+
:param alive_only: Whether to check alive cache item only (default to True).
9898
9999
:return: True if the desired cached item is present, False otherwise.
100100
"""
@@ -118,14 +118,14 @@ def cache_contains_argument(function_arguments, alive_only=False):
118118
return values_toolkit.is_cache_value_valid(value) if alive_only else True
119119
return False
120120

121-
def cache_contains_key(key, alive_only=False):
121+
def cache_contains_key(key, alive_only=True):
122122
"""
123123
Return True if the cache contains a cache item with the specified key. This function is only recommended to use
124124
when you provide a custom key maker; otherwise, use cache_contains_argument() instead.
125125
126126
:param key: A key built by the default key maker or a custom key maker.
127127
128-
:param alive_only: Whether to check alive cache item only (default to False).
128+
:param alive_only: Whether to check alive cache item only (default to True).
129129
130130
:return: True if the desired cached item is present, False otherwise.
131131
"""
@@ -135,14 +135,14 @@ def cache_contains_key(key, alive_only=False):
135135
return values_toolkit.is_cache_value_valid(value) if alive_only else True
136136
return False
137137

138-
def cache_contains_result(return_value, alive_only=False):
138+
def cache_contains_result(return_value, alive_only=True):
139139
"""
140140
Return True if the cache contains a cache item with the specified user function return value. O(n) time
141141
complexity.
142142
143143
:param return_value: A return value coming from the user function.
144144
145-
:param alive_only: Whether to check alive cache item only (default to False).
145+
:param alive_only: Whether to check alive cache item only (default to True).
146146
147147
:return: True if the desired cached item is present, False otherwise.
148148
"""

memoization/caching/statistic_cache.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def cache_is_full():
4545
"""Return True if the cache is full"""
4646
return True
4747

48-
def cache_contains_argument(function_arguments, alive_only=False):
48+
def cache_contains_argument(function_arguments, alive_only=True):
4949
"""
5050
Return True if the cache contains a cached item with the specified function call arguments
5151
@@ -67,33 +67,33 @@ def cache_contains_argument(function_arguments, alive_only=False):
6767
can be represented by:
6868
{'a': 4, 'b': 5, 'c': 6}
6969
70-
:param alive_only: Whether to check alive cache item only (default to False).
70+
:param alive_only: Whether to check alive cache item only (default to True).
7171
7272
:return: True if the desired cached item is present, False otherwise.
7373
"""
7474
return False
7575

76-
def cache_contains_key(key, alive_only=False):
76+
def cache_contains_key(key, alive_only=True):
7777
"""
7878
Return True if the cache contains a cache item with the specified key. This function is only recommended to use
7979
when you provide a custom key maker; otherwise, use cache_contains_argument() instead.
8080
8181
:param key: A key built by the default key maker or a custom key maker.
8282
83-
:param alive_only: Whether to check alive cache item only (default to False).
83+
:param alive_only: Whether to check alive cache item only (default to True).
8484
8585
:return: True if the desired cached item is present, False otherwise.
8686
"""
8787
return False
8888

89-
def cache_contains_result(return_value, alive_only=False):
89+
def cache_contains_result(return_value, alive_only=True):
9090
"""
9191
Return True if the cache contains a cache item with the specified user function return value. O(n) time
9292
complexity.
9393
9494
:param return_value: A return value coming from the user function.
9595
96-
:param alive_only: Whether to check alive cache item only (default to False).
96+
:param alive_only: Whether to check alive cache item only (default to True).
9797
9898
:return: True if the desired cached item is present, False otherwise.
9999
"""

test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,23 @@ def test_memoization_for_cache_contains(self):
492492
self.assertFalse(tested_function.cache_contains_argument((100,), alive_only=True))
493493
self.assertFalse(tested_function.cache_contains_key(tested_function.cache_make_key((100,), None), alive_only=True))
494494
self.assertFalse(tested_function.cache_contains_result(100, alive_only=True))
495+
self.assertTrue(tested_function.cache_contains_argument((100,), alive_only=False))
496+
self.assertTrue(tested_function.cache_contains_key(tested_function.cache_make_key((100,), None), alive_only=False))
497+
self.assertTrue(tested_function.cache_contains_result(100, alive_only=False))
495498

496499
self.assertFalse(tested_function.cache_contains_argument({'keyword': 10}, alive_only=True))
497500
self.assertFalse(tested_function.cache_contains_key(tested_function.cache_make_key((), {'keyword': 10}), alive_only=True))
498501
self.assertFalse(tested_function.cache_contains_result(1, alive_only=True))
502+
self.assertTrue(tested_function.cache_contains_argument({'keyword': 10}, alive_only=False))
503+
self.assertTrue(tested_function.cache_contains_key(tested_function.cache_make_key((), {'keyword': 10}), alive_only=False))
504+
self.assertTrue(tested_function.cache_contains_result(1, alive_only=False))
499505

500506
self.assertFalse(tested_function.cache_contains_argument([(50, 2, 3, 4), {'keyword1': 5, 'keyword2': 6}], alive_only=True))
501507
self.assertFalse(tested_function.cache_contains_key(tested_function.cache_make_key((50, 2, 3, 4), {'keyword1': 5, 'keyword2': 6}), alive_only=True))
502508
self.assertFalse(tested_function.cache_contains_result(50, alive_only=True))
509+
self.assertTrue(tested_function.cache_contains_argument([(50, 2, 3, 4), {'keyword1': 5, 'keyword2': 6}], alive_only=False))
510+
self.assertTrue(tested_function.cache_contains_key(tested_function.cache_make_key((50, 2, 3, 4), {'keyword1': 5, 'keyword2': 6}), alive_only=False))
511+
self.assertTrue(tested_function.cache_contains_result(50, alive_only=False))
503512

504513
def test_memoization_for_cache_remove_if(self):
505514
for tested_function in (f33, f34, f35, f36):
@@ -545,8 +554,14 @@ def is_dead(cache_key, cache_result, is_alive):
545554

546555
time.sleep(0.6) # wait until cache expires
547556

557+
self.assertTrue(tested_function.cache_contains_argument((1,), alive_only=False))
558+
self.assertFalse(tested_function.cache_contains_argument((1,), alive_only=True))
559+
548560
tested_function(2)
549561
tested_function.cache_remove_if(is_dead)
562+
self.assertFalse(tested_function.cache_contains_argument((1,), alive_only=False))
563+
self.assertFalse(tested_function.cache_contains_argument((42,), alive_only=False))
564+
self.assertFalse(tested_function.cache_contains_argument({'c': 42}, alive_only=False))
550565
self.assertFalse(tested_function.cache_contains_argument((1,), alive_only=True))
551566
self.assertFalse(tested_function.cache_contains_argument((42,), alive_only=True))
552567
self.assertFalse(tested_function.cache_contains_argument({'c': 42}, alive_only=True))

0 commit comments

Comments
 (0)