Pamhyr2/src/tools.py

156 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
import time
import traceback
from colorama import Fore
from colorama import Back
from colorama import Style
from functools import (
reduce, partial, wraps
)
##########
# TIMERS #
##########
_timers = {}
_calls = {}
def reset_timers():
global _timers
global _calls
_timers = {}
_calls = {}
def display_timers():
global _timers
global _calls
fmax = max(
map(
lambda f: len(f.__qualname__) + len(f.__module__),
_timers
)
)
head = " +--"
head += f"{Style.BRIGHT}{Fore.BLUE}Timers{Style.RESET_ALL}"
for t in range(fmax + 26):
head += "-"
head += "+"
print(head)
lst = sorted(
map(
lambda f: (f, _timers[f], _calls[f]),
_timers
),
key = lambda f: f[1],
reverse = True
)
for func, time, calls in lst:
name = (f"{Fore.BLUE}{func.__module__}{Style.RESET_ALL}" +
f".{Style.BRIGHT}{Fore.GREEN}{func.__qualname__:<{fmax - len(func.__module__)}}{Style.RESET_ALL}")
print(f" | {name} | {time:>10.6f} sec | {calls:>5} calls |")
tail = " +--"
for t in range(fmax + 32):
tail += "-"
tail += "+"
print(tail)
def timer(func):
"""Function wrapper to register function runtime"""
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
value = None
try:
value = func(*args, **kwargs)
except Exception as e:
print(f"[{Fore.RED}ERROR{Style.RESET_ALL}]" +
f"[{func.__module__}.{Fore.GREEN}{func.__qualname__}{Style.RESET_ALL}]: " +
f"{Fore.RED}{e}{Style.RESET_ALL}")
traceback.print_exc()
end_time = time.perf_counter()
run_time = end_time - start_time
_timers[func] += run_time
_calls[func] += 1
return value
_timers[func] = 0
_calls[func] = 0
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}: {value}"
print(r)
return value
return wrapper
################
# OTHERS TOOLS #
################
@timer
def flatten(lst):
"""Flatten list of list
Args:
lst: A list of list
Returns:
returns a list of element
"""
if not lst:
return []
return reduce(list.__add__, lst)
def old_pamhyr_date_to_timestamp(date:str):
v = date.split(":")
if len(v) != 4:
return 0
m = [
(24 * 60 * 60), # Day to sec
(60 * 60), # Hour to sec
60, # Minute to sec
1 # Sec
]
ts = reduce(
lambda acc, x: acc + x,
map(
lambda v, m: int(v) * int(m),
v, m
)
)
return ts