Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
context manager to time code and send text message when done
Context manager or function to send text messages to your phone when a
process is done.
Edit the global variables. You might be able to find your phone e-mail
address here:
with SendText("long running process"):
import smtplib
from email.MIMEText import MIMEText
import time
import getpass
# global configuration
email_from = ''.join(['', 'AT', 'gmail', '.com']).replace('AT','@')
email_to = ''.join(['###########', 'AT', '']).replace('AT','@')
subject = 'My Project'
from_whom = 'My Project'
def send_text(process, pwd, elapsed=None):
message = "Done running %s" % process
if elapsed:
message += "\nElapsed: %s" % elapsed
subject = message
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = from_whom
msg['To'] = email_to
server = smtplib.SMTP('', 587)
server.login(email_from, pwd)
server.sendmail(email_from, email_to, msg.as_string())
class SendText(object):
def __init__(self, process):
self.process = process
def __enter__(self):
self.pwd = getpass.getpass("Gmail password: ")
self.tic = time.time()
def __exit__(self, exc_type, exc_value, traceback):
toc = time.time()
elapsed = round(toc-self.tic,2)
print "Elapsed: %f seconds" % elapsed
try: # catch an error so result still passes through
send_text(self.process, self.pwd, elapsed)
print "Sending text failed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.