Commit 6fcd0b83 authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby
Browse files

tools: Add trace wrapper, and some terminal color for tools display.

Showing with 29 additions and 2 deletions
+29 -2
# -*- 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 #
################
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment