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> Size: 8B
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
Instructions for database updating
In case of database structure upgrade, developers must follow these steps:
- Update the new database structure modifying /cacholote/database.py, using SQLAlchemy ORM technologies
- Execute from the cacholote work folder:
alembic revision -m "message about the db modification"
- The last command will create a new python file inside /alembic/versions. Fill the
upgrade
function with the operations that must be executed to migrate the database from the old structure to the new one. Keep in mind both DDL (structure modification) and DML (data modification) instructions. For reference, use https://alembic.sqlalchemy.org/en/latest/ops.html#ops. Similarly, do the same with thedowngrade
function. - Commit and push the modifications and the new file.
For details about the alembic migration tool, see the Alembic tutorial.
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
Built Distribution
File details
Details for the file cacholote-1.2.1.tar.gz
.
File metadata
- Download URL: cacholote-1.2.1.tar.gz
- Upload date:
- Size: 51.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eee6dafb0794e9ab059bc3e5c3d9465fd71b634c4dd5052905d43485a913468 |
|
MD5 | 2584ea31c89c480d048fa2821227fb2a |
|
BLAKE2b-256 | abd7b6011bd7e1cbbc92182162bfea611945fb7f0847223f2daba080eddc1df7 |
File details
Details for the file cacholote-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: cacholote-1.2.1-py3-none-any.whl
- Upload date:
- Size: 36.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9adbd1e6eb8a4a290eecfdc2a79cedd638028efbbe9fe9fa4a87681771878dc |
|
MD5 | d46ab81159656a96b37c10058ad6e98b |
|
BLAKE2b-256 | 4d3c4725f41aabbf62d7818cdfb6af2a8db646c5b5cf98372dcf01cc1042f2b7 |