import functools
import time


def method_timeit(method=None, name=None):
    """
    Measure execution time of provided method
    Records time into times dictionary

    :param method: method to time
    :type method: Callable
    :param name: key argument for the times dictionary
    :type name: str
    """

    def decorator(method, name_dec=None):
        @functools.wraps(method)
        def wrapper(self, *args, **kw):
            # necessary bc can't reassign external name
            name_dec = name
            if not name_dec:
                name_dec = method.__name__
            ts = time.time()
            result = method(self, *args, **kw)
            te = time.time()
            self.times[name_dec] += (te - ts)
            return result

        return wrapper

    if callable(method):
        return decorator(method, name_dec=name)
    return decorator