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>
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.10:

conda create -n DEVELOP -c conda-forge python=3.10
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

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-0.5.3.tar.gz (44.4 kB view details)

Uploaded Source

Built Distribution

cacholote-0.5.3-py3-none-any.whl (30.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cacholote-0.5.3.tar.gz
  • Upload date:
  • Size: 44.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for cacholote-0.5.3.tar.gz
Algorithm Hash digest
SHA256 cd93edd48913ea47d86ad094887ce83c2d949a25346ef424fb6c09dc412da72b
MD5 a4843af234a973d492909ee0a45e20ee
BLAKE2b-256 01f1d2611f64e51bf1e59e31b1b62a52adb55a596b0e76db3d0c5de2fd5a8444

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cacholote-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 30.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for cacholote-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 da274db630b06370acd5d75a7d8c6f10ffe1612318e2750d689355092501ba7a
MD5 9a2ba90f389d55692147200d1c897927
BLAKE2b-256 d046d0ede9e6d2a457955c07add8f806d8d44681555102b631e18f692d58ed7d

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