Skip to content

Commit

Permalink
refactor: clean up code in implementations of measure()
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdevpl committed Mar 11, 2024
1 parent ba35155 commit 9ece093
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions timing/group.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Handling of group of timings."""

import collections.abc
import contextlib
import datetime
import functools
import types
import typing as t

Expand Down Expand Up @@ -98,19 +98,20 @@ def ...
return self._measure_decorator(function, name)

@contextlib.contextmanager
def _measure_context(self, name: str) -> t.Generator:
"""Return contextlib.ContextDecorator that can be used as decorator or context."""
def _measure_context(self, name: str) -> t.Generator[Timing, None, None]:
"""Return the just-started timer as context variable."""
timer = self.start(name)
yield timer
timer.stop()

def _measure_decorator(self, function: types.FunctionType,
name: t.Optional[str] = None) -> collections.abc.Callable:
def _measure_decorator(self, function: types.FunctionType, name: str | None = None):
"""Return the original function wrapped in a timing context."""
if name is None:
name = function.__name__

@functools.wraps(function)
def function_wrapper(*args, **kwargs):
with self.measure(name):
with self._measure_context(name):
return function(*args, **kwargs)
return function_wrapper

Expand Down

0 comments on commit 9ece093

Please sign in to comment.