Skip to main content

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:

  1. Update conda environment: make conda-env-update
  2. Install this package: pip install -e .
  3. Sync with the latest template (optional): make template-update
  4. Run quality assurance checks: make qa
  5. Run tests: make unit-tests
  6. Run the static type checker: make type-check
  7. Build the documentation (see Sphinx tutorial): make docs-build

Instructions for creating a new database version

In case of database structure upgrade, developers must follow these steps:

  1. Update the new database structure modifying /cacholote/database.py, using SQLAlchemy ORM technologies
  2. Execute from the cacholote work folder:
    alembic revision -m "message about the db modification"
    
  3. 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 the downgrade function.
  4. Commit and push the modifications and the new file.

Instructions for moving between different database versions

The package comes with its own 'cacholote-alembic-cli' script in order to move between different database versions. This script is a slight modified version of the 'alembic' script, overriding default config path used (cacholote/alembic.ini) and the sqlalchemy.url used, that is automatically computed by the environment and not read from any ini file.

All the database releases where you can migrate up and down must be defined by files contained inside the folder /cacholote/alembic/versions. All these files are in a version queue: each file has link to its revision hash (variable 'revision', the prefix of the file name) and to the next older one (variable 'down_revision'), and contains code to step up and down that database version.
Some useful commands are listed below.

  • To migrate to the newest version, type:
    cacholote-alembic-cli upgrade head
  • To upgrade to a specific version hash, for example 8ccbe515155c, type:
    cacholote-alembic-cli upgrade 8ccbe515155c
  • To downgrade to a specific version hash, for example 8ccbe515155c, type:
    cacholote-alembic-cli downgrade 8ccbe515155c
  • To get the current version hash of the database, type:
    cacholote-alembic-cli current

Database migration changes could be applied to the cacholote component of the database, too. In such case, migrate the cacholote component after the migration by the 'broker-alembic-cli' tool.

Other details are the same of the standard alembic migration tool, see the Alembic tutorial.

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.

Release files for cacholote 1.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cacholote 1.7.1
File Size Uploaded
cacholote-1.7.1.tar.gz 55.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cacholote 1.7.1
File Interpreter ABI Platform
cacholote-1.7.1-py3-none-any.whl Python 3 none any Details

Total release size: 94.4 kB

Release files / cacholote-1.7.1.tar.gz

Download URL cacholote-1.7.1.tar.gz
Size 55.1 kB
Tags Source
SHA-256 checksum
How to use checksums
56b22cf4ad0e70b606c8c5982c403ea01c44d95ad41866b006ed14873ab3eadb
BLAKE2b-256 checksum
How to use checksums
b80438efde8c62664aac8f6035b5687179215e53a43691b90d4849a6f335a9a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 9, 2025.

Transparency log

Release files / cacholote-1.7.1-py3-none-any.whl

Download URL cacholote-1.7.1-py3-none-any.whl
Size 39.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a4bf37ab94cb4c226ea24eb056e547cdedd1133f5eca228c44e3b99b5b9dff2
BLAKE2b-256 checksum
How to use checksums
69fb3ea16b06f646ef2dd8e8219828e88537e68f5325e25eccaf92b4a5180b2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 9, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.7.1 This release

2 release files

1.7.0

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page