Created
April 24, 2022 03:14
-
-
Save ibrezm1/edcde40fc689e31f9f862289aee57340 to your computer and use it in GitHub Desktop.
Check trace for a function execution wrapper decorator
This file contains hidden or 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
| # Decorators | |
| import sys, traceback | |
| import time | |
| # Create a function with function argument | |
| def timerit(func): | |
| #Create a wrapper to that function with args | |
| def wrapper(*arg): | |
| # function preporcessing | |
| t=time.time() | |
| #Call function with args | |
| res = func(*arg) | |
| #Function post processing | |
| print(time.time()-t) | |
| # return the function response | |
| return res | |
| # Return wrapper | |
| return wrapper | |
| # Wrap the function on the wrapper | |
| @timerit | |
| def myfunc(cat): | |
| for lin in traceback.format_stack(): | |
| if "<ipython-input-" in lin: | |
| print(lin) | |
| return cat | |
| # Calling this will first call | |
| # timerit --> Wrapper -- > myfunc --> return myfunc --> return wratter -- return timer it | |
| myfunc('jod') | |
| #traceback.format_stack() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment