Index
Scrat Library
stash(serializer=None, name=None, hashers=None, hash_code=True, ignore_args=None, watch_functions=None, watch_globals=None, force=None, disable=None, max_size=None, cache_policy=CachePolicy.lru)
#
Wrap a function to stash the results
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serializer |
Optional[Serializer]
|
Select a serializer for the function's result, by default a good
serializer is inferred from the typehint, using |
None
|
name |
Optional[str]
|
Name that identifies this function, by default the function name is used. |
None
|
hashers |
Optional[Dict[str, Hasher]]
|
Dictionary specifying hashers used for the arguments, by default hashers
are selected according to the type of the argument, using |
None
|
hash_code |
Optional[bool]
|
Control if the function's code should be used in the hash, by default True. |
True
|
ignore_args |
Optional[List[str]]
|
List of arguments to ignore from the hash, by default None |
None
|
watch_functions |
Optional[List[Any]]
|
List of functions which code should be included in the hash, by default None |
None
|
watch_globals |
Optional[List[str]]
|
List of global variables to include in the hash, by default None |
None
|
force |
Optional[bool]
|
If set to True the stash is ignored, the function is called and the result
is saved to the stash, by default the global setting |
None
|
disable |
Optional[bool]
|
If set to True the stash is ignored, the function called and the result
is not saved, by default the global setting |
None
|
max_size |
Optional[int]
|
Maximum size allowed for files of this function, if the limit is about to be met other files are removed befor storing a new one based on the cache_policy |
None
|
cache_policy |
CachePolicy
|
Cache policy, by default Least Recentrly Used (LRU) is applied |
lru
|
Notes#
If possible, avoid using the default PickleSerializer
. This serializer is used by
default because it works with most objects but pickle is not a good format to store
the results long-term. We encourage users to select one the other serializers
provided or writing a custom one.
Examples:
Simple example
>>> import scrat as sc
>>> @sc.stash()
>>> def funcion():
>>> return 1
Custom serializer
>>> @sc.stash(serializer=sc.JsonSerializer())
>>> def funcion():
>>> return {"json": True}
Source code in scrat/decorator.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|