cornsnake.decorators

Decorators for your Python code.

Documentation

 1"""
 2Decorators for your Python code.
 3
 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/decorators.html)
 5"""
 6
 7from typing import Any, Callable
 8from . import util_log
 9from . import util_time
10
11logger = util_log.getLogger(__name__)
12
13
14def timer(original_function: Callable) -> Any:
15    """
16    Adds a timer to your function - prints out the time taken, to the console and writes to log.
17    """
18
19    def wrapper(*args: Any, **kwargs: Any) -> Any:
20        start = util_time.start_timer()
21
22        # Call the original function
23        result = original_function(*args, **kwargs)
24
25        elapsed = util_time.end_timer(start)
26        message = f"[time taken: {util_time.describe_elapsed_seconds(elapsed)}]"
27        print(message)
28        logger.info(message)
29
30        return result
31
32    return wrapper
logger = <Logger cornsnake.decorators (DEBUG)>
def timer(original_function: Callable) -> Any:
15def timer(original_function: Callable) -> Any:
16    """
17    Adds a timer to your function - prints out the time taken, to the console and writes to log.
18    """
19
20    def wrapper(*args: Any, **kwargs: Any) -> Any:
21        start = util_time.start_timer()
22
23        # Call the original function
24        result = original_function(*args, **kwargs)
25
26        elapsed = util_time.end_timer(start)
27        message = f"[time taken: {util_time.describe_elapsed_seconds(elapsed)}]"
28        print(message)
29        logger.info(message)
30
31        return result
32
33    return wrapper

Adds a timer to your function - prints out the time taken, to the console and writes to log.