No project description provided
Project description
contextcache
Cache a python function only in certain contexts.
Usage
Here's an example:
# example.py
import contextvars
import contextcache
# Define a private ContextVar to store the cached values. Don't touch this ContextVar!
_double_cache = contextvars.ContextVar("double_cache", default=None)
# Use the `enable_caching` decorator to enable context caching for `double`.
@contextcache.enable_caching(_double_cache)
def double(n: int) -> int:
print(f"doubling {n}")
return n * 2
# Without caching.
double(1)
double(1)
# With caching.
with contextcache.use_caching(double):
double(2)
double(2)
Here's the output:
python example.py
doubling 1
doubling 1
doubling 2
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
contextcache-0.1.0.tar.gz
(1.8 kB
view hashes)
Built Distribution
Close
Hashes for contextcache-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45ecc7bda1a6b529f0ee5fd28bd571570b96a1bbd446a8581726134a5e1d6842 |
|
MD5 | 115f0c7ac467db749e82c512393e1eab |
|
BLAKE2b-256 | e882e2bf84691fca5bd7495c70cb6fb87a69182e3671e62ea1ed5705612d7edb |