Last active
May 29, 2020 21:49
-
-
Save richiefrost/b5f5159ea2011bdfc33a138a1e940cca 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
from time import time | |
def log_time(func): | |
"""Logs the time it took for func to execute""" | |
def wrapper(*args, **kwargs): | |
start = time() | |
val = func(*args, **kwargs) | |
end = time() | |
duration = end - start | |
print(f'{func.__name__} took {duration} seconds to run') | |
return val | |
return wrapper | |
# Example usage | |
@log_time | |
def get_data(db, query): | |
"""Gets data from a SQL-based database""" | |
data = db.get(query) | |
return data | |
if __name__ == '__main__': | |
# Decorated function will print 'get_data took X seconds to run' | |
db = SQLDB() | |
query = 'SELECT * FROM foo' | |
data = get_data(db, query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment