Python


I never thought I’d manage to find a job coding in python for a living. But that is what my full-time job is, right now, anyway.

I can’t really talk about the work I’m doing, since it is a commercial enterprise, but I can talk about what sort of things I am coding. I have spent the last couple of weeks rebuilding a server in python that uses SOAP to communicate with the outside world (well, a client application, anyway) over SSL connections. Python is used so it is easily extensible, without having to recompile. Eventually it will be dynamic, where new modules can be added to a database, and depending on the userid of a request, a different function will be called. It’s really quite exciting.

I’ve come across a couple of new software programs - one of which is NX (nomachine) Client, which is a remote tunnel for X windowing. I can remote in via this to work from home, as well as ssh or sftp. Which is fairly cool. Speedier than VNC, since I think the local X-Windowing system is responsible for some of the drawing tasks. Feels about the same over ADSL as ARD does over WLAN.

In my “free” time I’ve been doing a couple of other things, both programming tasks. The first is a web application for an art gallery to create HTML and PDF invitations and newsletters. Originally I planned to use a web app so that it could eventually be rolled out as a blog-alike - in fact originally it was just going to be a WordPress installation with some minor modifications. It turned out to be easier to rewrite it from scratch. I have learned from this process that PHP is crap: it’s never clear about the way to do stuff, and many functions have weird names. count_chars, for instance, doesn’t really count the characters, unless you decide that things like #@! aren’t characters. In which case you want strlen. Which had me tricked for some time, since I stopped looking once I had found count_chars. Python and len(anything) is much better.

Speaking of python (again), I’ve also been working on a Regular Expression helper - similar to the one that comes with Komodo IDE. I started (and pretty much finished, in a matter of hours, to the extent it solved my first use problem) this after having to load up Komodo just to get a visual representation of which bits of a text block were being matched by a regex. Still some kinks to work out - I need to figure out how to put stuff into an outline view, so I can see more than just matches, but match groups. Then it will be all good.

In the process of my work job, I downloaded SOAP Client, a freeware tool for testing SOAP packets. It was all good until I tried sending HTTPS requests, which it fails unfathomably on (cannot connect to endpoint…). I emailed the author, and he then promptly released the source code. I’ve snaffled that from Google Code, and I’ll try to hack through it a bit to implement SSL connections. Not sure how to go about it at this stage - dunno if it is with WebKit or something else I need to do. I also plan to add in the ability to edit the SOAP request manually before it is sent off to the server.

I start Uni in a couple of weeks - I’m doing an introductory Java course in intensive mode, which I expect to be fairly easy. I’m really only doing it so I can do the meatier sounding subjects, like Programming Language Concepts and Systems Programming. I really think I’m going to enjoy this course. I will be interested when I come to the Internet Computing subject, since I’ve been doing a fair bit of that in various forms over the past few years. Be interesting to see what the academics think it means.

Well, that’s been my life over the time period since coming back from the beach for a 10 day holiday over New Year. Apart from squeezing in a few games of Touch Football here and there, I’ve pretty much been chained to my laptop.

And loving it.

View Comments (0)   RSS Feed for Comments on this Post

Using python, it’s easy to get data from IMDb into your iTunes database.

You’ll need a few tools to get this working. appscript, py-IMDb and CocoaDialog. Install each of these, and for the latter, note where you store it. I’m not going to go through the code, as it is fairly well commented. If you can’t get it to work, leave a comment and I’ll see if I can help.

IMDB2iTunes.py

Note that you will need to save the file above, and rename it so it has a .py extension.

View Comments (10)   RSS Feed for Comments on this Post

I’ve consolidated all of my media onto my new Mac Mini, but didn’t really think some things through when I first did it. For instance, I put all of my video data in before transferring my music across. I’ve still kept a copy of my music on the laptop, for what it’s worth, but because I just copied files, then I lost all of my rating data.

I looked at a couple of solutions for transferring the ratings, the most promising was a semi-manual method of creating smart playlists, one for each rating.

That was too old-tech for me, so I came up with a solution that uses Remote Apple Events. Now, to make the code easier, I’m using python and appscript, so make sure you have both of those installed.

#! /usr/bin/python

import appscript

# Set up the two iTunes libraries.
local = appscript.app('itunes')
# You'll need to replace jens.local with your remote machine's name
remote = appscript.app(url='eppc://jens.local/itunes')

local.lib = local.playlists()[0].tracks()
remote.lib = remote.playlists()[0].tracks()

# Create a dictionary with all local track names/artists/albums

library = {}

for each in local.lib:
    key = each.name()+":"+each.artist()+":"+each.album()
    library[key] = each

# Iterate over every remote track.
# If it is in the local library, take the local rating and
#    apply that to the remote track.

for each in remote.lib:
    key = each.name()+":"+each.artist()+":"+each.album()
    print key,
    if library.has_key(key):
        each.rating.set(library[key].rating())
        print "rated."
    else:
        print "doesn't exist in local library"

That’s it. I’ve used this to transfer all of my ratings from local to remote iTunes. Granted, there is no check to see if I’ve got all of the local tracks on the remote machine - but that is mainly because I don’t have my Podcast library on the remote machine, and I hope (know?) I have all of the music, and that’s the stuff I care about.

