Created
March 6, 2021 09:15
-
-
Save cgravier/8e9c1903ada68cddf065dc36f657432d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import time | |
from functools import wraps | |
logger = logging.getLogger(__name__) | |
# Misc logger setup so a debug log statement gets printed on stdout. | |
logger.setLevel("DEBUG") | |
handler = logging.StreamHandler() | |
log_format = "%(asctime)s %(levelname)s -- %(message)s" | |
formatter = logging.Formatter(log_format) | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
def timed(func): | |
"""This decorator prints the execution time for the decorated function.""" | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
start = time.time() | |
result = func(*args, **kwargs) | |
end = time.time() | |
logger.debug("{} ran in {}s".format(func.__name__, round(end - start, 2))) | |
return result | |
return wrapper | |
@timed | |
def some_function_timed(): | |
print("Hello world") | |
if __name__ == "__main__": | |
slow_function() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment