Having given both a whirl

29.04.2009 20:02

I'm fluent in both Perl and Python. I've been writing Perl scripts of various sizes since I began using Linux and more recently I've also worked on larger projects using Perl (from G-Tablix to Wikiprep). On the other hand my work at Zemanta has largely been about writing software in Python. Large amounts of complicated, production critical software in fact.

There's this very obvious philosophical difference between these two languages: Perl is all about there is more than one way to do it, while in Python there is (preferably) only One.

Recently I've rewritten software for this blog in Perl. My colleagues were surprised when I mentioned this. Why didn't I do it in Python, when I had plenty of examples at work of how easy it is to set up a web page with Django for example?

Perl and Python

In my experience with both languages the fundamental difference in design philosophy shows in an interesting way: Writing software in Perl is fun by itself. The multiplicity of solutions to a single problem that Perl offers gives you the opportunity to express yourself. You can stop for a moment to contemplate the most elegant way of doing something. It's about writing software in your own, distinct style. To match your thought patterns. Not surprisingly, Perl is often compared to poetry.

Writing Python on the other hand is dull. If you already know in advance the algorithm you are about to write, writing code in Python is pure drudgery. You only have this limited number of valid expressions. You are forced to do indentation just right. Chances are that you'll write exactly the same code as the other guy if he was given the same specification. It's boring because it's simple and straightforward.

You better be discovering something new on the algorithmic level when using Python, or it won't be very enjoyable. On the other hand writing Perl code itself is fun.

Python is for experiments on a logical level. Perl is for experiments on the syntax level.

Then there's the different approach to errors: In majority of cases Perl will do what you want (unless what you want is consistency, as perlfunc(1) author remarks), maybe giving you a warning that you can later research and fix if you're a perfectionist. But in most cases if an undef gets into the stream it's an one-in-a-thousand occurrence that you can safely ignore.

Python on the other will usually throw exceptions in the most unexpected places and a single uncaught exception will kill your program (which was probably 5 minutes away from completing that 100 hour task).

It's the bugs in the code where the situation turns around. Fixing bugs in Python is fun. You get a descriptive back trace by default on all errors, code is easy to read even when it's not yours and it's satisfying to follow that stray None value as it traversed half a dozen modules until it got to some function that actually tried to do arithmetic on it.

On the other hand when something does go seriously wrong in Perl you better be the author of the code you are looking at and be familiar with interpreter's guts. Since functions will silently pass around invalid values and wrongly encoded strings, the root cause of the error you're hunting may be thousands of lines separated from its effects.

Now it's probably clear why I wrote my blogging software in Perl. It's an application that will most likely never be used, maintained or extended by anyone else by me. I was reinventing the wheel. There is no opportunity to be creative with software design of a yet another blog. It's a straightforward job of telling the computer what you want. So at least I was able to get some satisfaction from the act of expressing my wishes by jotting them down in Perl.

Having said that, I still firmly believe Python is way better language for any kind of cooperative work when compared with Perl. Most of software is at one point worked on by more than one person, so on average Python is probably a better bet when starting a project from scratch.

On another thought, even going through your own code a few years later may be considered working together with your younger self. So far I hadn't had any problems understanding my oldest Perl scripts. Then again maybe they're just not old enough.

Posted by Tomaž | Categories: Ideas | Comments »

Changing GNOME menu icon, Debian way

27.04.2009 18:37

If you updated your Debian Unstable installation recently and you use GNOME, you've probably noticed that the usual foot icon in the panel was replaced with the Debian logo.

Debian branded GNOME menu

I don't like that change, so how do I put the old icon back on the menu? After a quick search I found this guide, but for some reason it didn't work for me.

It turns out there's a Debian way of doing it:

$ update-alternatives --list start-here.svg
$ sudo update-alternatives --set start-here.svg /usr/share/icons/gnome/scalable/places/gnome-foot.svg
Using '/usr/share/icons/gnome/scalable/places/gnome-foot.svg' to provide 'start-here.svg'.
$ sudo gtk-update-icon-cache --force /usr/share/icons/gnome
gtk-update-icon-cache: Cache file created successfully.
$ killall gnome-panel
Posted by Tomaž | Categories: Code | Comments »

