Skip to content

Instantly share code, notes, and snippets.

@cedeber
Created December 5, 2018 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cedeber/76f12015fa4175d4c4cd474546d9c95e to your computer and use it in GitHub Desktop.
Save cedeber/76f12015fa4175d4c4cd474546d9c95e to your computer and use it in GitHub Desktop.
Python decorator
import time
from functools import wraps
# Use this function only if you need decorator parameters
def howmuchtime(limit=None):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
total = time.time() - start
if limit is not None and total >= limit:
print(total)
return res
return wrapper
return decorator
@howmuchtime(0)
def hello():
print("hello world")
hello()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment