Last active
February 11, 2018 04:57
-
-
Save namoshizun/e66249604b4ba46f798329afe2f4aa99 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
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