Skip to content

Instantly share code, notes, and snippets.

@Proteusiq
Created September 17, 2018 10:16
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 Proteusiq/df9ded302ed576b8bada5bd42e6247fa to your computer and use it in GitHub Desktop.
Save Proteusiq/df9ded302ed576b8bada5bd42e6247fa to your computer and use it in GitHub Desktop.
ptimer
from datetime import datetime
class MyImportError(Exception):
'''Error when humanfriendly module not installed'''
def __init__(self,original_exception,msg):
super(MyImportError, self).__init__(f'\n\n{original_exception} {msg}')
self.original_exception = original_exception
msg = ('\nInstall using conda or pip:'
'\n>pip install humanfriendly'
'\n>conda install -c conda-forge humanfriendly')
try:
import humanfriendly
except ModuleNotFoundError as m:
raise MyImportError(m,msg)
def ptimer(function):
'''
Outputs function execution time in humanized form
Use:
@ptimer
def myfunction(x):
return x**2
'''
def wrapper(*args,**kwargs):
'''
returns functions output if any
'''
u = datetime.now() # Start time
f_return = function(*args,**kwargs)
v = datetime.now() # End time
delta = v - u
print('Execution Time: {}'.format(
humanfriendly.format_timespan(delta.seconds))
)
return f_return #Return function contents
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment