Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialized variable watcher #80

Open
ionelmc opened this issue Mar 24, 2020 · 1 comment
Open

Specialized variable watcher #80

ionelmc opened this issue Mar 24, 2020 · 1 comment

Comments

@ionelmc
Copy link
Owner

ionelmc commented Mar 24, 2020

There's VarsPrinter and VarsSnooper but they can't handle well this usecase: find out when a specific field or variable changes, perhaps even to a certain value.

Some ideas for a predicate:

Watcher(attr="foobar", type="MyClass", value=lambda value: value.startswith("stuff"))
Watcher(name="self", value=lambda value: value.field.startswith("stuff"))
Watcher(name="self", attr="field", type="MyClass", value=lambda value: value.startswith("stuff"))
Watcher(name=lambda name: name in ["self", "instance", "obj"], attr="field", type="MyClass", value=lambda value: value.startswith("stuff"))

Usecases like this one https://stackoverflow.com/a/54536941/23658 could be supported like this:

inst = ...

trace(Watcher(object=inst, attr="field", type="MyClass", value=lambda value: value.startswith("stuff")))

inst.field = "stuff123"

All the options and alternative names:

  • name, variable
  • attr, field
  • type, klass
  • value
  • object, instance, inst

There is a question of disallowing name and object at the same time. Perhaps allow it but make it mean "if object is object and variable name is name".

Another problem is if type is a string or the actual type.

@ionelmc
Copy link
Owner Author

ionelmc commented Mar 30, 2020

Idea 1 - implement an ID tracker (don't poke around in objects):

  • name - state[event.code, name] = id(event.locals[name])
  • name + attr - state[event.code, name, attr] = id(event.locals[name].__dict__[attr])
  • klass + attr - for name in event.locals: if type(event.locals[name]).__name__ == klass: state[klass, attr] = id(event.locals[name].__dict__[attr])
  • klass - for name in event.locals: state[event.code, name] = id(event.locals[name])
  • inst + attr - state[id(inst), attr] = id(inst.__dict__[attr])
  • value - doesn't affect storage

Trigger condition: state[<criteria>] != new_id and (value(new_value) if callable(value) else value == new_value). Need to note that value == new_value is a potentially dangerous side-effect incurring operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant