File tree Expand file tree Collapse file tree 2 files changed +25
-17
lines changed Expand file tree Collapse file tree 2 files changed +25
-17
lines changed Original file line number Diff line number Diff line change 4
4
5
5
from labelled_functions .abstract import Unknown
6
6
from labelled_functions .labels import label , LabelledFunction
7
+ from copy import copy
7
8
8
9
9
10
# API
@@ -49,22 +50,15 @@ def timed_func(*args, **kwargs):
49
50
return timed_func
50
51
51
52
52
- # def cache(func, memory=None):
53
- # """Return a copy of func but with results cached with joblib.
53
+ def decorate (func , decorator ):
54
+ new_func = copy (func )
55
+ new_func .function = decorator (func .function )
56
+ return new_func
54
57
55
- # TODO: When used on a pipeline, do not merge the pipeline into a single function.
56
- # """
57
- # from labelled_functions.labels import LabelledFunction
58
58
59
- # if memory is None:
60
- # from joblib import Memory
61
- # memory = Memory("/tmp", verbose=0)
62
-
63
- # return LabelledFunction(
64
- # memory.cache(func),
65
- # name=func.name,
66
- # input_names=func.input_names,
67
- # output_names=func.output_names,
68
- # default_values=func.default_values,
69
- # )
59
+ def cache (func , memory = None ):
60
+ if memory is None :
61
+ from joblib import Memory
62
+ memory = Memory ("/tmp" , verbose = 0 )
63
+ return decorate (func , memory .cache )
70
64
Original file line number Diff line number Diff line change 3
3
4
4
import pytest
5
5
6
- from labelled_functions .decorators import keeping_inputs
6
+ from labelled_functions import label , pipeline
7
+ from labelled_functions .decorators import keeping_inputs , decorate , cache
7
8
from example_functions import *
8
9
9
10
@@ -26,3 +27,16 @@ def test_keeping_inputs():
26
27
with pytest .raises (TypeError ):
27
28
keeping_inputs (all_kinds_of_args )(0 , 1 , 2 , 3 )
28
29
30
+
31
+ def test_cache ():
32
+ # Cache one function
33
+ pipe = cache (label (random_radius )) | label (cylinder_volume )
34
+ a = pipe (length = 1.0 )
35
+ b = pipe (length = 1.0 )
36
+ assert a == b
37
+
38
+ # Cache whole function
39
+ pipe = cache (pipeline ([random_radius , cylinder_volume ]))
40
+ a = pipe (length = 1.0 )
41
+ b = pipe (length = 1.0 )
42
+ assert a == b
You can’t perform that action at this time.
0 commit comments