From 6fcd0b83af27fd9e0ba73705473ba4549543d1a1 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr> Date: Thu, 20 Apr 2023 09:04:19 +0200 Subject: [PATCH] tools: Add trace wrapper, and some terminal color for tools display. --- src/tools.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/tools.py b/src/tools.py index 8d6b5faa..5d4e954b 100644 --- a/src/tools.py +++ b/src/tools.py @@ -1,6 +1,11 @@ # -*- coding: utf-8 -*- import time + +from colorama import Fore +from colorama import Back +from colorama import Style + from functools import ( reduce, partial, wraps ) @@ -23,7 +28,7 @@ def display_timers(): global _timers global _calls - print(" +---------------------------------------------------------Timers--+") + print(f" +--{Fore.BLUE}Timers{Style.RESET_ALL}---------------------------------------------------------+") lst = sorted( map( @@ -35,7 +40,7 @@ def display_timers(): ) for func, time, calls in lst: - print(f" | {func:<32} | {time:>10.6f} sec | {calls:>5} calls |") + print(f" | {Fore.GREEN}{func:<32}{Style.RESET_ALL} | {time:>10.6f} sec | {calls:>5} calls |") print(" +-----------------------------------------------------------------+") @@ -65,6 +70,28 @@ def timer(func): return wrapper +######### +# DEBUG # +######### + +def trace(func): + @wraps(func) + def wrapper(*args, **kwargs): + t = time.ctime() + head = f"[{Fore.BLUE}TRACE{Style.RESET_ALL}]" + c = f"{head}[{t}] Call {func.__module__}.{Fore.GREEN}{func.__qualname__}{Style.RESET_ALL}({args}, {kwargs})" + print(c) + + value = func(*args, **kwargs) + + t = time.ctime() + r = f"{head}[{t}] Return {func.__module__}.{Fore.GREEN}{func.__qualname__}{Style.RESET_ALL}" + print(r) + + return value + + return wrapper + ################ # OTHERS TOOLS # ################ -- GitLab