Efficiently cache calls to functions
Project description
cacholote
Efficiently cache calls to functions
Quick Start
>>> import cacholote
>>> cacholote.config.set(cache_db_urlpath="sqlite://")
<cacholote.config.set ...
>>> @cacholote.cacheable
... def now():
... import datetime
... return datetime.datetime.now()
>>> now() == now()
True
>>> with cacholote.config.set(use_cache=False):
... now() == now()
False
Cache files
>>> import cacholote
>>> import tempfile
>>> tmpdir = tempfile.TemporaryDirectory().name
>>> cacholote.config.set(
... cache_db_urlpath="sqlite://",
... cache_files_urlpath=tmpdir,
... )
<cacholote.config.set ...
>>> cached_open = cacholote.cacheable(open)
>>> cached_file = cached_open("README.md")
>>> cached_file.name.startswith(tmpdir)
True
>>> import filecmp
>>> filecmp.cmp("README.md", cached_file.name)
True
Cache Xarray objects
>>> import cacholote
>>> import pytest
>>> xr = pytest.importorskip("xarray")
>>> import tempfile
>>> tmpdir = tempfile.TemporaryDirectory().name
>>> cacholote.config.set(
... cache_db_urlpath="sqlite://",
... cache_files_urlpath=tmpdir,
... )
<cacholote.config.set ...
>>> @cacholote.cacheable
... def dataset_from_dict(ds_dict):
... return xr.Dataset(ds_dict)
>>> ds = dataset_from_dict({"foo": 0})
>>> ds
<xarray.Dataset>
Dimensions: ()
Data variables:
foo int64 ...
>>> ds.encoding["source"].startswith(tmpdir)
True
Configuration
Configuration settings can be accessed using cacholote.config.get()
and modified using cacholote.config.set(**kwargs)
. It is possible to use cacholote.config.set
either as a context manager, or to configure global settings. See help(cacholote.config.set)
.
Defaults are controlled by environment variables and dotenv files. See help(cacholote.config.reset)
.
Workflow for developers/contributors
For best experience create a new conda environment (e.g. DEVELOP) with Python 3.11:
conda create -n DEVELOP -c conda-forge python=3.11
conda activate DEVELOP
Before pushing to GitHub, run the following commands:
- Update conda environment:
make conda-env-update
- Install this package:
pip install -e .
- Sync with the latest template (optional):
make template-update
- Run quality assurance checks:
make qa
- Run tests:
make unit-tests
- Run the static type checker:
make type-check
- Build the documentation (see Sphinx tutorial):
make docs-build
License
Copyright 2019, B-Open Solutions srl.
Copyright 2022, European Union.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
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
cacholote-0.8.0.tar.gz
(44.9 kB
view hashes)
Built Distribution
cacholote-0.8.0-py3-none-any.whl
(30.2 kB
view hashes)
Close
Hashes for cacholote-0.8.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bb3b82135b6381f2f53652a907aca94226dbed09e03936f750505a47cb395ca |
|
MD5 | 07ea5387bd8c46cebce668f2ed4d579e |
|
BLAKE2b-256 | e3a79a19c3abc39f3d93e72c1ab7e619d9e9307c055e3a9763fe11c3ba5f2771 |