Skip to content

Commit

Permalink
Making timed decorator compatible with classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ancestor-mithril committed Nov 30, 2024
1 parent 239aa28 commit 1f17c4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
11 changes: 11 additions & 0 deletions tests/test_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ def wait(self, x):
key = next(iter(out.keys()))
self.assertIn(ClassA.__name__, key)

def test_class_name(self):
out = {}

class ClassA:
def __call__(self, x):
sleep(x)

timed(out=out)(ClassA())(0.1)
key = next(iter(out.keys()))
self.assertTrue(key.endswith("<locals>.ClassA"))

def test_return_time(self):
seconds = 0.1

Expand Down
16 changes: 11 additions & 5 deletions timed_decorator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,18 @@ def update_timing_dict(out: dict, key: str, elapsed: int, own_time: int):
def get_fn_name(fn):
if hasattr(fn, "name"): # torch.jit.ScriptFunction uses name instead of __name__
return fn.name
return fn.__name__
if hasattr(fn, "__name__"):
return fn.__name__
if hasattr(fn, "__class__"):
return fn.__class__.__name__
return type(fn).__name__


def get_fn_qualname(fn):
if hasattr(
fn, "qualified_name"
): # torch.jit.ScriptFunction uses qualified_name instead of __qualname__
if hasattr(fn, "qualified_name"): # torch.jit.ScriptFunction uses qualified_name instead of __qualname__
return fn.qualified_name
return fn.__qualname__
if hasattr(fn, "__qualname__"):
return fn.__qualname__
if hasattr(fn, "__class__"):
return fn.__class__.__qualname__
return type(fn).__qualname__

0 comments on commit 1f17c4e

Please sign in to comment.