Skip to main content

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 @dirtyflag decorator
  • Supports attributes of any datatype, built-in or custom
  • Works with Python dataclasses
  • Nested classes should have the '@dirtyflag' decorator applied as well

linting: pylint

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.0.7.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

dirtyflags-1.0.7-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file dirtyflags-1.0.7.tar.gz.

File metadata

  • Download URL: dirtyflags-1.0.7.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for dirtyflags-1.0.7.tar.gz
Algorithm Hash digest
SHA256 a0df85e7e4f4b6fc8559f6e3d4300c16636ca850ae6f6db3d8217ab141233677
MD5 341b3f6689eb3decf3d6af2b237d1814
BLAKE2b-256 01a0ac350a56917260262d16fef0213eac1a692ab747ca53d034ebdc65753aa4

See more details on using hashes here.

File details

Details for the file dirtyflags-1.0.7-py3-none-any.whl.

File metadata

  • Download URL: dirtyflags-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for dirtyflags-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0c5f9eedf4e6ba8017e5eb1f45cafc765d739e442d878722f5c25085bd34f64b
MD5 f33687441ef9653829ede429b4993b54
BLAKE2b-256 f6546d33d1d330f50f62e9c97b871dc3a8354494ac80cca56d6730af9516fc88

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page