Using Python to Update your StatusNet Account
So you want to post to a StatusNet account (for instance at identi.ca ) using Python but don’t want to read the StatusNet API (which is compatible with Twitter so Python packages for the Twitter API should work just fine
).
Here is a little snipplet of Python code that lets you update your status:
#
import json, urllib2
from urllib import urlencode
# some stuff to identify yourself against the server
# and the base API path
user = "you"
pwd = "XXXX"
apibase = "https://identi.ca/api"
# connection magic
pwd_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
pwd_mgr.add_password(None, apibase, user, pwd)
handler = urllib2.HTTPBasicAuthHandler(pwd_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
# now define a message
msg = "Hi from Python"
# url encode it nicely and set your own client name – no links in source!
themsg = urlencode({‘status’:msg,‘source’:‘your client’})
# and send the notice
urllib2.urlopen(apibase+‘/statuses/update.json?s’ themsg, ‘’)
# that’s all folks!
And that’s about all you need to update your status using Python (2.something, the Python 3 urllib has changed a little bit ). Have a look at the API calls to do more fancy stuff and keep in mind to check for the character limit – which might be different from 140 chars on your server.
PivotX, Flattr and Textile
Since shortly before Christmas I am using flattr here in my blog1. Which is not a big deal, as m.eik of reaktanz.de has written a flattr plugin for PivotX that works very well. All you have to do (besides having a flattr account) is to put something like flattr uid="your_uid" button="compact" (surrounded by a pair of double []) in your entrypage_html template and the plugin will generate the javastcript code for you to include the flattr button automatically for the displayed blog entry.
I had a little problem with that, as it takes the introduction part of the entry as the description for the thing for flattr. As I use textile for formatting my postings the textile syntax would end up in the flattr description as well… I added two lines of code to the extension and replaced a third to render the textile2 syntax.
$textile = new Textile();
$descr = getDefault($params['description'], $textile->TextileThis($PIVOTX['db']->entry['introduction']));
Just replace the original descr=... line in the snippet_flattr.php file before uploading it to your extensions directory. If you don’t use Textile, you don’t need these two lines of a dirty hack at all as a proper implementation does looks differnt… ![]()
A second issue might be privacy. I’ve looked around in the flattr docs there seems no automatic way to automatically generate the flattr button for each blog entry (for instance) without calling flattrs API, which would expose your surfing behaviour to them. I’ve therefore put a static flattr button in the sidebar of the blog, which is hosted on my server. The dynamically generated flattr button, which includes a counter of how many flattrs a thing already received and adds the thing if it is not jet in the flattr database, is only included at the entry pages. You might want to disable javascript or use browser plugins like NoScript to block flattr buttons as well as some other nasty stuff.
Don’t feel obliged to flattr me, I don’t want to get rich with this blog here. You may also leave a comment down below, via OStatus, jabber or elsewhere ![]()
1 flattr is a micro payment system, say a virtual tip jar. You can define a monthly amount of money (min 2€) that will be distributed among the stuff you have flattered during a month. You can either flatter a thing once or subscribe to it. bicyclemark recorded a nice introduction together with Tim Pritlove to flattr recently.
2 All the rendered HTML elements will be stripped out from the description in any case as flattr does not allow HTML syntax in the description! But not render the text will look strange on the description pages. Think e.g. of the syntax to include an image with some style definitions…
Lessons Learned - 4: Log Log Plots
There is a simple way to do loglog plots with python using the matplotlib but IMO these plots suffer a major flaw. Using this method results in plots with axes labeled 10 n which is not the loglog plot I want… It relatively easy to fix this problem – just plot the log of the stuff and set the axis labeling by your self ![]()
Lessons Learned - 2: Histograms
Ok, here we go with Part 2 of the Lessons Learned. After I’ve covered basic plotting using matplotlib in this part I’ll show you two ways to display histograms of some data.
Lessons Learned - 1: Basic Plotting in Python using Matplotlib
I have handed in my Diploma Thesis this Monday an the thought hit my mind that I’ve learned so many things during the last year, that I can at least share some of it. Therefore I plan to do a little series here Lessons Learned.
The 1st one, this one, will deal with basic plotting in Python, because the basic plots with matplotlib are looking somewhat nice but can be made nicer with only a few easy steps.
Mass Mess with Symlinks
I broke, by accident, some 1000 symbolic links while trying to free some disk space when deleting their source files. One thing I could do was to play back an backup1 of the file obviously, but then I’d still have the problem of a full disk as the 1000 symlinks pointed to files of about 130GB file size. Now one of the backups of these data is placed on another hard-disk in the PC so I can as well just repair the symlinks and be happy.
Repairing, meaning deleting the broken links and setting a new one, for 1000 links by hand was not something I planed to do so I searched for a tool to do this. Surprisingly I did not found any2. So I turned that problem in a little Python programming exercise with the following goal:
- check every symbolic link in the current directory
- if it is broken and a simple replacement of parts of the source name that are provided by the user (me) will fix it (in my case it is work2 → work1), set a new link
You can get the whole code here: symrename.py.
Place the file into your $PATH, make it executable and call it with two parameters broken part and fixed part. E.g.: symrename work2 work1 will check every symbolic link in the current directory and replace work2 in the source filename with work1 in broken links. Remember to mask special chars and don’t be surprised if you replace short strings, that the result is failing.
The script was tested under Gentoo and OpenSuSE with different Python versions (2.6+) so I hope that it will work for you too. If not feel free to contact me.
1 And what a relief it was to have multiple backups of these data ![]()
2 Ok, I did not search that long, so feel free to advertise some tools you know that do the job in the comments.
annotate text to images with python
After the last gaming sessions I spend some thoughts into a fix idea I got during the ride home. As I’m not jet sure whether or not I realize this idea I’ll rather not tell you about it yet ![]()
Part of the realization of that idea would be placing a lot of text to some images that already exist. Therefore I spend some time researching nice ways to do this, with Python as that’s the language I have to use for my Diploma Thesis anyway…
I was a bit disappointed by the tutorials I found, probably because I used the wrong questions at the search engine… That was until I found the great tutorials by Nadia Alramli from last summer about the Python Image Library (see links below)
The very basic way to place some text at any given point of an image (without using Gimp) is illustrated in the following code snippet:
image = Image.open("image.png")
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("arial.ttf", 20, encoding="unic")
draw.text( (10,10), u"This is a test! with German umlauts äöü", fill=‘#a00000’, font=font)
image.save("out.png","PNG")
This will open the image (as long as PIL supports the image type), select a font (TrueType or OpenType), place the text at any position you desire (here at pixel 10,10) and save the image again.
Links
- Python Imaging Library
- PIL Tutorial: From Basic to Advanced Drawing
- PIL Tutorial: How to Create a Button Generator
edit : looks like the syntax highlighting has some problems with the generated HTML, just ignore the (2010-12-08) installed the GeSHi extension which does the job.<span class="caps"> and </span> tags … I’ll look at this later …





