ScriptNotifier

A Notifier class used to notify the user when programs crash or can be used to send update emails or text messages during the running of a script

ScriptNotifier Class

class pygeostat.scriptnotifier.scriptnotifier.ScriptNotifier(print_error=True, email=False, text=False, SMTP_Dict=None, TWILIO_Dict=None)

Notify the user of the status of your script. If error occurred, print error message. Can send email or text messages with script status or error if the correct dictionaries are initialized

Parameters:
  • print_error (bool) – print error to the current console
  • email (bool) – send status alerts or crash reports to email. Requires SMTP_Dict
  • text (bool) – send a text message to your phone using twilio account. Requires TWILIO_Dict
  • SMTP_Dict (dict) – A dictionary with the smtp settings required for sending an email from python
  • TWILIO_Dict (dict) – A dictionary with your TWILIO account information needed to send text messages to yourself using TWILIO

Examples

an example of the dictionaries needed and initializing the notifier class

>>> smtp_dict = {'from_addr': 'myless_secure_email@gmail.com',
...              'to_addr': 'my_email@gmail.com',
...              'login': 'myless_secure_email@gmail.com',
...              'password': 'Secr@tPa22word',
...              'smtpserver': 'smtp.gmail.com',
...              'SSL': True}
...
... twilio_dict = {'accountSID': '34characterSID',
...                'authToken': '32characterTOKEN',
...                'myNumber': '+17809995555',
...                'twilioNumber': '+15873334444',
...                'length_limit': 310}
...
... notifier = gs.ScriptNotifier(print_error=True, email=True, text=True
...                              SMTP_Dict=smtp_dict, TWILIO_Dict=twilio_dict)

once the class is initialized, then crash reports can be sent to all initialized outputs (print_error, email, text)

>>> if 'ERROR' in message:
...     notifier.crash_report(message)

or status updates can be embedded at points in your script and they will be sent to all initialized outputs (print_error, email, text)

>>> for idx in range(2000):
...     something = idx + something_else
... notifier.status_alert('finished first loop!')

or send just an email

>>> notifier.email_myself('finished first model', 'UPDATE')

or send just a text

>>> notifier.text_myself('finished 5th model', status='UPDATE')

Note

GMAIL: in order to send emails with a gmail account you have to switch the security settings to allow access from less secure apps

Note

TWILIO: you can sign up for a trial account with twilio for free. The trial account provides a free credit to send text messages with. You have to initialize a phonenumber to send them from and you lose this number when you don’t send any texts for 30 days. It seems like if that happens though you can just initialize a new number.

Note

Install TWILIO: pip install twilio

Code author: Tyler Acorn - February 20, 2017

Connect to the Email Server

ScriptNotifier.connect_email_server()

setup up the email server

Code author: Tyler Acorn - February 20, 2017

Connect to TWILIO texting client

ScriptNotifier.connect_twilio()

connect to the twilio client with the accountSID and authToken

Code author: Tyler Acorn - February 20, 2017

Send a Crash Report

ScriptNotifier.crash_report(errormsg)

Send an error message that the script crashed and initiate a sys.exit(). Will use whatever communications methods set during initialization of the class (console, text, email)

Parameters:errormsg (str) – the message you want sent to yourself.

Code author: Tyler Acorn - February 20, 2017

Send an Status Update Message

ScriptNotifier.status_alert(message, status='Update')

Send yourself an update of your script status. Will use whatever communication method(s) set during initialization of the class (console, text, email)

Parameters:
  • message (str) – the message you want sent to yourself.
  • status (str) – if included will be appended to the start of the text for example ‘Update’

Code author: Tyler Acorn - February 20, 2017

Send an Email Message

ScriptNotifier.email_myself(message, status=None, attachments=None, max_att_size=10485760)

send an email to yourself from python with the status of your script. The ability to attach files has been added. Note: there is a maximum total attachment size of 25MB that this function will allow you to attach.

Parameters:
  • message (str) – the message that you want to email yourself
  • status (str) – the status will be included in the subject of the email. for example ‘ERROR’ will provide the subject line ‘ScriptNotifier: ERROR’
  • attachments (list) – str or list of the file paths to attach to the email
  • max_att_size (int) – The maximum total size (bytes) of the attachments. This can’t be set above 25MB, but a lower limit can be set.

Code author: Tyler Acorn - February 20, 2017

Send a Text Message

ScriptNotifier.text_myself(message, status=None)

Text yourself from python using a TWILIO account

1. Set-up a trial account at twilio, some restrictions apply: limited number of messages phone numbers that are unused for 30 days are lost

  1. Get your twilio number to text from, your SID, and authToken
  2. pip install twilio
Parameters:
  • message (str) – the message you want texted to yourself
  • status (str) – if included will be appended to the start of the text for example ‘ERROR:’

Note

length_limit in dictionary is used to trim the message so that you don’t send a massive sized text to yourself. This allows you to send the first XXX number of characters of a crash report to yourself if you want.

Code author: Warren E. Black

Log Progress

pygeostat.scriptnotifier.utils.log_progress(sequence, every=None, size=None, name='Items')

A progress bar from https://github.com/alexanderkuk/log-progress. See the git repo for usage and pygeostat.scriptnotifier.utils.py:log_progress() for license

Parameters:sequence – The thing that is being iterated over

Example

>>> from pygeostat import log_progress
>>> from time import sleep
>>> for i in log_progress(range(200)):
...     sleep(0.1)
_images/log_progress.gif

Deprecate Function

pygeostat.scriptnotifier.deprecation.deprecated(since, message='', name='', alternative='', pending=False, obj_type=None, addendum='')

Decorator to mark a function or a class as deprecated.

Parameters:
  • since (str) – The release at which this API became deprecated. This is required.
  • message (str, optional) – Override the default deprecation message. The format specifier %(name)s may be used for the name of the object, and %(alternative)s may be used in the deprecation message to insert the name of an alternative to the deprecated object. %(obj_type)s may be used to insert a friendly name for the type of object being deprecated.
  • name (str, optional) –

    The name of the deprecated object; if not provided the name is automatically determined from the passed in object, though this is useful in the case of renamed functions, where the new function is just assigned to the name of the deprecated function. For example:

    def new_function():
        ...
    oldFunction = new_function
    
  • alternative (str, optional) – An alternative object that the user may use in place of the deprecated object. The deprecation warning will tell the user about this alternative if provided.
  • pending (bool, optional) – If True, uses a PendingDeprecationWarning instead of a DeprecationWarning.
  • addendum (str, optional) – Additional text appended directly to the final message.

Examples

Basic example:

@deprecated('1.4.0')
def the_function_to_deprecate():
    pass

Make Directory

pygeostat.scriptnotifier.filemanagement.mkdir(dirnames, verbose=False)

Make directory(s).

Parameters:
  • dirnames (str or list) – directory(s) to create
  • verbose (bool) – print to screen if an error is encountered, including if the dirnames already exists

Remove Directory

pygeostat.scriptnotifier.filemanagement.rmdir(dirnames, verbose=False)

Remove directory(s).

Parameters:
  • dirnames (str or list) – directory(s) to remove
  • verbose (bool) – print to screen if an error is encountered, including if the dirnames has already been removed

Remove File

pygeostat.scriptnotifier.filemanagement.rmfile(filenames, verbose=False)

Remove file(s).

Parameters:
  • filenames (str) – file to remove
  • verbose (bool) – print to screen if an error is encountered, including if the file has already been removed