Skip to main content

Decorators for caching instance methods.

Project description

cachegrab

Package version License

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 named self._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 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 protected_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.3.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cachegrab-0.2.3-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file cachegrab-0.2.3.tar.gz.

File metadata

  • Download URL: cachegrab-0.2.3.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cachegrab-0.2.3.tar.gz
Algorithm Hash digest
SHA256 be097d29dad9db626c97553de6959d70a0aa2d121597ec301e05795fffc7d901
MD5 40ed3023f93fef2fccf1209982d2b7da
BLAKE2b-256 bac88776d78e4260007b1ef5746ce9f8ad903d8770847ce35c61385df3d6bf2b

See more details on using hashes here.

File details

Details for the file cachegrab-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: cachegrab-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cachegrab-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 95cf7bf5b6dbfc6e9ee8de85148dce09849ebc13d68897ef4a7f0e9bea98a3c8
MD5 8cc32f647ee5cdc838001b59b59e9266
BLAKE2b-256 52165c5551138f12bfd35850d5e8ea45772ff1d14e1cf3aa047702b898571259

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page