Decorators for caching instance methods.
Project description
cachegrab
cachegrab
is a Python package that provides decorators for caching instance methods.
Installation
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 namedself._deep_cache
.protected_attribute
➔ Decorator that returns the protected attribute corresponding to the name of the decorated function. If the protected attribute does not exist, it sets the attribute to the return value of the decorated function.
Example Usage
Imports
from cachegrab import deep_cached_property, protected_attribute
from functools import cached_property
Decorate Instance Methods
Consider the example class Dog
below:
toys
➔ decorated withcached_property
because toys can be buried and are therefore mutable.is_good_boy
➔ decorated withdeep_cached_property
because his good boy status is never in question.tricks
➔ decorated withprotected_attribute
to prevent direct overwrites.
class Dog(object):
@cached_property
def toys(self):
return {'ball','bone'}
@deep_cached_property
def is_good_boy(self):
return True
@protected_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:
dog = Dog()
dog.bury_toys()
dog.good_boy = False
Let's look at the results:
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)
dog toys ➜ ? good boy? ➜ True _deep_cache ➜ {'is_good_boy': True} _tricks ➜ {'sit','shake'}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cachegrab-0.2.1.tar.gz
(3.4 kB
view details)
Built Distribution
File details
Details for the file cachegrab-0.2.1.tar.gz
.
File metadata
- Download URL: cachegrab-0.2.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4702723b73e9ed5a3f92d972deb7d97a48004ba815bb92da00311cf8d4214d34 |
|
MD5 | 4c774638dd449720ffe29f404008cc2f |
|
BLAKE2b-256 | 79350f655d86848688412776256849185446d5a1a54f0188d1071954b3f268ff |
File details
Details for the file cachegrab-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: cachegrab-0.2.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 023b87b4ba7a10a614235071f2910cc912e455253975c90cb0d5ce6a43e3cbfb |
|
MD5 | 8210232b764cad3d7a6571f81298fa8c |
|
BLAKE2b-256 | 1df885d44918e222f8bc1207671f8b13a73ae9402f8a4b748a02a088dffa6d35 |