contextcache


Namecontextcache JSON
Version 0.3.0 PyPI version JSON
download
home_page
Summary
upload_time2023-10-19 13:44:14
maintainer
docs_urlNone
authorYour Name
requires_python>=3.10,<4.0
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # contextcache

[![CI](https://github.com/Peter554/contextcache/actions/workflows/ci.yml/badge.svg)](https://github.com/Peter554/contextcache/actions/workflows/ci.yml)

Cache a python function *only in certain contexts*.

## Usage

Here's an example:

```sh
cat example.py     
```
        
```py
import contextcache

# Define a private CacheContextVar to store the cached values. Don't touch this CacheContextVar from anywhere else!
# You need to define a separate CacheContextVar for every function for which you want to enable caching.
# Use `None` as the default.
_double_cache = contextcache.CacheContextVar("double_cache", default=None)


# Use the `enable_caching` decorator to enable context caching for `double`.
@contextcache.enable_caching(_double_cache)
def double(n: int) -> int:
    print(f"Doubling {n}, working...")
    return n * 2


# Without caching.
print(f"Without caching")
print(double(1))
print(double(1))

# With caching.
with contextcache.use_caching(double):
    print(f"\nWith caching")
    print(double(1))
    print(double(1))

# Without caching, again.
print(f"\nWithout caching, again")
print(double(1))
print(double(1))
```

Here's the output:

```sh
python example.py
```

```
Without caching
Doubling 1, working...
2
Doubling 1, working...
2

With caching
Doubling 1, working...
2
2

Without caching, again
Doubling 1, working...
2
Doubling 1, working...
2
```

See the tests for further examples.

## Caveats

* Function arguments must be hashable.
            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "contextcache",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Your Name",
    "author_email": "you@example.com",
    "download_url": "https://files.pythonhosted.org/packages/55/33/4cfd58fc6df62e1b7640de2b5215c9505696e9266eae833a8170c5452fb0/contextcache-0.3.0.tar.gz",
    "platform": null,
    "description": "# contextcache\n\n[![CI](https://github.com/Peter554/contextcache/actions/workflows/ci.yml/badge.svg)](https://github.com/Peter554/contextcache/actions/workflows/ci.yml)\n\nCache a python function *only in certain contexts*.\n\n## Usage\n\nHere's an example:\n\n```sh\ncat example.py     \n```\n        \n```py\nimport contextcache\n\n# Define a private CacheContextVar to store the cached values. Don't touch this CacheContextVar from anywhere else!\n# You need to define a separate CacheContextVar for every function for which you want to enable caching.\n# Use `None` as the default.\n_double_cache = contextcache.CacheContextVar(\"double_cache\", default=None)\n\n\n# Use the `enable_caching` decorator to enable context caching for `double`.\n@contextcache.enable_caching(_double_cache)\ndef double(n: int) -> int:\n    print(f\"Doubling {n}, working...\")\n    return n * 2\n\n\n# Without caching.\nprint(f\"Without caching\")\nprint(double(1))\nprint(double(1))\n\n# With caching.\nwith contextcache.use_caching(double):\n    print(f\"\\nWith caching\")\n    print(double(1))\n    print(double(1))\n\n# Without caching, again.\nprint(f\"\\nWithout caching, again\")\nprint(double(1))\nprint(double(1))\n```\n\nHere's the output:\n\n```sh\npython example.py\n```\n\n```\nWithout caching\nDoubling 1, working...\n2\nDoubling 1, working...\n2\n\nWith caching\nDoubling 1, working...\n2\n2\n\nWithout caching, again\nDoubling 1, working...\n2\nDoubling 1, working...\n2\n```\n\nSee the tests for further examples.\n\n## Caveats\n\n* Function arguments must be hashable.",
    "bugtrack_url": null,
    "license": "",
    "summary": "",
    "version": "0.3.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6b8b89416d3191a186825e394d0ae90e928968848df203f3383bfaf9fa845415",
                "md5": "b879e4470b88202e348af3df56cf0f71",
                "sha256": "d0d91474eeee45606c84ea6a292b49277e63da807bb6360afd7e525d8cc4e363"
            },
            "downloads": -1,
            "filename": "contextcache-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b879e4470b88202e348af3df56cf0f71",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 2761,
            "upload_time": "2023-10-19T13:44:10",
            "upload_time_iso_8601": "2023-10-19T13:44:10.467215Z",
            "url": "https://files.pythonhosted.org/packages/6b/8b/89416d3191a186825e394d0ae90e928968848df203f3383bfaf9fa845415/contextcache-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "55334cfd58fc6df62e1b7640de2b5215c9505696e9266eae833a8170c5452fb0",
                "md5": "63509983ac7a015d87e617ef6c1a7617",
                "sha256": "ee04bf8ec0683c5a8d52df8097442328499d7c77c534c6f73167fe5acd63273a"
            },
            "downloads": -1,
            "filename": "contextcache-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "63509983ac7a015d87e617ef6c1a7617",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 2304,
            "upload_time": "2023-10-19T13:44:14",
            "upload_time_iso_8601": "2023-10-19T13:44:14.439120Z",
            "url": "https://files.pythonhosted.org/packages/55/33/4cfd58fc6df62e1b7640de2b5215c9505696e9266eae833a8170c5452fb0/contextcache-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-19 13:44:14",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "contextcache"
}
        
Elapsed time: 0.47120s