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.
- Like
@PropCache:- Decorator for classes. Registers xprops, extends
__getstate__, and adds__cp_purge__.
- Decorator for classes. Registers xprops, extends
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 withF_PickleIgnore__from__dict__- Classes automatically receive:
__getstate__with the pickle-purge logic__cp_purge__: alias toPropCache.cp_purge(self)
Notes
- Inheritance: xprops and
F_PickleIgnore__flags are merged via the MRO.
Changelog
0.2
- cache entries are now also accessible via subtypes
...
@PropCache
class User:
...
class PremiumUser(User):
...
pu = PremiumUser(42, loader)
PropCache.cp_purge(pu)
0.1
Initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xpropcache-0.2.0.tar.gz.
File metadata
- Download URL: xpropcache-0.2.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83424874cf7dfaf1e674a4517cb8e388e8d8d00a26d76e0defe57151efab1b8d
|
|
| MD5 |
49aefe70061d046da56d4e3b11aa5310
|
|
| BLAKE2b-256 |
e0e45fc091fcff50d89bac9c944a8552ccdda00ccbec906f93a18d0a89d63ddc
|
File details
Details for the file xpropcache-0.2.0-py3-none-any.whl.
File metadata
- Download URL: xpropcache-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0198b30203a238ef67f9429525dcc26b745b1c86753897b3c9d0a359febf843
|
|
| MD5 |
24c0e1b764357cee5312c65d65477667
|
|
| BLAKE2b-256 |
66812f20da30b4e4881b85e0cd2d88d668557deadb1cfb23de7de7dd6ce721b7
|