From 44a70581b5492a86dc417abf4f3221a7d0a815a9 Mon Sep 17 00:00:00 2001 From: lukas leufen <l.leufen@fz-juelich.de> Date: Wed, 26 Feb 2020 11:53:04 +0100 Subject: [PATCH] name attribute in time tracking --- src/helpers.py | 5 +++-- test/test_helpers.py | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/helpers.py b/src/helpers.py index 8a50b0e7..ab679905 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -49,9 +49,10 @@ class TimeTracking(object): method. Duration can always be shown by printing the time tracking object or calling get_current_duration. """ - def __init__(self, start=True): + def __init__(self, start=True, name="undefined job"): self.start = None self.end = None + self._name = name if start: self._start() @@ -93,7 +94,7 @@ class TimeTracking(object): def __exit__(self, exc_type, exc_val, exc_tb): self.stop() - logging.info(f"undefined job finished after {self}") + logging.info(f"{self._name} finished after {self}") def prepare_host(create_new=True, sampling="daily"): diff --git a/test/test_helpers.py b/test/test_helpers.py index b807d2b8..07ec244e 100644 --- a/test/test_helpers.py +++ b/test/test_helpers.py @@ -117,6 +117,14 @@ class TestTimeTracking: expression = PyTestRegex(r"undefined job finished after \d+:\d+:\d+ \(hh:mm:ss\)") assert caplog.record_tuples[-1] == ('root', 20, expression) + def test_name_enter_exit(self, caplog): + caplog.set_level(logging.INFO) + with TimeTracking(name="my job") as t: + assert t.start is not None + assert t.end is None + expression = PyTestRegex(r"my job finished after \d+:\d+:\d+ \(hh:mm:ss\)") + assert caplog.record_tuples[-1] == ('root', 20, expression) + class TestPrepareHost: -- GitLab