dirtyflags is a simple Python decorator that tracks when and which instance attributes have changed.
Project description
dirtyflags
dirtyflags is a simple Python decorator that tracks when and which instance attributes have changed.
>>> from dirtyflags import dirtyflag
>>> @dirtyflag
>>> class ChangingObject():
>>> def __init__(self, attr1: int, attr2: str = "Default Value"):
>>> self.attr1 = attr1
>>> self.attr2 = attr2
>>>
>>> def __str__(self):
>>> return (f"attr1 = {self.attr1}, attr2='{self.attr2}'")
>>> # create an instance of the class
instance_default = ChangingObject(attr1=1)
>>> print(f"instance_default is: {instance_default}")
instance_default is: attr1 = 1, attr2='Default Value'
>>> # dirtyflags tracks whether a change has occurred - in this case it has not
>>> print(f"Has this instance changed = {instance_default.is_dirty()}")
Has this instance changed = False
>>> # now change the value of an attribute
>>> instance_default.attr1 = 234
>>> # now dirty flag indicates the class has changed - and tells you what has changed
>>> print(f"Has this instance changed = {instance_default.is_dirty()}")
>>> print(f"The attribute(s) that have changed: {instance_default.dirty_attrs()}")
Has this instance changed = True
The attribute(s) that have changed: {'attr1'}
>>> # dirtyflags even tracks changes when using __setter__
>>> instance_default.__setattr__('attr2', 'changed the default')
>>> print(f"Has this instance changed = {instance_default.is_dirty()}")
>>> print(f"The attribute(s) that have changed: {instance_default.dirty_attrs()}")
Has this instance changed = True
The attribute(s) that have changed: {'attr2', 'attr1'}
Installing dirtyflags
pip install dirtyflags
dirtyflags officially supports Python 3.8+
Supported Features and Best Practices
- Sinmply use the
@dirtyflagdecorator - Supports attributes of any datatype, built-in or custom
- Works with Python dataclasses
- Nested classes should have the '@dirtyflag' decorator applied as well
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
dirtyflags-1.5.0.tar.gz
(4.3 kB
view details)
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 dirtyflags-1.5.0.tar.gz.
File metadata
- Download URL: dirtyflags-1.5.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db3316cf2510af8f3aad4f116620449092f9485ac6e24cc879affb85fa786001
|
|
| MD5 |
74ff534547cc4c6adcc82857fc7ec819
|
|
| BLAKE2b-256 |
ff57e3269763e6130a9fc7ff5de5bec67b1217b4ddb3330c2373797735d64ffd
|
File details
Details for the file dirtyflags-1.5.0-py3-none-any.whl.
File metadata
- Download URL: dirtyflags-1.5.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fa7faa01e958843a4824b6cdf540cb9afa7e89e4e4617d173819e3b0bb176ba
|
|
| MD5 |
5dda571cb5618abca34d5a4919823f56
|
|
| BLAKE2b-256 |
aed327f1ebe5626939e59b64d9c76e9c389f9af643eba0422f286365fbfe03a9
|