locache


Namelocache JSON
Version 3.0.0 PyPI version JSON
download
home_page
SummaryCache expensive function calls to disk.
upload_time2023-04-21 16:11:29
maintainer
docs_urlNone
author
requires_python>=3.6
license
keywords cache disk
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # locache
[![PyPI](https://img.shields.io/pypi/v/locache?style=for-the-badge)](https://pypi.org/project/locache/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/locache?color=green&label=Downloads&logo=Python&logoColor=white&style=for-the-badge)](https://pypistats.org/packages/locache)
[![GitHub](https://img.shields.io/github/license/jla-gardner/local-cache?style=for-the-badge)](LICENCE.md)

A small utility library for caching the results of deterministic and pure function calls to disk.
This ability is only intended for use on expensive function calls with simple arguments and keyword arguments.
The cache can optionally invalidate itself if changes to the function's source code are detetcted.

## Installation

`pip3 install locache`

## Examples

When used in normal python scripts, `@persist` is sufficient.

`foo.py`:

```python
from locache import persist

@persist
def my_func(x, num=3):
    print("Hi from foo!")
    return x * num

my_func(1)        # prints "Hi from foo!", returns 3
my_func(2, num=2) # prints "Hi from foo!", returns 4
my_func(1)        # returns 3
my_func(2, num=2) # returns 4
```

Running `foo.py` will lead to the creation of a `foo.cache/my_func/` directory, with files `x=1_num=3` and `x=2_num=2`.

### Notebooks

When using python notebooks, the `name` keyword is also required:

`bar.ipynb`:

```python
from locache import persist

@persist(name="notebook")
def my_func(x, num=3):
    print("Hi from foo!")
    return x * num

my_func(1)        # prints "Hi from foo!", returns 3
my_func(1)        # returns 3
```

Running this cell will lead to the creation of a `notebook.cache/my_func/` directory in the same directory as the notebook.

## Resetting the Cache

By default, the cache is invalidated and reset if source code changes to the function in question are dedicated.
This behaviour can be surpressed: `@persist(auto_invalidate=False)`

Results for specific function calls can be removed from the cache by deleting the appropriate file.

Programmatic resetting of the cache is also possible:

```python
from locache import persist, reset_cache

@persist
def foo(x):
    print("Hi from foo!")
    return x ** 2

foo(1) # prints "Hi from foo!", returns 3
foo(1) # returns 3

reset_cache(foo)

foo(1) # prints "Hi from foo!", returns 3
```

In a notebook setting, the relevant name needs to also be passed:

```python
@persist(name="notebook")
def foo(x):
    return x**2

foo(1)
reset_cache(foo, name="notebook")
```

## Logging

Cache logging can optionally be enabled:

```python
from locache import verbose; verbose(True)
```

## Anti-Examples

Don't pass large data structures to persisted functions:

```python
import numpy as np
from locache import persist

@persist # don't do this!
def foo(x):
    return np.sum(x * x)

arr = np.ones(10) + np.randn(10)
foo(arr)
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "locache",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "cache,disk",
    "author": "",
    "author_email": "John Gardner <gardner.john97@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/8c/3e/48ebd56de61d0b8758c421815e2435a3177ee4ffcefa0215cfb21b25473a/locache-3.0.0.tar.gz",
    "platform": null,
    "description": "# locache\n[![PyPI](https://img.shields.io/pypi/v/locache?style=for-the-badge)](https://pypi.org/project/locache/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/locache?color=green&label=Downloads&logo=Python&logoColor=white&style=for-the-badge)](https://pypistats.org/packages/locache)\n[![GitHub](https://img.shields.io/github/license/jla-gardner/local-cache?style=for-the-badge)](LICENCE.md)\n\nA small utility library for caching the results of deterministic and pure function calls to disk.\nThis ability is only intended for use on expensive function calls with simple arguments and keyword arguments.\nThe cache can optionally invalidate itself if changes to the function's source code are detetcted.\n\n## Installation\n\n`pip3 install locache`\n\n## Examples\n\nWhen used in normal python scripts, `@persist` is sufficient.\n\n`foo.py`:\n\n```python\nfrom locache import persist\n\n@persist\ndef my_func(x, num=3):\n    print(\"Hi from foo!\")\n    return x * num\n\nmy_func(1)        # prints \"Hi from foo!\", returns 3\nmy_func(2, num=2) # prints \"Hi from foo!\", returns 4\nmy_func(1)        # returns 3\nmy_func(2, num=2) # returns 4\n```\n\nRunning `foo.py` will lead to the creation of a `foo.cache/my_func/` directory, with files `x=1_num=3` and `x=2_num=2`.\n\n### Notebooks\n\nWhen using python notebooks, the `name` keyword is also required:\n\n`bar.ipynb`:\n\n```python\nfrom locache import persist\n\n@persist(name=\"notebook\")\ndef my_func(x, num=3):\n    print(\"Hi from foo!\")\n    return x * num\n\nmy_func(1)        # prints \"Hi from foo!\", returns 3\nmy_func(1)        # returns 3\n```\n\nRunning this cell will lead to the creation of a `notebook.cache/my_func/` directory in the same directory as the notebook.\n\n## Resetting the Cache\n\nBy default, the cache is invalidated and reset if source code changes to the function in question are dedicated.\nThis behaviour can be surpressed: `@persist(auto_invalidate=False)`\n\nResults for specific function calls can be removed from the cache by deleting the appropriate file.\n\nProgrammatic resetting of the cache is also possible:\n\n```python\nfrom locache import persist, reset_cache\n\n@persist\ndef foo(x):\n    print(\"Hi from foo!\")\n    return x ** 2\n\nfoo(1) # prints \"Hi from foo!\", returns 3\nfoo(1) # returns 3\n\nreset_cache(foo)\n\nfoo(1) # prints \"Hi from foo!\", returns 3\n```\n\nIn a notebook setting, the relevant name needs to also be passed:\n\n```python\n@persist(name=\"notebook\")\ndef foo(x):\n    return x**2\n\nfoo(1)\nreset_cache(foo, name=\"notebook\")\n```\n\n## Logging\n\nCache logging can optionally be enabled:\n\n```python\nfrom locache import verbose; verbose(True)\n```\n\n## Anti-Examples\n\nDon't pass large data structures to persisted functions:\n\n```python\nimport numpy as np\nfrom locache import persist\n\n@persist # don't do this!\ndef foo(x):\n    return np.sum(x * x)\n\narr = np.ones(10) + np.randn(10)\nfoo(arr)\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Cache expensive function calls to disk.",
    "version": "3.0.0",
    "split_keywords": [
        "cache",
        "disk"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "96745de9757511c4548975d47bb017962c013998826deaa20c6cb6eaac69fe86",
                "md5": "dfef6b37c7813ae04e9b772921ab14f0",
                "sha256": "48a4388cbc679d003c20f6374ee98fc48b2ffdfb2c15c8323be8d4a2f684574d"
            },
            "downloads": -1,
            "filename": "locache-3.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dfef6b37c7813ae04e9b772921ab14f0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 4782,
            "upload_time": "2023-04-21T16:11:25",
            "upload_time_iso_8601": "2023-04-21T16:11:25.965142Z",
            "url": "https://files.pythonhosted.org/packages/96/74/5de9757511c4548975d47bb017962c013998826deaa20c6cb6eaac69fe86/locache-3.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8c3e48ebd56de61d0b8758c421815e2435a3177ee4ffcefa0215cfb21b25473a",
                "md5": "e9b060b0330e26cf1c32cba8fde3490b",
                "sha256": "ca3a9b24f6e3fc980e2288b82593e42098ad665c631f9ac4e6efca82d5bcd4c5"
            },
            "downloads": -1,
            "filename": "locache-3.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "e9b060b0330e26cf1c32cba8fde3490b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 5338,
            "upload_time": "2023-04-21T16:11:29",
            "upload_time_iso_8601": "2023-04-21T16:11:29.657980Z",
            "url": "https://files.pythonhosted.org/packages/8c/3e/48ebd56de61d0b8758c421815e2435a3177ee4ffcefa0215cfb21b25473a/locache-3.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-21 16:11:29",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "locache"
}
        
Elapsed time: 0.06039s