跳转到主要内容

高效缓存函数调用

项目描述

卡乔洛特

高效缓存函数调用

快速入门

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

缓存文件

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

缓存Xarray对象

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

配置

配置设置可以通过使用 cacholote.config.get() 访问,并使用 cacholote.config.set(**kwargs) 修改。可以使用 cacholote.config.set 作为上下文管理器,或配置全局设置。请参阅 help(cacholote.config.set)

默认值受环境变量和dotenv文件控制。请参阅 help(cacholote.config.reset)

开发者/贡献者工作流程

为了获得最佳体验,请创建一个新的conda环境(例如DEVELOP),使用Python 3.11

conda create -n DEVELOP -c conda-forge python=3.11
conda activate DEVELOP

在推送到GitHub之前,运行以下命令

  1. 更新conda环境: make conda-env-update
  2. 安装此包: pip install -e .
  3. 同步最新 模板(可选): make template-update
  4. 运行质量保证检查: make qa
  5. 运行测试: make unit-tests
  6. 运行静态类型检查器: make type-check
  7. 构建文档(请参阅 Sphinx教程): make docs-build

数据库更新说明

如果发生数据库结构升级,开发者必须遵循以下步骤

  1. 更新新的数据库结构,修改 /cacholote/database.py,使用 SQLAlchemy ORM技术
  2. 在cacholote工作文件夹中执行
    alembic revision -m "message about the db modification"
    
  3. 最后一条命令将在 /alembic/versions 内创建一个新的 Python 文件。在 upgrade 函数中填充必须执行的迁移操作,以将数据库从旧结构迁移到新结构。请记住同时包含 DDL(结构修改)和 DML(数据修改)指令。有关参考,请访问 https://alembic.sqlalchemy.org.cn/en/latest/ops.html#ops。同样,也要对 downgrade 函数进行相同的操作。
  4. 提交并推送修改和新文件。

有关 alembic 迁移工具的详细信息,请参阅 Alembic 教程

许可证

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

    https://apache.ac.cn/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.

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

cacholote-1.4.0.tar.gz (52.1 kB 查看哈希值)

上传时间

构建分发

cacholote-1.4.0-py3-none-any.whl (36.4 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF 赞助商 PingdomPingdom 监控 SentrySentry 错误记录 StatusPageStatusPage 状态页面