Last active
March 29, 2018 09:43
-
-
Save costajob/9561ea1bc86136ad8527519eaff64883 to your computer and use it in GitHub Desktop.
Playing with python decorators
This file contains hidden or 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 datetime import datetime | |
from time import sleep | |
from sys import argv | |
def measure(func): | |
def decorator(n): | |
start = datetime.now() | |
res = func(n) | |
end = datetime.now() | |
print('%s completed in %.2f seconds' % (func.__name__, (end - start).total_seconds())) | |
return res | |
return decorator | |
@measure | |
def lazy(n): | |
print('i am going to take a snap!') | |
sleep(float(n)) | |
if __name__ == '__main__': | |
n = argv[1] if len(argv) > 1 else 1 | |
lazy(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment