Skip to content

Instantly share code, notes, and snippets.

@namoshizun
Last active February 11, 2018 04:57
Show Gist options
  • Save namoshizun/e66249604b4ba46f798329afe2f4aa99 to your computer and use it in GitHub Desktop.
Save namoshizun/e66249604b4ba46f798329afe2f4aa99 to your computer and use it in GitHub Desktop.
import time
from functools import wraps
def timeit(comment=None):
"""
auto establish and close mongo connection
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
begintime = time.time()
ret = func(*args,**kwargs)
duration = (time.time() - begintime).total_seconds()/60
msg = '[{}] {}: duration (min) = {}'.format(begintime.strftime('%Y-%m-%d'), func.__name__, duration)
if comment is not None:
print '<{}>'.format(comment)
print msg
return ret
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment