https://github.com/dssg/dickens
Common Python descriptors
https://github.com/dssg/dickens
programming-utility
Last synced: 6 months ago
JSON representation
Common Python descriptors
- Host: GitHub
- URL: https://github.com/dssg/dickens
- Owner: dssg
- Created: 2017-11-09T00:23:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-04-19T19:57:01.000Z (over 3 years ago)
- Last Synced: 2025-10-02T22:51:30.318Z (10 months ago)
- Topics: programming-utility
- Language: Python
- Size: 8.79 KB
- Stars: 1
- Watchers: 8
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
=======
Dickens
=======
Additional Python decorators implementing the descriptor interface.
Use cases
=========
Like the built-in decorator, ``property``, these classes are initialized by and wrap a function, generally within the context of a class, in order to modify its behavior.
cached property
---------------
This decorator functions much like a read-only ``property``, with the difference that, upon access, it records its result in the instance's object data dictionary, which reference takes precedence in look-ups, thereby replacing itself for that object::
from descriptors import cachedproperty
@cachedproperty
def circumference(self):
return 2 * math.pi * self.radius
class property
--------------
A read-only ``property`` for class methods::
from descriptors import classproperty
@classproperty
def badpi(cls):
return 22 / 7
cached class property
---------------------
A class ``property``, which caches its result in the data dictionary of the class from which it was invoked, (under another name, so as not to interfere with inheritance of the property)::
from descriptors import cachedclassproperty
@cachedclassproperty
def badpi(cls):
return 22 / 7
class-only method
-----------------
A class method that **cannot** be accessed as an instance method::
from descriptors import classonlymethod
@classonlymethod
def circumference(cls, radius):
return 2 * cls.pi * radius
The class-only method *may* be overshadowed by instance data set under the same name.
Otherwise, instance access raises ``AttributeError`` (and such that it is forwarded to and *may* be handled by the instance's ``__getattr__``).
Installation
============
Dickens is a Python distribution, which may be installed via ``easy_install`` or ``pip``, *e.g.*::
pip install Dickens
...or, from source::
python setup.py install