Skip to content

Commit def0485

Browse files
committed
-
1 parent e7e8a8f commit def0485

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

python_toolbox/caching/cache.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,15 @@ def cached(function, *args, **kwargs):
9191

9292
result = decorator_tools.decorator(cached, function)
9393

94-
def cache_clear():
95-
'''Clear the cache, deleting all saved results.'''
96-
cached._cache.clear()
94+
def cache_clear(key=None):
95+
if key is None:
96+
cached._cache.clear()
97+
else:
98+
try:
99+
del cached._cache[key]
100+
except KeyError:
101+
pass
102+
97103
result.cache_clear = cache_clear
98104

99105
result.is_cached = True

python_toolbox/dict_tools.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'''Defines several functions that may be useful when working with dicts.'''
55

66

7-
def filter_items(d, condition):
7+
def filter_items(d, condition, force_dict_type=None):
88
'''
99
Get new dict with items from `d` that satisfy the `condition` functions.
1010
@@ -16,7 +16,10 @@ def filter_items(d, condition):
1616
'''
1717
# todo future: possibly shallow-copy `d` to allow for dict classes that
1818
# have more state, (like default factory.)
19-
dict_type = type(d) if (type(d).__name__ != 'dictproxy') else dict
19+
if force_dict_type is not None:
20+
dict_type = force_dict_type
21+
else:
22+
dict_type = type(d) if (type(d).__name__ != 'dictproxy') else dict
2023
return dict_type(
2124
(key, value) for (key, value) in d.iteritems() if condition(key, value)
2225
)

0 commit comments

Comments
 (0)