Tag Archives: python

GIO tutorial: Stream IO

This article is a short tutorial showing GIO’s file reading and writing functionality.

Note: This article is part of my ongoing GIO tutorial series, which currently consists of: (1) File and path operations; (2) File IO (this document); and (3) Launching files.

Continue reading

Advertisements

GIO tutorial: File operations

Recently I ported Exaile’s Files panel to use GIO instead of Python’s os module. The reason is simple: remote browsing.

Yes, you can now browse an SMB share or an SFTP server through the Files panel. Just mount the remote filesystem with gvfs-mount and enter the URL into the location bar. Previously Aren ported Exaile’s core to GIO as well, so remote files can be played just fine.

This is the kind of convenience you get out of using GIO/GVFS. In this article I’ll briefly explain what GIO is and how to do some basic file operations—similar to what the Files panel does—using GIO.

Screenshot of Exaile browsing and playing a file through GIO/GVFS

Note: This article is part of my ongoing GIO tutorial series, which currently consists of: (1) File and path operations (this document); (2) File IO; and (3) Launching files.

Continue reading

Unicode with Python 2 and PyGTK

Playing with Unicode in Python 2 is not fun, and combining this with third-party libraries brings even more headaches. This post explains how Unicode in PyGTK is handled.

Note: This information is only valid for Python 2.x. It will likely change when PyGTK releases support for Python 3.

Calling GTK+ functions: PyGTK accepts str and unicode objects as input. str objects are assumed to be in UTF-8. If you pass a non-UTF-8 str to a GTK+ function, it will work until you try to show it, where you’ll get a “PangoWarning: Invalid UTF-8 string passed to pango_layout_set_text()”.

Handling GTK+ return values: PyGTK functions always return strings as str objects. In most (all?) cases, the strings are encoded in UTF-8. Ideally, Python programs should use unicode strings internally, so it’s wise to convert the output of PyGTK function calls to unicode.

Example:

label1.set_text("Some UTF-8 string")
label1.set_text(u"Some Unicode string")
x = label1.get_text()  # x is an str object containing UTF-8 string.
y = unicode(x, 'utf-8')  # y is the unicode version of x.
y = x.decode('utf-8')  # Same as above.

End of 2008 update

I took a long hiatus from all my involvements in the free/open-source software world, including writing in this blog. Right now I’ve decided to go back, though I may not be as active as before.

Things happening during my break that I want to comment on:

  • Development on Exaile 0.3 started.
    • A lot of the database-related codebase was rewritten, but the DB has never been my main interest anyway.
    • I don’t see Unicode support improving. At this stage I’ll just wait for Python 3 support.
    • Portability bugs have crept in; I’ve fixed some of them, but haven’t been able to run Exaile on my only Windows (Vista) box.
    • The main Bazaar branch now contains several symbolic links. The problem is, Bazaar doesn’t support symlinks in Windows. This makes working on Exaile that much of a hassle, since I effectively can’t checkout the main branch from Windows. Yes, there are ways around this, but the truth is, I hate fighting with revision control systems just to be able to do my work. </rant> I worked around this by removing all the symlinks in the tree and re-creating them when necessary.
  • Python 3 released.
    • In isolation, this news means little. Python 3 itself is not perfect—I’ve bumped into a couple of problems—so it’ll be a while before it gets mass adoption. However, this release means everyone should start thinking about upgrading their code to the new syntax and API.
    • Rules of three: I don’t realistically expect all of Exaile’s dependencies to be ported to Python 3 anytime soon. There are essentially 3 camps we are waiting on: PyGTK, gst-python, and Mutagen. 3 months is my optimistic estimate of the time we need to wait before we can start porting Exaile. After that it’s probably another 3 months’ work for porting. (This projection is wildly off-mark. It’s been a while now and none of our dependencies support Python 3 yet, as of end of 2009. See also GNOME bug 566641.)
  • The Australian government is pushing for Internet content filtering.
    • The founder of iiNet (a big Aussie ISP) made a pretty strong comment against this.

Unicode in Python 2

… is a pain.

One of the things I like about Python is that it normally makes it harder to shoot yourself in the foot (monkey patching, anyone?). The only exception that is very frustrating for me is Python 2’s Unicode support, which is ugly and difficult to get right.

Really, at this point I don’t care much about other (planned) changes in Python 3. If Unicode support can be made as transparent as in Java or .NET, I would be really happy.

PyPy

I’ve known about PyPy for some time, even tried it at one point, but I never knew it was more than just “Python in Python”.

For one, it has a translation layer that basically takes the implementation (which can also be for languages other than Python) written in RPython and turns it into code for various platforms (native, CLI, JVM, etc.).

It’s also home to some interesting experiments in Python; watch the new development blog to get a taste. For example, the post about the rope data structure is really cool if you’re into that sort of stuff.

The fact that the developer of Psyco is working on it is also promising. (Have a look at Psyco’s shootout page if you don’t know what it’s all about.)

In general, a project worth monitoring.