PyQTrailer revisited

November 10, 2010 in en, fedora, open source, projects, pyqt, python, software

Some time ago, I wrote about my little project: apple.com trailer downloader. Apple is still not very open-source friendly as far as its trailer website is concerned. So all points I made in my original post still stand. To my surprise this little project is still alive and kicking, with new ideas for improvements coming and coming :-). What is even more important: it seems that so far no breakage happened due to apple changing something on the web.

Since the first version I released almost 6 months ago several new features appeared. Some of them include:

  • Parallel downloading of trailers
  • Ability to run movie player (mplayer, vlc, etc) without downloading file to HDD
  • Lot of customisation/performance options added
  • Working support for trailer search
  • Localisation support
  • Python 3 support

Latest version (0.5.2) is available in Gentoo repositories already, and should hit Fedora updates in next day or so (this will be delayed due to new package acceptance criteria though). Enjoy.

Python3, PyQt4 and missing QString

October 20, 2010 in howto, open source, programming, pyqt, python, qt

As I was recently adding support for Python 3 to my little trailer downloader application that I mentioned before (PyQTrailer) I encountered a strange problem with PyQt4 that only occurred in Python 3.

Let’s take this simple python example:


$ python
>>> from PyQt4.QtCore import QString
>>>

That same code snippet doesn’t work in python3 interpreter though:


$ python3
>>> from PyQt4.QtCore import QString
Traceback (most recent call last):
File "", line 1, in
ImportError: cannot import name QString
>>>

My first instinct was: Bug! Gentoo PyQt4 ebuild was doing something terrible and somehow made PyQt4 unusable in python3 interpreter. Turns out my gut instinct was wrong (once again :-) ).

PyQt4 since version 4.6 changed API of QString and QVariant for python 3.x. For QString this is due to fact that from Python 3.0, string literals are unicode objects (no need for u’unicode’ magic anymore). This means that you can use ordinary Python strings in place for QString. But I wanted my QString for something like this:


...
downloadClicked = pyqtSignal((QString, ))
...

This snippet creates Qt signals that you can then emit. Question is… How can we update this for Python 3.x? We could probably just replace QString with type(“”), but for a change that wouldn’t work with Python 2.x. So? Python dynamic nature to the rescue!
Edit: simplified QString definition (thanks Arfrever)


try:
from PyQt4.QtCore import QString
except ImportError:
# we are using Python3 so QString is not defined
QString = str

If we put previous code sample to the beginning of our Python file we can use QString in our code and it will keep working both in Python 3.x and Python 2.x. Case closed dear Watson.