A Python decorator for logging attribute changes in classes
Project description
Selv
A Python decorator for logging attribute changes in class. Track every modification to your object's attributes with automatic logging and changelog.
Motivation
Tracking attribute changes helps me understand how a complex class behaves and speeds up debugging when issues occur. Having said this, I need a simple way to log all attribute changes without writing boilerplate code for each attribute.
Features
- Automatic attribute change tracking: Decorated class automatically log all attribute changes
- Container support: Tracks changes to dictionaries, lists, sets, and tuples, including nested modifications
- Flexible logging: Use good ol
printstatement or any custom logger function - View changelog: Query complete change history for any attribute
Installation
pip install selv
Or using mighty uv:
uv add selv
Usage
Tracking changes
from selv import selv
@selv
class Counter:
def __init__(self):
self.value = 0
def increment(self):
self.value += 1
def decrement(self):
self.value -= 1
# Create counter and use it
counter = Counter()
counter.increment()
counter.decrement()
# Changes are automatically logged:
# [Counter] value: 0 -> 1
# [Counter] value: 1 -> 0
View changelog
# View all changes
all_changes = counter.view_changelog()
print(all_changes)
# [
# {'time': datetime, 'attr': 'value', 'from': None, 'to': 0},
# {'time': datetime, 'attr': 'value', 'from': 0, 'to': 1},
# {'time': datetime, 'attr': 'value', 'from': 1, 'to': 0}
# ]
# View changes for specific attribute
value_changes = counter.view_changelog("value")
print(value_changes)
# [
# {'time': datetime, 'from': None, 'to': 0},
# {'time': datetime, 'from': 0, 'to': 1},
# {'time': datetime, 'from': 1, 'to': 0}
# ]
# View changes grouped by attribute
grouped = counter.view_changelog(format="attr")
# {
# 'value': [
# {'time': datetime, 'from': None, 'to': 0},
# {'time': datetime, 'from': 0, 'to': 1},
# {'time': datetime, 'from': 1, 'to': 0}
# ]
# }
Parameters
The @selv decorator has a few parameters to customize its behavior.
-
track_private(bool, default:True)- When
True: Tracks all attributes including those starting with_(private attributes) - When
False: Only tracks public attributes (those not starting with_)
- When
-
logger(Callable[[str], None], default:print)- Function to use for logging change messages (e.g.,
logging.info,logging.debug) - Can be any function that accepts a string argument
- Function to use for logging change messages (e.g.,
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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
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 selv-1.0.0.tar.gz.
File metadata
- Download URL: selv-1.0.0.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2676d254430a61cdc7a7cb3c2c2b98ed455d275a4033365267238389dca58e91
|
|
| MD5 |
246be59d52b373b702e58b9f1d10938b
|
|
| BLAKE2b-256 |
5322d9ca190625973262a884200b87e8e48bff8902b20d8c79a84bff479101fd
|
File details
Details for the file selv-1.0.0-py3-none-any.whl.
File metadata
- Download URL: selv-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dee13106f120f13f5a653b1c190920d599dfbc6d70ea0211704da0201f54f514
|
|
| MD5 |
1f92263ca63adcd10e54ac36654e78a0
|
|
| BLAKE2b-256 |
8de19b888d2038079c48bc3c27ddeecd0045aa878f070dd783663f924b7cfdde
|