Extend the tracing abilities and application tools #62
thorwhalen
started this conversation in
enhancement
Replies: 1 comment
-
|
Some code to work on: from i2.footprints import MethodTrace
def is_not_dunder(attr_name: str):
return not attr_name.startswith('__') or not attr_name.endswith('__')
class NewMethodTrace(MethodTrace):
def __init__(self, include=None):
self.include = include
self.trace = []
def __repr__(self):
trace_str = ', '.join(map(lambda x: f'{x}', self.trace))
return f'<{type(self).__name__} with .trace = {trace_str}>'
def _should_be_traced(self, attr_name: str):
if self.include:
return attr_name in self.include
else:
return is_not_dunder(attr_name)
def __getattr__(self, attr_name: str):
if self._should_be_traced(attr_name):
def traced_operation(*args, **kwargs):
self.trace.append((attr_name, args, kwargs))
return self
return traced_operation
else:
try:
self.__dict__[attr_name]
except KeyError as e:
raise AttributeError(*e.args)
t = NewMethodTrace()
t.foo(1, bar=2)
t.only_pos(3, 4)
t.only_kw(this=3, that=4)
t + 2
t.trace
assert t.trace == [
('foo', (1,), {'bar': 2}),
('only_pos', (3, 4), {}),
('only_kw', (), {'this': 3, 'that': 4}),
('__add__', 2)
]Problems:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
We have
MethodTracenow, see issuesMethodTrace: To transform operators and other methods into differed commands
and
MethodTrace: Refactor to extract method factory and name_and_signature list.
But there's a lot more to do.
First, we'd like to have the ability to control what is traced -- right now, it's only a fixed set of dunders, but would like to be able to have include/exclude possibilities. We probably want
MethodTrace, by default, to trace the said dunders, but also all other methods, dynamically (unless specified differently by the include/exclude mechanism).Further, in order to trace attribute uses like
obj.attr = valwe need to have a__setattr__or (probably warranted at this point) use descriptors.Beta Was this translation helpful? Give feedback.
All reactions