Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

general: switch from deprecated apprdirs to platformdirs, update readme #74

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@
"{flink('cachew.settings')} exposes some parameters that allow you to control `cachew` behaviour:\n",
"- `ENABLE`: set to `False` if you want to disable caching for without removing the decorators (useful for testing and debugging).\n",
" You can also use {flink('cachew.extra.disabled_cachew')} context manager to do it temporarily.\n",
"- `DEFAULT_CACHEW_DIR`: override to set a different base directory. The default is the \"user cache directory\" (see [appdirs docs](https://github.com/ActiveState/appdirs#some-example-output)).\n",
"- `DEFAULT_CACHEW_DIR`: override to set a different base directory. The default is the \"user cache directory\" (see [platformdirs docs](https://github.com/tox-dev/platformdirs?tab=readme-ov-file#example-output)).\n",
"- `THROW_ON_ERROR`: by default, cachew is defensive and simply attemps to cause the original function on caching issues.\n",
" Set to `True` to catch errors earlier.\n",
"- `DEFAULT_BACKEND`: currently supported are `sqlite` and `file` (file is somewhat experimental, although should work too). \n",
Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ Cachew gives the best of two worlds and makes it both **easy and efficient**. Th

# How it works

- first your objects get [converted](src/cachew/marshall/cachew.py#L33) into a simpler JSON-like representation
- first your objects get [converted](src/cachew/marshall/cachew.py#L30) into a simpler JSON-like representation
- after that, they are mapped into byte blobs via [`orjson`](https://github.com/ijl/orjson).

When the function is called, cachew [computes the hash of your function's arguments ](src/cachew/__init__.py:#L592)
When the function is called, cachew [computes the hash of your function's arguments ](src/cachew/__init__.py:#L588)
and compares it against the previously stored hash value.

- If they match, it would deserialize and yield whatever is stored in the cache database
Expand All @@ -140,18 +140,18 @@ and compares it against the previously stored hash value.



* automatic schema inference: [1](src/cachew/tests/test_cachew.py#L371), [2](src/cachew/tests/test_cachew.py#L385)
* automatic schema inference: [1](src/cachew/tests/test_cachew.py#L387), [2](src/cachew/tests/test_cachew.py#L401)
* supported types:

* primitive: `str`, `int`, `float`, `bool`, `datetime`, `date`, `Exception`

See [tests.test_types](src/cachew/tests/test_cachew.py#L698), [tests.test_primitive](src/cachew/tests/test_cachew.py#L734), [tests.test_dates](src/cachew/tests/test_cachew.py#L650), [tests.test_exceptions](src/cachew/tests/test_cachew.py#L1133)
* [@dataclass and NamedTuple](src/cachew/tests/test_cachew.py#L615)
* [Optional](src/cachew/tests/test_cachew.py#L515) types
* [Union](src/cachew/tests/test_cachew.py#L841) types
* [nested datatypes](src/cachew/tests/test_cachew.py#L431)
See [tests.test_types](src/cachew/tests/test_cachew.py#L687), [tests.test_primitive](src/cachew/tests/test_cachew.py#L725), [tests.test_dates](src/cachew/tests/test_cachew.py#L637), [tests.test_exceptions](src/cachew/tests/test_cachew.py#L1124)
* [@dataclass and NamedTuple](src/cachew/tests/test_cachew.py#L602)
* [Optional](src/cachew/tests/test_cachew.py#L531) types
* [Union](src/cachew/tests/test_cachew.py#L832) types
* [nested datatypes](src/cachew/tests/test_cachew.py#L447)

* detects [datatype schema changes](src/cachew/tests/test_cachew.py#L461) and discards old data automatically
* detects [datatype schema changes](src/cachew/tests/test_cachew.py#L477) and discards old data automatically


# Performance
Expand All @@ -165,20 +165,20 @@ You can find some of my performance tests in [benchmarks/](benchmarks) dir, and


# Using
See [docstring](src/cachew/__init__.py#L296) for up-to-date documentation on parameters and return types.
See [docstring](src/cachew/__init__.py#L292) for up-to-date documentation on parameters and return types.
You can also use [extensive unit tests](src/cachew/tests/test_cachew.py) as a reference.

Some useful (but optional) arguments of `@cachew` decorator:

* `cache_path` can be a directory, or a callable that [returns a path](src/cachew/tests/test_cachew.py#L408) and depends on function's arguments.
* `cache_path` can be a directory, or a callable that [returns a path](src/cachew/tests/test_cachew.py#L424) and depends on function's arguments.

By default, `settings.DEFAULT_CACHEW_DIR` is used.

* `depends_on` is a function which determines whether your inputs have changed, and the cache needs to be invalidated.

By default it just uses string representation of the arguments, you can also specify a custom callable.

For instance, it can be used to [discard cache](src/cachew/tests/test_cachew.py#L103) if the input file was modified.
For instance, it can be used to [discard cache](src/cachew/tests/test_cachew.py#L119) if the input file was modified.

* `cls` is the type that would be serialized.

Expand Down Expand Up @@ -271,10 +271,10 @@ Now you can use `@mcachew` in place of `@cachew`, and be certain things don't br
## Settings


[cachew.settings](src/cachew/__init__.py#L68) exposes some parameters that allow you to control `cachew` behaviour:
[cachew.settings](src/cachew/__init__.py#L64) exposes some parameters that allow you to control `cachew` behaviour:
- `ENABLE`: set to `False` if you want to disable caching for without removing the decorators (useful for testing and debugging).
You can also use [cachew.extra.disabled_cachew](src/cachew/extra.py#L21) context manager to do it temporarily.
- `DEFAULT_CACHEW_DIR`: override to set a different base directory. The default is the "user cache directory" (see [appdirs docs](https://github.com/ActiveState/appdirs#some-example-output)).
- `DEFAULT_CACHEW_DIR`: override to set a different base directory. The default is the "user cache directory" (see [platformdirs docs](https://github.com/tox-dev/platformdirs?tab=readme-ov-file#example-output)).
- `THROW_ON_ERROR`: by default, cachew is defensive and simply attemps to cause the original function on caching issues.
Set to `True` to catch errors earlier.
- `DEFAULT_BACKEND`: currently supported are `sqlite` and `file` (file is somewhat experimental, although should work too).
Expand Down
2 changes: 1 addition & 1 deletion generate-readme
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
cd "$(dirname "$0")"

# '--TagRemovePreprocessor.remove_cell_tags={"noexport"}'
jupyter nbconvert --execute --to markdown --template readme.tpl README.ipynb
uv run --with=jupyter --extra=testing jupyter nbconvert --execute --to markdown --template readme.tpl README.ipynb

# TODO run it on CI to make sure it renders and up to date?
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
dynamic = ["version"] # version is managed by setuptools_scm
name = "cachew"
dependencies = [
"appdirs" , # default cache dir
"platformdirs", # default cache dir
"sqlalchemy>=1.0", # cache DB interaction
"orjson", # fast json serialization
]
Expand Down
4 changes: 2 additions & 2 deletions src/cachew/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def orjson_dumps(*args, **kwargs): # type: ignore[misc]

orjson_loads = json.loads

import appdirs # type: ignore[import-untyped]
import platformdirs

from .backend.common import AbstractBackend
from .backend.file import FileBackend
Expand Down Expand Up @@ -71,7 +71,7 @@ class settings:
'''
ENABLE: bool = True

DEFAULT_CACHEW_DIR: PathIsh = Path(appdirs.user_cache_dir('cachew'))
DEFAULT_CACHEW_DIR: PathIsh = Path(platformdirs.user_cache_dir('cachew'))

'''
Set to true if you want to fail early. Otherwise falls back to non-cached version
Expand Down
Loading