cachedproperty = property + cached_property
Project description
landuo (cachedproperty)
just a property but lazier...
Simply put, cachedproperty
= property
+ cached_property
.
Version: 1.0.0
Reasons for Creating This
- In-built
property
decorator allows you to manage your instances' attributes but at a cost of repeated computation for each read of the attribute. cached_property
built by many [1], allows you to cache your instances' attributes but at a cost of losing the ability to implementsetter
methods of them.
cachedproperty
(used interchangably with cached property
or its variants) solves this problem by caching of the values of your instances' attributes AND implementing setter
methods for each of them.
Objectives
Objective | Description | Status |
---|---|---|
Backward compatibility | It should be 'easy' (ideally, by adding a one-liner) to existing project to incorporate caching in property. | |
Caching | Property must be cached. It should allow the use of another 'cache' beyond the instance's __dict__ attribute for instances whose __dict__ attribute is a non-mutable mapping. Read more. |
Kinda? |
Setter method is available | Cached property should allow for the implementation of .setter(self, value) method. |
|
Deleter method is available | Cached property should allow for the implementation of .deleter(self) method. |
|
Async compatibility | Cached property should be thread-safe. pydanny's cached_property allows for this. |
No plans to do that in near future. |
Properties should be inherited as it is. | An example would be best here, if self.name returns self._name for a superclass, then for the subclass self.name should still return self._name , UNLESS wilfully overridden, e.g. self.name returns f'Hi, I'm {super().name} , the subclass' managed attribute name prepends 'Hi, I'm ' . |
Installation
Using pip
With pip
installed, you can use the following command to install landuo
.
python -m pip install landuo
Check that landuo
is installed by importing it in a python console.
Example:
from landuo import property
>>> property
<class 'landuo._lpusedict.BaseMutableCachedProperty'> # Output
How to Use
For Existing Project
If you have an existing project which already uses the python's inbuilt property
,
- add
landuo
to your dependencies, - add the following declaration into the import declarations in your modules that use the python's inbuilt
property
.
from landuo import property
#... other import statements
That's it! All your original property
should now have caching. By default, the cache used is the instance's attribute __dict__
, read more at APIs: cachedproperty.
Glossary
Managed Attribute(s) - The set of attribute(s) of a class which is managed by the cachedproperty
/ property
object. To 'manage' means that the 'read' and/or 'write' capabilities of the attribute is decided by another python object.
Private Variable(s) - Python has no strictly 'private' variable. In this case, the 'private' variable refers to the attribute of a class that is managed by another python object.
Example:
class Person:
def __init__(self, name):
self._name = name
# self._name is referred as the 'private' variable
@property # `property` manages `self.name` and its underlying private variable is `self._name`
def name(self):
return self._name
References
[1] Here is a list of implementations for 'cached_property
'.
- functools'
cached_property
- pydanny's
cached_property
- penguinolog's
cached_property
- werkzeug's
cached_property
- bottles'
cached_property
- bottles'
lazy_attribute
- sad2project's
LazyProperty
Why the Name landuo
?
懒惰 (Hàn yǔ Pīn yīn: lănduò) means lazy in Chinese, which describes the cachedproperty
class of this package.
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.