Slack/Email/Sound alert when your code block finishes running.
Python 2/3
-
Using
pip:pip install codealert -
Manually: Download
python/codealert/folder, and import it in your Python project.
Check out python/CodeAlert.ipynb for working examples.
# Import after pip install codealert
from codealert import *
# Initialize
codealert = CodeAlert()
# Easy way to turn it on/off
codealert.set('on', True)
# Method chaining way to set attributes. Sound and email is enabled but Slack is not.
codealert.set('sound_enabled', True).set('email_enabled', True).set('emails', ['YOUR_EMAIL'])
List of attributes:
on:boolsound_enabled:boolor astrindicating the sound you want to be played, i.e. passing indonewill invoke a terminal sound saying done.email_enabled:boolemails:[]array of string emails to send toslack_enabled:boolslack_urls:[]array of slack urls to send to. Please obtain your slack urls from https://my.slack.com/services/new/incoming-webhook/logtext:strdefault log text that is sent to your email/Slack. Ifpingis being called with astrargument passed in, that will overridelogtext.printstatements in apingddecorated function will also overridelogtext.
# Start Code block
import time
time.sleep(3)
# End Code block
# One line ping here that will send you an email and emit a sound
codealert.ping('Finished sleeping for 3 seconds')
If you want to enable Slack, you'll have to reconfigure your codealert instance:
slack_urls = ['https://hooks.slack.com/services/T---/B---/---']
codealert.set('slack_enabled', True).set('slack_urls', slack_urls)
Then ping to send you a notification in Slack:
import time
time.sleep(1)
codealert.ping("CodeAlert pinging Slack after 1 second")
pingd(codealert, options) decorator that takes in two optional arguments: codealert instance and options which is a dictionary of attributes that applies only to this function. If codealert is not passed in, a new codealert instance will be initialised. If options is not passed in, options set in codealert will be used.
Note: options does not mutate codealert instance passed in.
custom_options = {'email_enabled': False, 'sound_enabled': 'done', 'slack_enabled': True}
# Pass in code alert and custom options
@pingd(codealert, custom_options)
def func(a, b, c):
import time
time.sleep(3)
# Test call function
func('a', 'b', 'c')
This will send you a Slack notification and play a sound after the end of the function execution.
Decorated functions also sends you the ping whenever an error happens in the function. Furthermore, all print statements in a function will be forwarded to you, along with the error invoked:
#
@pingd(codealert)
def func_error():
import time
time.sleep(3)
print('This code is before the error')
x = None + 2
print('This code does not print')
# Test call function
func_error()
The above function when called will send to your Slack the following message:
Error:
======
unsupported operand type(s) for +: 'NoneType' and 'int'
Print:
======
This code is before the error
- I have hosted a small server to handle email notifications. Hence if you are using email notifications, please do not spam my server, i.e. do not place a
ping()call inside aforloop. Violation of this will result in me banning your usage of the server for email notifications permanently.
- Please commit to a separate branch and submit a pull request.
- If you run your own node.js server, remember to change the
BASE_URLinCodeAlert.pyto your server's host and port. - If you want to run email alerts, you have to create a
credentials.txtin the root directory of your node.js app. The first line of the file will be your email address, and the second line being your email pass, i.e.
[email protected]
abc123