Conditional Expressions in Python 2.4

Python 2.5 introduced a new syntax structure: conditional expressions. For programmers in languages such as C, these structures seem very basic and fundamental, but Python lacked them for many years. As I said, Python 2.5 introduced such a syntax structure; one may use it in the following form:

x =  a if condition else b

As you probably guessed, a is assigned to x if condition evaluates to true, and b is assigned otherwise. This is pretty much equivalent to the C conditional expression. But as I said, this structure was only introduced in 2.5. Previous versions of Python are still widely deployed and in use, so how do you achieve the same thing in older versions of Python?
Continue reading Conditional Expressions in Python 2.4

radio.py – a Wrapper Script for Listening to Radio in Linux

Download radio-0.3.tar.gz.

Update: radio.py-0.4 is now available.

I like listening to music and radio while working, and fortunately there are numerous ways to do that. Unfortunately, most ways that allow you to listen to radio are very resource-consuming memory hogs (such as listening to streaming media via web browsers) or very unfriendly to users (listening via mplayer, for example). So, I set out to find a way that would use as few system resources as possible while keeping it user-friendly. One other requirement I had was being able to do all that from the command line, so it would work great with GNU Screen and wouldn’t require an X server (if I worked without one).

I used mplayer for some time for listening to radio. I had a file with a list of web radio stream URLs, which I would copy and pass to mplayer -playlist. This method met two of the requirements (minimal resources and command-line interface), but wasn’t really user-friendly. So, I wrote a little wrapper script in Python around mplayer – radio.py. After a quick installation (download and extract the tar archive and copy radio.py somewhere in your PATH), radio.py will allow you to listen to stations easily, and it will also do a couple more things for you.

To listen to a station, just call radio.py with the station’s name; e.g., in the command line enter radio.py BBC1 to listen to BBC Radio 1. To view a list of known stations, run radio.py --list. Currently there aren’t many stations (just stations I thought were needed or that I listen to). You can easily edit radio.py to add new stations (the script is documented and very clear). If you do so, please write a comment or email me so I will be able to add those stations to the next release by default.

So, as you’ve seen, radio.py allows you to easily listen to radio, as easily as writing the station’s name. But, as I said, it can do more things that I thought should be in a radio script. It has both a sleep feature (that turns off the radio after a specified amount of time) and a wake-up feature (that starts the radio after a specified amount of time). These two features can be used together, and practically allow you to use radio.py as an alarm clock.

You can find more information about radio.py options by calling radio.py --help. I hope you will find this script as useful as I do.

Download:
radio-0.3.tar.gz.

Convert KDevelop’s Source Archive to a Source Package

I use KDevelop as my main IDE, and I’m pretty satisfied. KDevelop can create a source archive of the project’s source code automatically for you, which simplifies distribution of the project. Unfortunately, the archive created isn’t ready for distribution. The user can’t just run ./configure ; make, as they need to run all the automake tools first. That’s not ideal for distribution. So you need to convert this source archive to a source package that is ready for the user to compile immediately.

Continue reading Convert KDevelop’s Source Archive to a Source Package