These are common function decorators I use when writing, testing, or debugging Python code. If you have an incredibly complex codebase, I recommend using something like a python IDE.
Import the python module and use the @
syntax to decorate functions.
If I have a model that takes a long time to train, I send a notification to my email when the function is done running like this:
import decorators as dec
def send_email(username, pw, api_key, msg):
sess = authenticate(username, pw, api_key)
sess.send_email(msg)
@dec.post_exec(lambda : send_email('username', 'password', 'api_key', 'done'))
def train():
# Takes a long time to train
# Will send email when done
If don't like arbitrary "thunk" patterns, you can do something like this:
import decorators as dec
@dec.post_exec(send_email, 'username', 'password', 'api_key', 'done')
def train():
# Takes a long time to train
# Will send email when done
If we need to see the outputs of a function, instead of writing something like this:
def test_func(index, str1, str2):
# do something
out1 = test_func(0, "abc", "def")
out2 = test_func(2, "cds", "asda")
out3 = test_func(100, "foo", "bar")
print(out1)
print(out2)
print(out3)
>> foo
>> bar
>> baz
we can do this:
import decorators as dec
@dec.print_output
def test_func(index, str1, str2):
# do something
out1 = test_func(0, "abc", "def")
out2 = test_func(2, "cds", "asda")
out3 = test_func(100, "foo", "bar")
>> foo
>> bar
>> baz