This article is a short tutorial showing GIO’s file reading and writing functionality.
This is the 2nd part of my GIO tutorial series. Part 1 talks about simple file operations with GIO.
This article is a short tutorial showing GIO’s file reading and writing functionality.
This is the 2nd part of my GIO tutorial series. Part 1 talks about simple file operations with GIO.
Recently I ported Exaile’s Files panel to use GIO instead of Python’s os module. Why? 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.
This is the 1st part of my GIO tutorial series. Part 2 talks about GIO stream reading and writing.
This is a short explanation of UTF-8—what it is, how it works, and why it’s popular.
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
This is a bug in pysqlite 2.4.0, fixed in 2.4.1.
References:
… 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.
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.