Observable class attributes in pure Python
Project description
ObservAttr: Observable Attributes
Pure Python implementation of observable class instance attributes (or a sort of reactive variables). It uses a descriptor that returns a proxy object which can be observed.
The proxy object defines all public methods of the underlying object, and notifies subscribers after method calls. Callables can be subscribed to the proxy object and are notified with the underlying object as argument.
The descriptor instantiates the proxy object after instantiating an instance of the parent class. So it is not an observable class attribute, but an observable instance attribute. For immutable types (e.g. Number), the __set__
implementation copies the observers and returns a new proxy upon variable assignment. No change of type is allowed upon new descriptor variable assignment.
Usage
from observattr import ObservableAttribute
Declare as class attribute with no type specified:
class Example:
# This instantiates the descriptor for this class
observable = ObservableAttribute()
def __init__(self, anything, subscribed_function):
# This calls the __set__ method of the descriptor:
# - The descriptor instantiates 'hidden' proxy self._observable with 'anything' as the underlying object
# - type(anything) is now the fixed type of self.observable
self.observable = anything
self.observable.subscribe(subscribed_function)
Declare as class attribute with a fixed specified type:
class Example:
# This instantiates the descriptor for this class
observable = ObservableAttribute(list)
def __init__(self, value, subscribed_function):
# This calls the __get__ method of the descriptor:
# - The descriptor instantiates 'hidden' proxy self._observable with 'list()' as the underlying object
self.observable.append(value)
self.observable.subscribe(subscribed_function)
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
File details
Details for the file observattr-21.6.1.tar.gz
.
File metadata
- Download URL: observattr-21.6.1.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.25.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 562b0eda14300e9361a38a0772ecd04b22b53150efd3815473d015e413651fa8 |
|
MD5 | 0c3eb84307b6a249c04b547d66b742fd |
|
BLAKE2b-256 | 31ee5ed2d6c647e32f1f06bde0415b60449879750568965a46253171d575bb61 |
File details
Details for the file observattr-21.6.1-py2.py3-none-any.whl
.
File metadata
- Download URL: observattr-21.6.1-py2.py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.25.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32e3550b39a2f3b171d92f4ec92b98e35768f8106ad2a04df5e14cea70bf166a |
|
MD5 | 7b51b3471eb6fecf91b30a4c64bae098 |
|
BLAKE2b-256 | fec9d73f62dd66845800732e2688ddbcb88661be3a61a684d27a268580b196d9 |