Skip to main content

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 value modifications inside container
  • Flexible logging: Use good ol print statement or any custom logger function
  • View changelog: Query complete change history for any attribute
  • Exclude specific attributes: Optionally exclude specific attributes from tracking

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")
print(grouped)
# {
#   '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.

  1. 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 _)
  2. 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
  3. exclude (List[str], default: None)

    • List of attribute names to exclude from tracking
    • Useful for exclude sensitive data or unimportant attributes

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

selv-1.1.0.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

selv-1.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file selv-1.1.0.tar.gz.

File metadata

  • Download URL: selv-1.1.0.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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

Hashes for selv-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ee78939607cf1e73686633f5d74660e3f07df898cee23f802abcbb612cc3f91e
MD5 bd4d5f07e5bb895c51d2c74e4607ef03
BLAKE2b-256 43c289844a44274819cb2912f671779b7bd6c970fb51cd128ecdd0210133bcaa

See more details on using hashes here.

File details

Details for the file selv-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: selv-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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

Hashes for selv-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a2969338f08f7340753a3b1cfa2600ec844e6e99466dcca1db902b0845b405ee
MD5 a71005628722d986b1de9deb2923268f
BLAKE2b-256 e9661ca1219ba5e5cd68a8d298f35c83c8e6dcc2f98bcfa50f51d2d60a74b7da

See more details on using hashes here.

Supported by

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