chromatictools.pickle module¶
Utilities for pickle
- chromatictools.pickle.pickled_cache(filepath: Union[str, Callable, property]) Callable[[Callable], Callable]¶
Store the last result of the function call in a pickled file (string version)
- Parameters
filepath (str or property or method) – The path of the file to read/write as a string or as a getter method or property
- Returns
function decorator. The decorated function will also have an attribute function ‘forced’, that calls the function forcing cache overwriting
- Return type
Callable[[Callable], Callable]
Example
>>> from chromatictools.pickle import pickled_cache >>> cache_file: str = ".pickled_example.cache" >>> @pickled_cache(cache_file) ... def echo(s: str): ... return s >>> echo("Alice") 'Alice' >>> echo("Bob") 'Alice' >>> echo.forced("Bob") 'Bob' >>> echo("Alice") 'Bob' >>> import os >>> os.remove(cache_file)
Remove cache for inter-session repeatability (otherwise, the second time the example is run, the first echo(“Alice”) would return “Bob”). In a real scenario, you probably wouldn’t want to do this, as inter-session memory is the main purpose of this decorator
- chromatictools.pickle.read_pickled(filepath: str, encoding: str = 'latin1') Any¶
Read the pickled data in the file
- Parameters
filepath (str) – The file path
encoding (str) – File encoding. Defaults to
"latin1"
- Returns
The unpickled data
- chromatictools.pickle.save_pickled(obj: Any, filepath: str)¶
Save data in a pickled file
- Parameters
obj (Any) – The data to pickle
filepath (str) – The path of the file to write. Directory is created if it doesn’t exist