Skip to content

A tiny observer for the attributes of Python objects.

License

Notifications You must be signed in to change notification settings

furkanonder/objerve

Repository files navigation

objerve

A tiny observer for the attributes of Python objects.

Actions Status GitHub issues GitHub stars GitHub license Downloads

Installation

objerve can be installed by running pip install objerve

Example Usage

Let's say you have a class like that;

class M:
    qux = "blue"

    def __init__(self):
        self.bar = 55
        self.foo = 89
        self.baz = 121

To watch the changes, you need the add the @watch() as a class decorator. Within the arguments of the watch decorator you should pass in lists for the keyword arguments of the attributes you wish to watch.

from objerve import watch

@watch(set={"foo", "qux"}, get={"bar", "foo"}, delete={"baz"})
class M:
    qux = "blue"

    def __init__(self):
        self.bar = 55
        self.foo = 89
        self.baz = 121


m = M()
m.bar = 233


def abc():
    m.foo += 10


m.qux = "red"


def get_foo(m):
    m.bar


abc()
m.foo
del m.baz
get_foo(m)

Output:

Set | foo = 89
  File "/home/blue/objerve/examples/example.py", line 9, in __init__
    self.foo = 89

Set | qux = red
  File "/home/blue/objerve/examples/example.py", line 21, in <module>
    m.qux = "red"

Get | foo
  File "/home/blue/objerve/examples/example.py", line 18, in abc
    m.foo += 10

Set | foo = 99
  File "/home/blue/objerve/examples/example.py", line 18, in abc
    m.foo += 10

Get | foo
  File "/home/blue/objerve/examples/example.py", line 29, in <module>
    m.foo

Delete | baz
  File "/home/blue/objerve/examples/example.py", line 30, in <module>
    del m.baz

Get | bar
  File "/home/blue/objerve/examples/example.py", line 25, in get_foo
    m.bar