Skip to main content

landuo (cachedproperty)

just a property but lazier...

Simply put, cachedproperty = property + cached_property.

Version: 1.0.0

Reasons for Creating This

  1. In-built property decorator allows you to manage your instances' attributes but at a cost of repeated computation for each read of the attribute.
  2. cached_property built by many [1], allows you to cache your instances' attributes but at a cost of losing the ability to implement setter 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

More on 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

More on Usage

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'.

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.

Release files for landuo 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for landuo 1.0.0
File Size Uploaded
landuo-1.0.0.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for landuo 1.0.0
File Interpreter ABI Platform
landuo-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.4 kB

Release files / landuo-1.0.0.tar.gz

Download URL landuo-1.0.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
12f1c4c79794dc07c6eaad486a3ed061681dceae4a8a2a65a057738dbcdacdf9
BLAKE2b-256 checksum
How to use checksums
4cece0a6004524da36e2707e51e78c958333c150c2c038e3815fb07d4c2f1883
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / landuo-1.0.0-py3-none-any.whl

Download URL landuo-1.0.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b628501e4e18633459a2644775101555354cbb9dfd49c93ce6b974712cac7e4c
BLAKE2b-256 checksum
How to use checksums
8062d083f05e82dea5d1463c5a3bc515d016110e227094d40342db806c5200fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page