mirror of https://gitlab.com/pamhyr/pamhyr2
tools: Add trace wrapper, and some terminal color for tools display.
parent
c3954208a6
commit
6fcd0b83af
31
src/tools.py
31
src/tools.py
|
|
@ -1,6 +1,11 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from colorama import Fore
|
||||||
|
from colorama import Back
|
||||||
|
from colorama import Style
|
||||||
|
|
||||||
from functools import (
|
from functools import (
|
||||||
reduce, partial, wraps
|
reduce, partial, wraps
|
||||||
)
|
)
|
||||||
|
|
@ -23,7 +28,7 @@ def display_timers():
|
||||||
global _timers
|
global _timers
|
||||||
global _calls
|
global _calls
|
||||||
|
|
||||||
print(" +---------------------------------------------------------Timers--+")
|
print(f" +--{Fore.BLUE}Timers{Style.RESET_ALL}---------------------------------------------------------+")
|
||||||
|
|
||||||
lst = sorted(
|
lst = sorted(
|
||||||
map(
|
map(
|
||||||
|
|
@ -35,7 +40,7 @@ def display_timers():
|
||||||
)
|
)
|
||||||
|
|
||||||
for func, time, calls in lst:
|
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(" +-----------------------------------------------------------------+")
|
print(" +-----------------------------------------------------------------+")
|
||||||
|
|
||||||
|
|
@ -65,6 +70,28 @@ def timer(func):
|
||||||
|
|
||||||
return wrapper
|
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 #
|
# OTHERS TOOLS #
|
||||||
################
|
################
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue