From 433e1d0159a364904107f2faa9688456379fff91 Mon Sep 17 00:00:00 2001 From: Stefan Maschek Date: Thu, 11 Jan 2024 13:19:45 +0100 Subject: [PATCH 1/7] [TAQO-PAM-Eviden] TAQ-169 stop_watch time decorator --- src/utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/utils.py b/src/utils.py index 2a41a40d..db920837 100644 --- a/src/utils.py +++ b/src/utils.py @@ -202,3 +202,23 @@ def end_time_measurement(start: float) -> float: """ end = time.perf_counter() return round((end - start) * 1000, 3) + + +def quark_stop_watch(func): + """usage as decorator to measure time,eg: + ``` + @quark_stop_watch + def run(input_data,....): + return processed_data + ``` + results in valid: + ``` + processed_data, time_to_process = run(input,.....) + ``` + """ + def wrapper(*args, **kwargs): + start = start_time_measurement() + return_value = func(*args, **kwargs) + duration = end_time_measurement(start) + return return_value, duration + return wrapper From 6e52edc8c4a9e2523f5bc8b5278df6a5caa677f5 Mon Sep 17 00:00:00 2001 From: Stefan Maschek Date: Thu, 11 Jan 2024 13:46:31 +0100 Subject: [PATCH 2/7] TAQ-169 adding position option --- src/utils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utils.py b/src/utils.py index db920837..e270ad51 100644 --- a/src/utils.py +++ b/src/utils.py @@ -217,8 +217,13 @@ def run(input_data,....): ``` """ def wrapper(*args, **kwargs): + pos = kwargs.pop("time_pos", None) start = start_time_measurement() return_value = func(*args, **kwargs) duration = end_time_measurement(start) + if pos != None and isinstance(return_value, tuple): + l = list(return_value) + l[pos] = duration + return tuple(l) return return_value, duration return wrapper From 9399ed29cf364d33f3578739f0f8a7f83279595d Mon Sep 17 00:00:00 2001 From: Stefan Maschek Date: Thu, 11 Jan 2024 15:33:19 +0100 Subject: [PATCH 3/7] Automatically add metric --- src/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils.py b/src/utils.py index e270ad51..11aab117 100644 --- a/src/utils.py +++ b/src/utils.py @@ -221,6 +221,8 @@ def wrapper(*args, **kwargs): start = start_time_measurement() return_value = func(*args, **kwargs) duration = end_time_measurement(start) + if len(args) and hasattr(args[0],"metrics"): + args[0].metrics.add_metric(f"time_to_process_{func.__name__}", duration) if pos != None and isinstance(return_value, tuple): l = list(return_value) l[pos] = duration From 153a0fca3526584f6fb1acd3df1983b2ffbd7981 Mon Sep 17 00:00:00 2001 From: Stefan Maschek Date: Fri, 12 Jan 2024 10:03:18 +0100 Subject: [PATCH 4/7] remove auto-metric and refine docstring --- src/utils.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils.py b/src/utils.py index 11aab117..ffe17b27 100644 --- a/src/utils.py +++ b/src/utils.py @@ -205,7 +205,8 @@ def end_time_measurement(start: float) -> float: def quark_stop_watch(func): - """usage as decorator to measure time,eg: + """ + Usage as decorator to measure time, eg: ``` @quark_stop_watch def run(input_data,....): @@ -215,14 +216,15 @@ def run(input_data,....): ``` processed_data, time_to_process = run(input,.....) ``` + if the return value of the function is of type tuple, + the optial additional keyword `time_pos` specifies a position in the returned tuple + that is to be replaced by the measured time. """ def wrapper(*args, **kwargs): pos = kwargs.pop("time_pos", None) start = start_time_measurement() return_value = func(*args, **kwargs) duration = end_time_measurement(start) - if len(args) and hasattr(args[0],"metrics"): - args[0].metrics.add_metric(f"time_to_process_{func.__name__}", duration) if pos != None and isinstance(return_value, tuple): l = list(return_value) l[pos] = duration From 0e5bc07d48cf30168105b6d1fc9bc5c7218c36b5 Mon Sep 17 00:00:00 2001 From: Juergen Schwitalla Date: Fri, 12 Jan 2024 12:30:06 +0100 Subject: [PATCH 5/7] minor improvements --- src/utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils.py b/src/utils.py index ffe17b27..d2f423da 100644 --- a/src/utils.py +++ b/src/utils.py @@ -209,23 +209,23 @@ def quark_stop_watch(func): Usage as decorator to measure time, eg: ``` @quark_stop_watch - def run(input_data,....): + def run(input_data,...): return processed_data ``` results in valid: ``` - processed_data, time_to_process = run(input,.....) + processed_data, time_to_process = run(input,...) ``` - if the return value of the function is of type tuple, - the optial additional keyword `time_pos` specifies a position in the returned tuple - that is to be replaced by the measured time. + If the return value of the function is of type tuple, + the optional keyword `time_pos` can be used to specify the position at which the + measured time is to be inserted in the returned tuple. """ def wrapper(*args, **kwargs): pos = kwargs.pop("time_pos", None) start = start_time_measurement() return_value = func(*args, **kwargs) duration = end_time_measurement(start) - if pos != None and isinstance(return_value, tuple): + if pos is not None and isinstance(return_value, tuple): l = list(return_value) l[pos] = duration return tuple(l) From 6dcca811a950cf1696aa55633aa8b41422019944 Mon Sep 17 00:00:00 2001 From: Juergen Schwitalla Date: Tue, 16 Jan 2024 17:18:24 +0100 Subject: [PATCH 6/7] amended quark_stop_watch --- src/utils.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/utils.py b/src/utils.py index d2f423da..6368d678 100644 --- a/src/utils.py +++ b/src/utils.py @@ -204,11 +204,11 @@ def end_time_measurement(start: float) -> float: return round((end - start) * 1000, 3) -def quark_stop_watch(func): +def quark_stop_watch(position: int = None): """ Usage as decorator to measure time, eg: ``` - @quark_stop_watch + @quark_stop_watch() def run(input_data,...): return processed_data ``` @@ -216,18 +216,22 @@ def run(input_data,...): ``` processed_data, time_to_process = run(input,...) ``` - If the return value of the function is of type tuple, - the optional keyword `time_pos` can be used to specify the position at which the + If the return value of the decorated function is of type tuple, + the optional parameter `position` can be used to specify the position at which the measured time is to be inserted in the returned tuple. - """ - def wrapper(*args, **kwargs): - pos = kwargs.pop("time_pos", None) - start = start_time_measurement() - return_value = func(*args, **kwargs) - duration = end_time_measurement(start) - if pos is not None and isinstance(return_value, tuple): - l = list(return_value) - l[pos] = duration - return tuple(l) - return return_value, duration - return wrapper + + :param position: the position at which to insert the time + :type position: int + """ + def wrap(func): + def wrapper(*args, **kwargs): + start = start_time_measurement() + return_value = func(*args, **kwargs) + duration = end_time_measurement(start) + if position is not None and isinstance(return_value, tuple): + rv = list(return_value) + rv.insert(position, duration) + return tuple(rv) + return return_value, duration + return wrapper + return wrap From b4a4e5a0a0ed0da58aee180bddf6e811cc052fb7 Mon Sep 17 00:00:00 2001 From: Juergen Schwitalla Date: Tue, 30 Jan 2024 09:38:09 +0100 Subject: [PATCH 7/7] quark_stop_watch renamed to stop_watch and comments amended. --- src/utils.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/utils.py b/src/utils.py index 6368d678..111f4990 100644 --- a/src/utils.py +++ b/src/utils.py @@ -18,7 +18,7 @@ import subprocess import sys import time -from typing import Union +from typing import Union, Callable import inquirer @@ -204,11 +204,11 @@ def end_time_measurement(start: float) -> float: return round((end - start) * 1000, 3) -def quark_stop_watch(position: int = None): +def stop_watch(position: int = None) -> Callable: """ Usage as decorator to measure time, eg: ``` - @quark_stop_watch() + @stop_watch() def run(input_data,...): return processed_data ``` @@ -218,10 +218,13 @@ def run(input_data,...): ``` If the return value of the decorated function is of type tuple, the optional parameter `position` can be used to specify the position at which the - measured time is to be inserted in the returned tuple. + measured time is to be inserted in the return tuple. - :param position: the position at which to insert the time + :param position: The position at which the measured time gets inserted in the return tuple. + If not specified the measured time will be appended to the original return value. :type position: int + :return: The wrapper function + :rtype: Callable """ def wrap(func): def wrapper(*args, **kwargs):