TinyP2P: P2P Sharing in 15 lines of Python

I’m not linking to this example to show how hard it would be to outlaw P2P apps, but rather as an example of how it is possible to write unreadable code in Python (it’s not just for Perl anymore!)

Developing for ITunes under Windows with Python

When running ITunes under Mac OSX, you can use AppleScript to do all kinds of neat things. Under Windows you don’t have AppleScript, so Apple created a series of COM interfaces to allow programmatic access to ITunes and you music library (Microsoft has done the same things with Windows Media Player).

The nice thing about the COM solution is that you aren’t tied to any specific programming language (like AppleScript, for example). Of course the language that people use most for COM is C++, but there are COM bindings for VB, JavaScript (this is what the ITunes SDK examples are done in), Java, C#, Perl, and Python. Since my office has made the decision that all scripts are to be done in Python, I’ve been trying to do the same for any scripts that I write in my free time.

Back on topic. I’ve started to do some development with using the ITunes COM interface under Python. Having done my share of COM for C++, I was expecting the worst. But the Python COM bindings are really well done. If the COM object has a property, it appears in Python as a class member variable. If the COM object has a function, just call it like you would any Python function.

But not all is wine and roses… COM uses Unicode strings for all of its communications. Not a problem for Python as it is very happy with Unicode. But a problem when you try to print un-printable characters on the console. The offender in question: the single right quote. Not the apostrophe, but the single right quote. Subte difference. I tried many different ways to try and convert this extended character to something I could print (encoders, etc.) but ended up giving up. The only way I could get it to work was to go through every string I wanted to print and when it found a single right quote and replace it with the apostrophe.

The other problem that I ran into was how COM represents DATE objects. I wanted to compare two DATE objects to see which one was the most recent, but the obvious solution:

if (date1 > date2):
    do_something()

didn’t work. It turns out that you have to coerce each object into a float (the native COM representation for a DATE object) before you can compare them.

if (float(date1) > float(date2)):
    do_something()

Once I figured that out, things were pretty simple.

You can check out my scripts at my Wiki.