'– – Josh

Intro to PL/Python

PyOhio, July 26, 2014

Code Examples

Twitter in your Database

A PL/Python example that shows the first page of a user’s home timeline as a view in the database. The view wraps a set-returning function. Additionally as a second example, a PL/Python trigger is attached to the view as a DO INSTEAD trigger, allowing an INSERT command to post a new tweet.

Remote slide control database structures and functionsand Flask micro-site.

During the talk, the slides above were driven through a remote connection. Movement and control commands were sent through an event stream connection, using PostgreSQL’s LISTEN/NOTIFY feature.

Central Ohio Python Users Group, January 25, 2010

+ PostgreSQL Conference East, March, 2010

Code Examples

RSS Reader

CREATE FUNCTION readrss (rssurl TEXT) RETURNS SETOF rssfeed AS $$
import feedparser

feed = feedparser.parse( rssurl )
for entry in feed["entries"]:
	yield ( entry["date"], entry["title"], entry["summary"] if entry.has_key('summary') else None, entry["link"] )
$$ LANGUAGE plpythonu STRICT VOLATILE;

Some RSS feeds don’t have summary portions (the example using a YouTube feed resulted in an error during the original presentation.) Python 2.5 introduced something resembling a ternary operator, which we can use here to replace any missing summary with the SQL NULL value. If it’s possible that other fields can be ommitted from an RSS feed we’ll probably want to do the same thing there.

PostgreSQL Directory Walk (8.4+)
This simple example combines two very simple PL/Python functions with the recursive CTE capability introduced in 8.4.

Low Account Balance Email
The function is shown in two revisions. If this were used in production the smtplib module should probably be combined with a module to generate the email message itself.


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...

Archives

All entries, chronologically...