Tag Archives: python

Python SMTP sink server

Debugging email sending in your Web application is always tricky as you need a working SMTP and you also need to watch out that you don’t accidently spam real users (if you’re working on a local copy of real database).

There is a neat one liner that acts as a “sink smtpd” server, meaning that it implements SMTP protocol, so you app can communicate with it, while printing everything it receives on screen:

sudo /usr/lib/python2.6/smtpd.py -n -c DebuggingServer localhost:25

(via Django Snippets)

Graphication on OS X (with pycairo)

I’m working on some visualizations and I want to achieve the look of Last.fm visualization lastgraph3:

wavegraph

The great thing about this is that Andew Godwin, author of the site open sourced the underlying library – Graphication.

Since I’m an OS X user, I decided to make the whole thing work on my system – Leopard 10.5.6 at the time of this writing. Here are the notes, mostly for my reference and because it might be also useful to others:

Get free XCode from Apple: http://developer.apple.com/

First build and install pkg-config from: http://pkgconfig.freedesktop.org/releases/pkg-config-0.23.tar.gz

Then install libpng and libjpeg libraries: http://ethan.tira-thompson.com/Mac%20OS%20X%20Ports.html

Build and install pixman from: http://www.cairographics.org/releases/

Build and install cairo from: http://cairographics.org/releases/

Build and install pycairo from: http://cairographics.org/releases/

Patching:
– you’ll need to edit cairo/Makefile.in file by modifying parameter _cairo_la_LDFLAGS (around line 215) to remove ‘-export-symbols-regex init_cairo’
(thanks Michael Dales)

Build instructions:
– just follow INSTALL file. Essential running:
./configure –prefix=/System/Library/Frameworks/Python.framework/Versions/2.5 (sys.path for my system)
and then make && sudo make install

Now you can finally build and install Graphication package :-)

All the installations steps are just ./configure && make && sudo make install and just work out of the box. I already have Python PIL installed from sources and I think you need this for one of the steps, but I’m not sure anymore for which one.

After you go through all the steps, you can start drawing something like that:

Simple visualization
Simple visualization

Now to figure out how to create something meaningful with it.

Reblog this post [with Zemanta]

Graphing Koornk

Saturdays are the days for hacking (after having a great run earlier of course). This time I’ve been preparing a network data set for a friend. The idea was to take a look at who follows who on Koornk.

Tools used: their API, Python scripts and Pajek to draw the results.

This is what I’ve got after a productive afternoon:

 

Koornk network
Koornk network (click on image for a bigger version)

I’ve only followed nodes that can be found by starting with Matija as initial node. Quick look at the resulted graph, we can see that their is a well connected group of people in a middle, with another group that is only partially connected to the first group. There are of course many others who are not well connected.

As this is only first prototype, there are a few more things to do on some other nice Saturday:

  • figure out a way to get list of all nicknames (one way would be to just go through the list of messages)
  • weight different connections, based on who’s talking to who
  • interpret the data (but since that’s not my homework, I hope the recipient of data set will want to do a guest-blog about this)
  • draw even prettier picture :-)

 

Reblog this post [with Zemanta]

Posting to Koornk using Python

While building something nice on Koornk (Slovenian Twitter like service), I stopped for a few months to learn how to do Authenticated POST requests using Python. I found urllib2 way too complicated, but soon I stumbled across great Yahoo Developer page – Make Yahoo! Web Service REST calls with Python that also lists alternative approach using httplib2 which then works beautifully.

Koornk blurry t-shirt
Image by Gandalfar via Flickr

Here’s the snippet of code used:

import urllib
import httplib2
http = httplib2.Http()

url = ‘http://www.koornk.com/api/update/’
username = ‘USERNAME’
password = ‘PASSWORD’

http.add_credentials(username, password)

params = urllib.urlencode({‘status': ‘ hey, im testing koornk api – httplib2 python request’})

response, content = http.request(url, ‘POST’, params,
headers={‘Content-type': ‘application/x-www-form-urlencoded’})

import simplejson
from pprint import pprint
s = simplejson.loads(content)
pprint(s)

Reblog this post [with Zemanta]

Visualizing books using Zemanta and Wordle

Most of my readers are by now probably already aware of Wordle, Java applet, that allows neat visualization tags. Given that Zemanta released early alpha API preview recently, I was looking for a fun project to showcase some of it.

So for this experiment I’m going to try to visualize some of the popular classic books, using text files of Gutenberg project. Technical details at the bottom of the post.

Jane Austen – Pride and Prejudice

Pride and Prejudice as through words and tags
Pride and Prejudice as through words and tags
Pride and Prejudice through words
Pride and Prejudice through words
Pride and Prejudice as tags

Herman Melville – Moby-Dick

Moby Dick through words
Moby Dick through words
Moby Dick through tags
Moby Dick through tags

George Orwell – 1984

1984 through words
1984 through words
1984 through tags
1984 through tags

Technical details

The whole process is done using a simple python script. The script reads in the text file, breaks it into chunks of 360 words, as is roughly one A5 page and then sends it to Zemanta API. It repeats this process for first 30 thousand words of the book. The limit is arbitrary, I just didn’t want to run the script for too long.

Afterward, the text was manually pasted into Wordle and I played with random function and details until I started to like the image. You can also take a look at my full Wordle gallery.

Lessons learned

I’m especially happy how 1984 turned out. For this kind of visualization it’s important to choose source carefuly, so you can get more powerful results that way. I’ll probably continue experimenting with this on 1984 text.

Zemanta Pixie