Skip to main content

Lightweight utilities for cached properties with selective reset and pickle-safe classes.

Project description

xpropcache

Lightweight utilities for cached properties with selective reset and pickle-safe classes.

  • Extends functools.cached_property with flags
  • Class decorator for automatic registration
  • Selective cache invalidation
  • Automatic cleaning of cache values and marked attributes for pickling

The xpropcache decorator is @PropCache.cached_property | @PropCache.cached_property(flags)

these properties are referred as xprops below

Installation

  • Pure Python, no external dependencies except standard library
  • Python 3.10+ required
pip install xpropcache --upgrade

Quickstart

from xpropcache import PropCache, F__PickleIgnore__

FLAGS_DB = 0x01

@PropCache
class User:
    
    uid: int
    
    # ignore during pickling
    _session: Session | F__PickleIgnore__

    def __init__(self, uid, loader, session=None):
        self.uid = uid
        self._loader = loader
        self._session = session

    @PropCache.cached_property
    def profile(self):
        return self._loader.load_profile(self.uid)

    @PropCache.cached_property(FLAGS_DB)
    def settings(self):
        return self._loader.load_settings(self.uid)

u = User(42, loader)
if not u.profile:  # computed and cached
    new_user(u)
    PropCache.cp_purge(u)  # invalidates all xprops
elif u.settings.is_premium:  # computed and cached
    ...
    PropCache.cp_reset_by_flag(u, FLAGS_DB)  # only invalidates settings

Concepts

  • @PropCache.cached_property | @PropCache.cached_property(flags):
    • Like functools.cached_property, with additional optional flags (int) for grouping.
    • Cached values are ignored during pickling.
  • @PropCache:
    • Decorator for classes. Registers xprops, extends __getstate__, and adds __cp_purge__.
  • F__PickleIgnore__:
    • Marker type for type annotations. Attributes annotated with this type (directly or in union) are removed during pickling (in addition to cached xprops).

API (short)

  • PropCache.cp_purge(inst): Reset all cached xprops of the instance.
  • PropCache.cp_reset_by_flag(inst, flags): Reset cached xprops of the instance whose flag bits apply (xprop.flags & flags).
  • PropCache.pickle_purge(inst, inst.__dict__): Removes cached xprops and attributes marked with F_PickleIgnore__ from __dict__
  • Classes automatically receive:
    • __getstate__ with the pickle-purge logic
    • __cp_purge__: alias to PropCache.cp_purge(self)

Notes

  • Inheritance: xprops and F_PickleIgnore__ flags are merged via the MRO.

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

xpropcache-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

xpropcache-0.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file xpropcache-0.1.0.tar.gz.

File metadata

  • Download URL: xpropcache-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for xpropcache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 09b5390ddbed3551bc4a19227b6716398a681130ad41dbe9464b732880618fe9
MD5 2b59e3fcd98c282956dd974f96f0bd48
BLAKE2b-256 512654ef3fd895750a1fed24855b1ff9cec1947b9fea7bd26381ba7fe81e6093

See more details on using hashes here.

File details

Details for the file xpropcache-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: xpropcache-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for xpropcache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21fc068d5bd799418b3ddf367b929ec797e07104cddc40b3415042ace6871c0d
MD5 8ea863fff67042d581772aed7475b300
BLAKE2b-256 2819effcca3d79565bd98359568726fd551eb3a66c472facfe9f6543324b682c

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