Transparent Object Change Observers for Python objects and dicts
Project description
obj-observe
obj-observe is a simple, zero-dependency Python library for observing changes on object attributes and dictionary keys.
Installation
pip install obj-observe
Example
from obj_observe import observe
class Player:
def __init__(self):
self.hp = 100
p = Player()
@observe(p, "hp")
def on_hp_change(old, new):
print(f"HP changed: {old} -> {new}")
p.hp = 50
Core Functions
observe(obj, attr, callback=None)
This is the main function for attaching an observer to an attribute or key.
-
As a Decorator:
from obj_observe import observe class Player: def __init__(self, hp): self.hp = hp p = Player(100) @observe(p, 'hp') def on_hp_change(old_value, new_value): print(f"HP changed from {old_value} to {new_value}") p.hp = 150 # Prints: HP changed from 100 to 150
-
With a Callback Function:
def hp_watcher(old, new): print(f"HP was {old}, now it is {new}") observe(p, 'hp', hp_watcher) p.hp = 50 # Prints: HP was 150, now it is 50
-
Observing Dictionary Keys:
When observing a dictionary,
observereturns anObservableDict. You must use this new object to track changes.my_dict = {'status': 'idle'} def on_status_change(old, new): print(f"Status changed from '{old}' to '{new}'") my_dict = observe(my_dict, 'status', on_status_change) my_dict['status'] = 'running' # Prints: Status changed from 'idle' to 'running'
remove_observers(obj, attr=None)
Detaches observers from an object.
-
Remove Observers from a Specific Attribute:
remove_observers(p, 'hp') p.hp = 0 # No notification will be sent
-
Remove All Observers from an Object:
remove_observers(p)
Running Tests
To run the included test suite:
python test.py
License
MIT
Project details
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 obj_observe-0.1.1.tar.gz.
File metadata
- Download URL: obj_observe-0.1.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d957a98b65ac4b624086ab8624429e6ef236c82c0d4849df241eea50ab8be6d
|
|
| MD5 |
bf2a8bd9b6ca61093382c5e93a44d637
|
|
| BLAKE2b-256 |
061d3c535de2f4c41a7fc571d0f4d79e7a8ff4410c2cf18154d68904561a41d3
|
File details
Details for the file obj_observe-0.1.1-py3-none-any.whl.
File metadata
- Download URL: obj_observe-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b618736eea1bd7ff45e80a0fa1436f291c0fa18b45af5a590545780b2601477
|
|
| MD5 |
56554ac6f13c748163c398e1754c25d6
|
|
| BLAKE2b-256 |
d89b7b9ea619515715a1dbdde14cfdcc301fbd5e30817be1539fb4c3657ee9ee
|