# cachegrab
<div>
[](https://pypi.org/project/cachegrab/)
[](https://github.com/zteinck/cachegrab/blob/master/LICENSE)
</div>
`cachegrab` is a Python package that provides decorators for caching instance methods.
## Installation
```sh
pip install cachegrab
```
## Main Features
- `deep_cached_property` ➔ Inspired by functools.cached_property, this decorator provides a deep copy of cached return values, ensuring they remain immutable after their initial access. The original return values are preserved in a dictionary named `self._deep_cache`.
- `cached_attribute` ➔ this decorator returns an internal attribute with the same name as the original, prefixed with an underscore.
## Example Usage
### Imports
```python
from cachegrab import deep_cached_property, cached_attribute
from functools import cached_property
```
### Decorate Instance Methods
Consider the example class `Dog` below:
- `toys` ➔ decorated with `cached_property` because toys can be buried and are therefore mutable.
- `is_good_boy` ➔ decorated with `deep_cached_property` because his good boy status is never in question.
- `tricks` ➔ decorated with `cached_attribute` to prevent direct overwrites.
```python
class Dog(object):
@cached_property
def toys(self):
return {'ball','bone'}
@deep_cached_property
def is_good_boy(self):
return True
@cached_attribute
def tricks(self):
return {'sit','shake'}
def bury_toys(self):
while self.toys:
self.toys.pop()
```
We will attempt to modify both cached properties:
```python
dog = Dog()
dog.bury_toys()
dog.good_boy = False
```
Let's look at the results:
```python
print('dog toys ➜', ', '.join(dog.toys) if dog.toys else '?')
print('good boy? ➜', dog.is_good_boy)
print('_deep_cache ➜', dog._deep_cache)
dog.tricks # access tricks property
print('_tricks ➜', dog._tricks)
```
<pre>
dog toys ➜ ?
good boy? ➜ True
_deep_cache ➜ {'is_good_boy': True}
_tricks ➜ {'sit','shake'}
</pre>
Raw data
{
"_id": null,
"home_page": "https://github.com/zteinck/cachegrab",
"name": "cachegrab",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Zachary Einck",
"author_email": "zacharyeinck@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/ec/e2/fd8f7a8d95d0f972363e8a8182261175718564d47128ffdfef87f7b35914/cachegrab-0.1.2.tar.gz",
"platform": null,
"description": "# cachegrab\n\n<div>\n\n[](https://pypi.org/project/cachegrab/)\n[](https://github.com/zteinck/cachegrab/blob/master/LICENSE)\n\n</div>\n\n\n`cachegrab` is a Python package that provides decorators for caching instance methods.\n\n\n## Installation\n```sh\npip install cachegrab\n```\n\n\n## Main Features\n- `deep_cached_property` \u2794 Inspired by functools.cached_property, this decorator provides a deep copy of cached return values, ensuring they remain immutable after their initial access. The original return values are preserved in a dictionary named `self._deep_cache`.\n- `cached_attribute` \u2794 this decorator returns an internal attribute with the same name as the original, prefixed with an underscore.\n\n\n## Example Usage\n\n### Imports\n```python\nfrom cachegrab import deep_cached_property, cached_attribute\nfrom functools import cached_property\n```\n\n### Decorate Instance Methods\nConsider the example class `Dog` below:\n- `toys` \u2794 decorated with `cached_property` because toys can be buried and are therefore mutable.\n- `is_good_boy` \u2794 decorated with `deep_cached_property` because his good boy status is never in question.\n- `tricks` \u2794 decorated with `cached_attribute` to prevent direct overwrites.\n\n```python\nclass Dog(object):\n\n @cached_property\n def toys(self):\n return {'ball','bone'}\n\n @deep_cached_property\n def is_good_boy(self):\n return True\n\n @cached_attribute\n def tricks(self):\n return {'sit','shake'}\n\n def bury_toys(self):\n while self.toys:\n self.toys.pop()\n```\n\nWe will attempt to modify both cached properties:\n```python\ndog = Dog()\ndog.bury_toys()\ndog.good_boy = False\n```\n\nLet's look at the results:\n```python\nprint('dog toys \u279c', ', '.join(dog.toys) if dog.toys else '?')\nprint('good boy? \u279c', dog.is_good_boy)\nprint('_deep_cache \u279c', dog._deep_cache)\n\ndog.tricks # access tricks property\nprint('_tricks \u279c', dog._tricks)\n```\n\n<pre>\ndog toys \u279c ?\ngood boy? \u279c True\n_deep_cache \u279c {'is_good_boy': True}\n_tricks \u279c {'sit','shake'}\n</pre>\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Decorators for caching instance methods.",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/zteinck/cachegrab",
"Repository": "https://github.com/zteinck/cachegrab"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3c3b6c2ac26ac2d8a33eba782ac7b75e37f2ec6a19273f7c9173eae814e35c7a",
"md5": "df0d756547ae8c8bafbed0077a75c8f0",
"sha256": "d7d0316c071bdf29ba3e80c250d3aa67d57c29bbdbdd7d3d0d3e2b815f9b4702"
},
"downloads": -1,
"filename": "cachegrab-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "df0d756547ae8c8bafbed0077a75c8f0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 3695,
"upload_time": "2024-07-03T04:45:41",
"upload_time_iso_8601": "2024-07-03T04:45:41.574734Z",
"url": "https://files.pythonhosted.org/packages/3c/3b/6c2ac26ac2d8a33eba782ac7b75e37f2ec6a19273f7c9173eae814e35c7a/cachegrab-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ece2fd8f7a8d95d0f972363e8a8182261175718564d47128ffdfef87f7b35914",
"md5": "da99b53fa723866801e797177cab51ed",
"sha256": "8953967bdf987424fd295c2c925f996db4736abe7a1fd820141e880480207f95"
},
"downloads": -1,
"filename": "cachegrab-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "da99b53fa723866801e797177cab51ed",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 3168,
"upload_time": "2024-07-03T04:45:43",
"upload_time_iso_8601": "2024-07-03T04:45:43.139857Z",
"url": "https://files.pythonhosted.org/packages/ec/e2/fd8f7a8d95d0f972363e8a8182261175718564d47128ffdfef87f7b35914/cachegrab-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-03 04:45:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "zteinck",
"github_project": "cachegrab",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "cachegrab"
}