Here’s a screenshot from my remote library.

View Comments (0)   RSS Feed for Comments on this Post

I’m going back to Uni next year, to complete a post-grad degree in Computer Science. This is a course that people without a background in Computing can enrol in, so I’ll be able to get a bit of credit towards some units, but not too much, since when I did part of a Computer Systems Engineering degree, Java wasn’t the language taught. Now, at all three Universities in Adelaide, it almost seems to be the only language being taught.

Just for my own personal gain, I’m also teaching myself Objective-C. This is the language used by Apple and third-party coders for most of the OS X application software. All of their APIs are available under Objective C, and Java, whilst still somewhat supported, doesn’t appear to be as high on Apple’s agenda as it is on the local Universities.

I’ve borrowed books in the past on Cocoa/ObjC, but never really made much progress. This time, having a laptop means I can actually do my learning on that, wherever I happen to be. In this coffee shop, for instance.

I’ve also found some handouts and example program “tests” at Stanford University, and have started to work through those. The first program, a simple conversion application for temperatures, was fairly simple, and I wrote that. I even managed to complete the extra credit sections, or at least two of them.

Psuedo-evidence (in that you can’t actually see this working):

(Note, the temperatures change to red when above a certain value, and to blue when below a certain value. Within the ‘normal’ range, they are black).

This wasn’t a programmatically onerous task, more about making sure you can connect up various components and have them affect one another.

Instead of doing the next task, I’m pretty confident I’ll be able to write the application I gave to my Year 12 students, a 9-letter word puzzle solver. It will be interesting to see if I can effectively re-implement the python version I’ve already written:

The only reason I really want to learn Obj-C is that it enables me to use a GUI design tool, rather than having to create the interface using coordinates. And packaging up python applications isn’t exactly fun, either.

And, after the 9-letter puzzle solver, I might reimplement a sudoku solver I wrote. Not sure if I have the python source code for that one. And I never wrote a GUI for it, either.

View Comments (0)   RSS Feed for Comments on this Post

Or Python3.0, as it is also known, has been released, or rather, the first alpha of it has.

It builds okay on OS X, although I’ll need to download some stuff to get it to build bsddb and readline, among other things. Since I use these two modules, I’ll get them at least.

There are lots of changes in python3.0, including integer division returning floats, and input() being what it should be (instead of having to use raw_input()). print has become a function, and a whole lot of other things have changed. In fact, programs are likely to not run under 3.0 without a rewrite.

I’ll keep 2.5 around for a while longer, methinks. Hell, I’ve still got 2.3 on here, just incase.

View Comments (0)   RSS Feed for Comments on this Post

Okay, I think I’ve found an appscript bug.

If you attempt to connect to a networked machine, such as doing:
>>> from appscript import *
>>> it = app(url=”eppc://user@machine.local/iTunes”)
>>> it.activate()
Which works fine if you have a password, fails if there is no password.  And the error reported is:

RuntimeError: Can’t get terminology for application (aem.Application(url=’eppc://user@machine.local/iTunes’)): CommandError -128: userCanceledErr

Just because I haven’t put in a password, doesn’t mean I have cancelled the operation.

I’m still working on a fix to get it to work.

View Comment (1)   RSS Feed for Comments on this Post

I came across a strange issue today. I have written a script that adds acronym tags to a post created in ecto, and another that adds the currently playing iTunes track, as nice links to iTMS.

However, the problem that arose is that the script wouldn’t run. I had the shebang line, and everything, but no joy.

Then I noticed that I have upgraded my python installation to 2.5, but for some reason the /usr/bin/python was still pointed at 2.3 - but my interpreter run from the bash shell, or from Komodo was 2.5. This itself wouldn’t be too much of an issue, except I was using appscript, and I had only installed appscript into the 2.5 Python installation library.

So, it was failing (way too silently, for my liking), and I couldn’t tell why.

All fixed now, though.

Fly Me AwaySupernatureGoldfrapp

View Comments (0)   RSS Feed for Comments on this Post

This is the best way I can find to test if an application is running under appscript:

try:
    app('System Events').processes['AppName']()
except:
    print 'Application AppName is not running'

I think it’s kinda neat. I’d like to know of a better method, but I looked pretty hard!

Burn Down The Mission (Live)Elton JohnLive In Australia

View Comments (3)   RSS Feed for Comments on this Post

Maybe I’m just being crazy, but I’m not really finding wxPython particularly pythonic. For instance, I’m trying to make a fairly simple application that has a SplitterWindow, and I seem to have to do a whole lot of sub-classing to actually get it to work. Feels more like Java than Python. Which I’m really not keen on doing.

NoticeGomezChillout Sessions 9

View Comments (4)   RSS Feed for Comments on this Post

bash:

mdls filename | grep FinderComment

AppleScript:

tell application "Finder"
    comment of file
end tell

python:

#!/usr/bin/pythonw
from appscript import *
path = '/Users/NAME/your/path/here'
comment = app('Finder').items[path.replace('/', ':')].comment.get()

View Comment (1)   RSS Feed for Comments on this Post

« Previous PageNext Page »