Skip to main content

Easily debug class attribute changes.

Project description

Easily debug and trace attribute changes in your Python classes

pip install attr-tracers

PyPI version

Usage

To use this module, simply import trace from it, and decorate your chosen class:

from tracers import trace

@trace("test")
class MyClass:
   def __init__(self) -> None:
       self.test = 0
   
   ...

You can set custom setter and getter methods for your trace, practically allowing you to subcribe to read/write events of an attribute.

from typing import Any
from tracers import trace

def handle_get(obj: object) -> Any:
    """Get variable"""
    
    # Note the namespace-based reference to `attr`
    print(f"Trying to get {attr} from {obj}")
    return getattr(obj, "_" + attr)
    
def handle_set(obj: object, new: Any) -> None:
    """Set variable"""
    
    print(f"Setting {attr} for {obj} from {getattr(obj, attr)} -> {new}")
    setattr(obj, "_" + attr, new)
    
attr = "test"

@trace(attr, getter=handle_get, setter=handle_set)
class MyClass:
   def __init__(self) -> None:
       self.test = 0

You can also optionally output traces into a file non-interactively using the output argument.

Please note that these methods do NOT get a reference to attr. This is a quirk of properties, but you can work around it using namespace-based referencing, like above.

Inner workings

This module under the hood is just an easy way to assign properties to a class.

When decorated with trace("attr"), a class has its attribute attr replaced with a property. The default setter and getter of this property access the "_attr" attribute, as the property takes up "attr".

Essentially, what it becomes is this:

class MyClass:
    @property
    def attr(self) -> Any:
        return self._attr
        
    @attr.setter
    def attr(self, new: str) -> None:
        # Display current trace
        # Take input, quit or dismiss if chosen
        
        self._attr = new

Why use this over manually defining properties?

While defining the properties manually is doable, this module comes in really handy for debugging.

Imagine having a pretty complex hierarchy of class-inheritance and objects all relating to eachother (like it happens in pytermgui). It often happens that there is a certain attribute that isn't quite behaving how it is supposed to. While you could define these properties yourself, being able to import trace and plop it on top of a class is really efficient for debugging.

It also looks much nicer!

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

attr_tracers-1.1.0.tar.gz (230.0 kB view details)

Uploaded Source

Built Distribution

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

attr_tracers-1.1.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: attr_tracers-1.1.0.tar.gz
  • Upload date:
  • Size: 230.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for attr_tracers-1.1.0.tar.gz
Algorithm Hash digest
SHA256 43de7a0a2e91708508e8430e94e829ca0119fff69c1fc0e1b6841248b76f4f56
MD5 3f679294a4c03f05f4c593fbc84cc34c
BLAKE2b-256 f0feb8c12392431f6f5ffda86c0f4e3d6e620d99e3f0afb1cdd539efe3c99c1b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: attr_tracers-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for attr_tracers-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0298865a418eca271122040c841f3317e8ee577327423fa7fcd78f1468243a3f
MD5 a116150585c6f491ad71baa131363333
BLAKE2b-256 5b90b420441eb3c31b4a5b992d1681ffe7a23451d8325494c8d08639c87fee63

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