tools.py 1.62 KiB
# -*- coding: utf-8 -*-

import time
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

    print(" +---------------------------------------------------------Timers--+")

    lst = sorted(
        map(
            lambda f: (f, _timers[f], _calls[f]),
            _timers
        ),
        key = lambda f: f[1],
        reverse = True
    )

    for func, time, calls in lst:
        print(f" | {func:<32} | {time:>10.6f} sec | {calls:>5} calls |")

    print(" +-----------------------------------------------------------------+")

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"{e}")

        end_time = time.perf_counter()
        run_time = end_time - start_time

        if func.__qualname__ not in _timers:
            _timers[func.__qualname__] = 0
            _calls[func.__qualname__] = 0

        _timers[func.__qualname__] += run_time
        _calls[func.__qualname__] += 1

        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)