Financial Python

Studies in Finance and Python

Archive for the ‘Python’ Category

Python Gmail Script

leave a comment »

Click here to download:

emailscriptexample.py (1 KB)

Sorry, forgot to attach the code for the previous post. Here it is.

Mirror at NotesToSelf

Advertisements

Written by DK

June 13, 2009 at 8:42 pm

Posted in Python

Tagged with ,

Sending Gmail with Automator and Python

with one comment

I wanted to automate some reminder emails I send out every week (because I always forget to send them), and thought I’d give Automator a try. The experience wasn’t that great. It launches OS X’s Mail application and, for whatever reason, doesn’t send anything. The message just sits in the Outbox. I think it’s a known bug in Automator, but it was sort of a letdown for something as simple as auto-sending email.

So instead, I adapted a short python script that sends email via the smtplib and email modules, and used Automator to run the script via an iCal plug-in (File>Save As Plugin). Creating a plugin creates an Automator calendar in iCal where your scheduled script resides. All you have to do is set the timing. I know the command-line folks probably use cron to schedule scripts like this, but I like seeing the scheduled script on my calendar. I also like the script approach because I don’t have to launch any other applications (e.g., Mail). The script also works for those of you using google apps (just use your google apps email address).

Mirror at NotesToSelf

Written by DK

June 13, 2009 at 8:20 pm

Posted in Python

Tagged with , ,

Update to Google Apps Script

with one comment

The Google guys finally updated the python client to reflect the addition of Groups to Google Apps. Groups allow admins to set permissions on distribution lists (for example, only list owners can send email to the list, etc.) Anyway, they basically added another “groups” service that allows the user to access Group objects. The group feed objects, however, are translated into regular python dictionaries for you and are not XML feeds. I’ve attached an updated python script (see this post for the original) that reflects the change.

Read the rest of this entry »

Written by DK

May 9, 2009 at 10:08 pm

Posted in Python

Tagged with , ,

Parse Google Reader Files

leave a comment »

I wanted to send a friend a subset of the blogs I subscribe to, but didn’t want to cut and paste a zillion URLs into an email. Instead, I exported all my subscriptions to an opml file (just go to “Settings” in Google Reader) and used OPML, a lightweight python module, to parse the file.

OPML is basically a subset of XML, focusing on subscription “outlines.” The module allows you to slice the file structure as you would a list or list of lists. Let’s go to the interpreter:

>>> import opml
>>> outline = opml.parse(‘google-reader-subscriptions.xml’)
>>> for element in range(len(outline)):
…     print outline[element].title

General Interest
NotesToSelf
Primaries
Finance
Religion
Tech

As you can see, it doesn’t take very much to pull out the top level elements of the file. The code above finds the number of top level elements and loops through them, printing out the element title. FYI, it helps to take a look at the raw opml file, just so you are familiar with the element attributes (e.g. “title”).

You can’t tell, however, just from looking at the interpreter output which elements include nested elements (though you probably will if you are parsing your own subscription file). I know NotesToSelf is actually a feed, and I can inspect the attributes of this feed using normal python slicing syntax:

>>> outline[1].xmlUrl
http://notestoself.posterous.com/rss.xml

>>> outline[1].title
‘NotesToSelf’

You can examine nested elements similarly. The following command references the title of the first feed (“[0]”) in the fourth element (“[3]”, the “Finance” folder):

>>> outline[3][0].title
‘A Credit Trader’

There are probably better ways to share subscriptions, but I look at it as a gentle introduction to XML parsing.


Written by DK

April 8, 2009 at 10:28 pm

Posted in Python

Tagged with ,

Using Google Apps Python Provisioning API

leave a comment »

I’ve gotten my church staff to use Google Apps for email and collaboration (sort of). Unfortunately, the Google Apps admin web tool is a bit inflexible (though it’s improving). In an effort to get more control over the data, I put together a little script that pulls all the users, distribution list names, and distribution list constituents into a google spreadsheet. The program uses two nice python libraries, xlrd and xlwt, to read and write to intermediary excel files since it’s not possible to directly create google spreadsheets (the workaround I used is to upload an xls file).

The documentation for xlrd is quite good. The documentation for Google’s Python Provisioning API is ok, but not particularly well organized. For example, I was trying to figure out the attributes for the different XML feed objects and was told no documentation existed (by a Google employee, no less). After hours of trial and error, I found the PrintFeed function below in a separate piece of Google documentation that made it clear it was possible to use a generic attribute. I accept the possibility I was being a idiot. Anyway, the documentation for xlwt is not good, but the code below should give you a decent start and a few fruitful search terms.

The script basically makes a spreadsheet with a summary tab that lists the name of each distribution list and the number of members in each list. It also creates a tab for each distribution list containing the email addresses of the list. Pretty basic, but handy if people (that aren’t admins) need to know what email lists are available (and their constituents).

I’m sure there are better ways to code this, but what the heck, it worked for me. But I make no guarantees! (UPDATE, see post with updated code.)

Written by DK

March 11, 2009 at 10:39 pm

Posted in Python

Tagged with ,