File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed
Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change 99# todo: examine thread-safety
1010
1111import functools
12+ import datetime as datetime_module
1213
1314from python_toolbox import decorator_tools
1415
@@ -23,7 +24,7 @@ class CLEAR_ENTIRE_CACHE(object):
2324
2425
2526@decorator_tools .helpful_decorator_builder
26- def cache (max_size = infinity ):
27+ def cache (max_size = infinity , time_limit = None ):
2728 '''
2829 Cache a function, saving results so they won't have to be computed again.
2930
@@ -54,6 +55,19 @@ def f(a, b=2):
5455 # compile a function accordingly, so functions with a simple argspec won't
5556 # have to go through so much shit. update: probably it will help only for
5657 # completely argumentless function. so do one for those.
58+
59+ if time_limit is not None :
60+ if not isinstance (time_limit , datetime_module .timedelta ):
61+ try :
62+ time_limit = datetime_module .timedelta (** time_limit )
63+ except Exception :
64+ raise TypeError (
65+ '`time_limit` must be either a `timedelta` object or a '
66+ 'dict of keyword arguments for constructing a '
67+ '`timedelta` object.'
68+ )
69+ assert isinstance (time_limit , datetime_module .timedelta )
70+
5771
5872 def decorator (function ):
5973
You can’t perform that action at this time.
0 commit comments