Skip to content

Instantly share code, notes, and snippets.

@ibrezm1
Created April 24, 2022 03:14
Show Gist options
  • Select an option

  • Save ibrezm1/edcde40fc689e31f9f862289aee57340 to your computer and use it in GitHub Desktop.

Select an option

Save ibrezm1/edcde40fc689e31f9f862289aee57340 to your computer and use it in GitHub Desktop.
Check trace for a function execution wrapper decorator
# 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