Created
September 17, 2018 10:16
-
-
Save Proteusiq/df9ded302ed576b8bada5bd42e6247fa to your computer and use it in GitHub Desktop.
ptimer
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
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