Paper pile flashback

24.04.2009 18:33

I came across this pile of ring-bound printouts while cleaning my bedroom yesterday.

Old tutorials

This used to be my precious printed library of documents I downloaded from BBSes and early web sites via agonizingly slow and unreliable modem connections.

I was fascinated by computer graphics then, trying to write my own 3D engine. Around that time my dad replaced a monochrome Hercules with a VGA graphics card and an old Compaq color monitor (RGB #000000 was displayed as dark green on it). Shareware version of Doom just barely ran on the AMD 386DX with the viewscreen set to minimum size.

On the left there's a VGA Trainer Program by Denthor of Asphyxia (a really wonderful set of tutorials that got me hooked on the topic after the first page). On the right is Speedy free direction texture mapping and other nifty tricks by Hekan "Zap" Andersson (in its full 9-pin dot-matrix glory).

Scary. Today it's faster and easier to retrieve a document like that from a server in Australia using a search engine in North America than to open a paper folder one meter away.

Posted by Tomaž | Categories: Life | Comments »

My contribution to global warming

22.04.2009 9:00

With today being the Earth Day, I went on and did some calculations on my more-or-less daily commute to Ljubljana.

Almost exactly a year ago I bought a car, since I got fed up with the bad state of Slovenian bus and rail public transport. I tried to stay away from this inefficient mode of transportation for as long as possible, but train delays and long bus rides pretty much made any life outside of Zemanta's office impossible.

Having a bit of a bad feeling about all that I tried to chose a car that wouldn't be that bad for the environment. So I paid close attention to the official fuel consumption and mg CO2/km figures when buying. In the end I decided on a Citroën C3 1.4i 16v

I also kept a log of the distance I've driven and how much gas I've bought. So now after one year I have some data which I can compare with those official specifications.

  • In one year I've driven 14000 km and burned 920 l of unleaded gasoline.
  • That gives fuel consumption of 6.4 l/100 km (37 miles per gallon for you imperial types). Not surprisingly, that's a bit higher than the official 6.2 l/100 km (combined urban/extraurban figure).
  • Burning 920 l of gasoline gives around 2100 kg of CO2. For comparison, the recently built solar power plant in Ljubljana is said to save 35000 kg of CO2 emissions per year.
  • Total chemical energy content of 920 l of gasoline is approximately 32 GJ or 8.9 MWh. That same solar power plant will produce 75 MWh per year.
  • This amount of gasoline cost me a little below 1000 €. The equivalent amount of electrical energy at last year's average residential prices would cost 890 €

According to official figures there were 1 million cars registered in Slovenia two years ago. If every one of those cars consumed the same amount of energy as mine per year, then two more nuclear power plants like Krško would be required in case everyone switched to hydrogen or electric propulsion (plus major upgrades to the distribution network).

Another interesting thing is this graph of fuel efficiency versus time. The yellow band is the time I used winter tires (ignore the peaks - they are the result of measurement error when I bought less than a full tank of gasoline).

Fuel efficiency versus time

I can see no significant difference in efficiency between summer and winter, which is surprising, since everyone seems to claim that winter tires are less efficient.

Posted by Tomaž | Categories: Life | Comments »

Get Satisfaction suggestion

18.04.2009 0:52

It's not my duty to read and reply to complaints that get posted to Zemanta's corner of Get Satisfaction public support forums. However I do get notified about them and usually read a few that look like they may have something to do with my department.

Surprisingly many of them rank pretty high on the level of politeness and usefulness in regard to solving the problem. However, there are some that make you wonder which evolutionary step the writer of that particular post missed. For those gems, I propose a minor change to the usual I'm feeling happy/sad/whatever cuteness, which would at least Give Satisfaction to the replier, since the original poster is obviously incapable of getting any.

Get Satisfaction UI proposal

* Not his real name. No offense meant to the chimpanzees.

Posted by Tomaž | Categories: Ideas | Comments »

Math humor

16.04.2009 22:33

Kaj ti bo pi, nauči se e!

Seen at the Faculty of Mathematics and Physics today.

Rough English translation would be What use is π? Learn e instead!

Posted by Tomaž | Categories: Life | Comments »

ASF to OGG with GStreamer

12.04.2009 20:07

Here's how to convert an audio recording in Microsoft's proprietary ASF format into Ogg/Vorbis using GStreamer.

ASF is a container format which can contain audio or video streams in many formats, so you first need to figure out which decoder you need. playbin is a plug-in that automatically tries to construct a working GStreamer pipeline for a specific file.

$ gst-launch -v playbin uri=file:///home/avian/foo.asf
/playbin0/decodebin0/ffdec_wmav20.src: caps = NULL
/playbin0/decodebin0/ffdec_wmav20.sink: caps = NULL
/playbin0/decodebin0/queue0.src: caps = NULL
/playbin0/decodebin0/queue0.sink: caps = NULL
/playbin0/decodebin0/asfdemux0.audio_00: caps = NULL
/playbin0/decodebin0/typefind.src: caps = NULL
Setting pipeline to NULL ...
FREEING pipeline ...

Ok, we need asfdemux to demultiplex the stream and ffdec_wmav2 to decode it.

So, a pipeline that decodes this particular file and re-encodes it into Vorbis looks like this:

$ gst-launch	filesrc location=foo.asf ! \
		asfdemux ! \
		ffdec_wmav2 ! \
		audioconvert ! \
		vorbisenc ! \
		oggmux ! \
		filesink location=foo.ogg

Simple, isn't it? It only takes an hour of trial and error to figure this stuff out.

Posted by Tomaž | Categories: Code | Comments »

Reversible computing

10.04.2009 19:35

I've stumbled upon a Wikipedia page about reversible computing recently. It's a concept I haven't seen before.

The idea is that there's a theoretical limit to the efficiency of computation. For every irreversible bit operation (for example AND, OR, but not XOR or NOT), it is thought that two new microscopic states are added to the thermodynamical system that's doing the computation, increasing its entropy by:

\Delta S = k\ln{2}

If the temperature T of the computer remains constant, an amount of heat must be released to the environment:

\Delta Q = kT\ln{2}

That's called the von Neumann-Landauer limit, and it comes to 33 picowatts at 10 GHz clock and 75°C

As it's name suggests, reversible computing is exploring possibilities of doing computations with only reversible operations, that is logic gates that have a 1 to 1 mapping between input and output states. In contrast to irreversible operations there is no known lower limit to the amount of work that has to be done by such a gate.

Such computers would be very different from what we have today. For example, a mere variable assignment in procedural languages is irreversible, since the previous content of the variable is lost. On the other hand, you could run a program backwards starting with the result and ending with the input parameters. That would be an interesting feature for a debugger.

As I see it, this kind of the research fits in the same box as Dyson spheres and warp drives. These kinds of limits won't be reached any time soon in practice (they may bug Universal AC eventually). But it is still fascinating to see just how far you can push some concept before the most basic physics starts to interfere with you.

Posted by Tomaž | Categories: Ideas | Comments »

Kinky stuff

08.04.2009 20:14

Latex glue

I need this to repair a robot dinosaur's neck. I swear!

Posted by Tomaž | Categories: Life | Comments »

The smell of magic smoke

05.04.2009 19:16

For the last few days there was an increasing smell of magic smoke (i.e. the smell of overheated electronics) in the room where my little server stands.

This morning I went to investigate and it turned out to be a classic case of power supply fan failure (interestingly, the PSU is the newest component in that machine). This time it seems I was quick enough and nothing else failed because of the elevated temperatures. After a generous application of oil to the fan's bearing everything seems to work fine.

Plus, I had the opportunity to do some trick photography:

Computer fan cut-out

Oh, and I can't believe that anyone would use compressed air to clean out dusty machines. My method of choice is a medium sized dry paintbrush combined with a vacuum cleaner and I still tasted the dust in my mouth for the rest of the day. I can't imagine what it would look (and feel) like if I would just evenly distribute the dust over the room's volume with a blow of air.

Posted by Tomaž | Categories: Life | Comments »

Dynamic Bass Boost

04.04.2009 0:21

A while ago I was doing some fairly trivial repairs on a Philips portable radio/CD/cassette player. It had a sticker on it claiming that this particular product features something called Dynamic Bass Boost.

From the outside this means an extra switch, that when turned on, causes the music to sound better (well, different). It appears that this is just another marketing name for a filter that tries to compensate for the frequency response of the human ear (sometimes it's called Loudness, my Sony calls it Dynamic Sound Generator).

The idea is that the ear is less sensitive to low and high end of the spectrum and that the sensitivity varies with the volume. So to compensate you insert a filter into the audio amplifier with a transfer function that matches the inverted ear sensitivity function.

I was curious how exactly that filter is implemented, so I disassembled the radio a bit further than it was strictly necessary for repairs and had a look at the circuits. Judging by the patent application I was expecting something complicated, with active filters and such.

Well, it turned out that in practice it is a lot simpler than that:

Dynamic Bass Boost circuit

This little passive filter circuit is all there is behind that button. It's implemented on a small circuit board that's attached directly to the rotary logarithmic potentiometer for volume adjustment (R1 and R2 on the picture) and is duplicated for each channel. Input is marked Ui and output is Uo. The button merely shorts C2 to the ground.

Unfortunately, the capacitors and resistors didn't have any recognizable markings on them, so I wasn't able to simply read their values. Instead of measuring each component separately, I measured the circuit's frequency response using a signal generator with frequency sweep and a digital oscilloscope with a Fourier transform:

Measured frequency response of disabled DBB circuit

(DBB turned off)

Measured frequency response of enabled DBB circuit

(DBB turned on)

The potentiometer has a tap at around on third of its range. As you can see from the circuit, with volume between zero and the tap position the shape of the transfer function is constant while at higher volume settings the transfer function gradually flattens out (compare that with the equal-loudness contours - the curves are flatter at higher volumes).

The measurements above were done with the volume knob near the tap, so that the filter was most effective.

Interestingly, the circuit attenuates lower frequencies even when it's "turned off". I wonder if this is intentional, to make the difference in sound more noticeable when you turn it on.

From the viewpoint of circuit analysis, it's pretty complicated to calculate the transfer function analytically without any simplifications. However if you assert that responses of the two capacitors are independent, you get a Bode plot with two poles and two zeros:

\omega_{p1} = \frac{1}{(R_1\|R_2 + R_3) \cdot C_3}
\omega_{z1} = \frac{1}{R_3 \cdot C_3}
\omega_{z2} = \frac{1}{R_1 \cdot C_1}
\omega_{p2} = \frac{1}{R_1\|R_2\|R_3 \cdot C_1}

The first pole and first zero are responsible for the decreased attenuation at the lower frequency range (they disappear when the switch shorts C3), while the second pole and the second zero result in the steady slope towards higher frequencies.

Here's the simulated filter attenuation versus frequency, this time in the more familiar logarithmic scale. I chose the values of the components so that the shape roughly matches the one I measured on the actual circuit (these are also the values written at the schematic above)

Simulated frequency response of DBB circuit

Posted by Tomaž | Categories: Analog | Comments »

Phonebook backup

01.04.2009 13:10

I've been using Sony Ericsson K810i for I don't know how many years. And just when I thought I knew its every feature inside and out it still managed to surprise me.

A couple of days ago I had a discussion with my uncle on how he could back up several hundred valuable phonebook entries on his mobile phone. While talking I was poking around the phonebook menus and found this deeply nested option:

Phonebook → Options → Advanced → Back up to M.S.

M.S. in this case means memory stick. There's also a matching restore option in the same menu.

The phone will only let you do one snapshot, however if you plug the memory stick into a PC you can get the backup file at /system/pim/PB_Backup.vcf and copy it somewhere safer (having a backup on the memory stick in the phone doesn't help you much when your phone gets stolen).

As an added bonus, it's a standard vCard file, so this is an easy way to get a computer-readable export of the phonebook (the alternative is to install the awful and windows-only synchronization software that comes with the phone).

Posted by Tomaž | Categories: Code | Comments »