Skip to main content

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:

  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 database updating

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.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cacholote-1.3.1.tar.gz (51.8 kB view details)

Uploaded Source

Built Distribution

cacholote-1.3.1-py3-none-any.whl (36.3 kB view details)

Uploaded Python 3

File details

Details for the file cacholote-1.3.1.tar.gz.

File metadata

  • Download URL: cacholote-1.3.1.tar.gz
  • Upload date:
  • Size: 51.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for cacholote-1.3.1.tar.gz
Algorithm Hash digest
SHA256 1efae515beead92c1728061cb8642045f38467b1524065405ef9e7193c6c5e3f
MD5 58f07bd74c773c7d7010e5838798b188
BLAKE2b-256 1692dc8d0fd07a26b87a30205bd2d8bed67967669182f598432bece1eafac751

See more details on using hashes here.

File details

Details for the file cacholote-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: cacholote-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 36.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for cacholote-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e28227a01ca2e925b618667a8397ef17baecde3f90ca732b4cdeac7f32021ccc
MD5 6140fc17e14ec1e1965a056d37142c3f
BLAKE2b-256 effabec077eaba9fd2fa50433508dfbc535f13cb281a9f04450abaa5f4c17206

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page