Skip to content

Instantly share code, notes, and snippets.

@costajob
Last active March 29, 2018 09:43
Show Gist options
  • Save costajob/9561ea1bc86136ad8527519eaff64883 to your computer and use it in GitHub Desktop.
Save costajob/9561ea1bc86136ad8527519eaff64883 to your computer and use it in GitHub Desktop.
Playing with python decorators
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