Continued Fractions for Representing Real Numbers

This is something I learned at EuroPython 2011. I think it came up in a lightning talk by Alex Martelli, but I don’t recall exactly.

Continued fractions are a representation of real numbers that allows for arbitrary-precision arithmetic.

If you’ve worked with floating-point numbers in Python (or most any other programming language, for that matter), you are aware of precision problems like this:

x = 1.0 / 3.0
total = 0.0
for i in range(300):
    total += x
print repr(total)
print total == 100.0

This prints 99.99999999999966, not 100.0. The reason is that the IEEE 754 floating-point representation of 1/3 isn’t exact, and this (initially small) error accumulates 300 times.

Continue reading Continued Fractions for Representing Real Numbers

Live Desktop Streaming via DLNA on GNU/Linux

TWiT and the Ubuntu terminal on a TV set via DLNA

Many modern TVs (and set-top boxes, gaming consoles, etc.) support DLNA streaming. Suppose you have a PC that stores all your music, downloaded podcasts, video podcasts, photos, and so on. You can run some DLNA media server software on your PC and stream your entire media collection to your TV over your home network. No more carrying around USB sticks, it’s all in your home cloud.

On GNU/Linux, I am using MediaTomb as my DLNA server. It’s nothing fancy (it’s a file server, after all), and it just works.

Okay, this takes care of media files stored on your PC. But can we do more? Is it possible to stream a live capture of your desktop to the TV?

Let’s say you’re watching a Flash video in your browser, and there’s no way to download the video file. Or, you’re watching a live event being streamed via Flash or whatever. It would be kinda cool to be able to stream that to your TV via DLNA. And it’s possible—not trivial, mind you, but I’ve seen it working at least once…

Continue reading Live Desktop Streaming via DLNA on GNU/Linux

Tutorial: Scaling Processing for Android Apps to Different Screens

In my previous article, I gave an introduction to the built-in features of the Android platform for supporting screens of various sizes and densities. In this article, I am going to expand on this and show you actual code for achieving screen-independence in an app. My example will be a Processing app (as that’s my own primary use case), but the ideas should apply equally well to any game or graphics-centric app.

“Chimpzilla Attacks!” mockup (no, I’m not serious)

Let’s use the game “Chimpzilla Attacks!” as an example. I made up this game specifically for the purpose of this tutorial—and already spent way too much time on the mockup. I have no idea what the game mechanics are, but judging from the cheesy graphics, it’s got to be some kind of “Punch The Monkey” knock-off. Anyway, back to the tutorial…

Continue reading Tutorial: Scaling Processing for Android Apps to Different Screens

Multiple Screen Sizes With Processing for Android

Samsung Galaxy Tab (Android Virtual Device)

In my ongoing effort to port a desktop Processing application to Android, I am now trying to add support for multiple screen sizes. The goal is that the same .apk works on, say, a 3.7″ Milestone equally well as on a 7″ Galaxy Tab and on a multitude of other screens. The Android Dev Guide has a comprehensive page that explains the platform features for supporting different screens. I did my own tests and experiments to better understand the concepts. This article explains my findings and hopefully saves you some time and work.

Continue reading Multiple Screen Sizes With Processing for Android

Sound Playback in Processing for Android

I added sound playback to the Processing app that I’m currently porting to Android. On the desktop, I used the Minim sound library, which is not supported on Android (although there is some discussion about alternatives and a possible port). All I really need from a sound library is a way to play several .ogg (or .mp3 or .wav) files simultaneously and asynchronously, query their playback durations, maybe set the playback volume of individual sounds, and stop playing sounds at any time.

The class is just fine for that.

In general, a sound can be played using the following code:

snd = new MediaPlayer();

My own app is a game that contains only a handful of .ogg files. I keep a HashMap of the MediaPlayer instances for all sounds in the game, with prepare() already called on them. Then I simply call start() whenever an event triggers the sound in the game.

Continue reading Sound Playback in Processing for Android

Building Debug Versions from Source Packages

To build a debug version of exactly the package that is installed in your Debian-based GNU/Linux distribution (tested on Ubuntu 10.10):

apt-get source rhythmbox

This does not require “root” access. It will simply download the source package to the current working directory.

To build the package, you might need developer libraries of dependency packages:

sudo apt-get build-dep rhythmbox

To configure the package so that it builds a debug version that does not replace the installed package, I used:

./ --prefix=/home/devel/rhythmbox-0.13.1/install CFLAGS="-g -O0"

All that is left to do is run make:

make -j 4
make -j 4 install

Android Development Without Eclipse

Following up on my “First Steps” article, I did some more Processing development on Android. I ran into some problems, solved some, and was able to start my ported Processing program inside the emulator.

This article is still not about actual Processing source code. All I did was fiddle with the Android SDK tools some more.

Building Without Eclipse

Eclipse is kind of a resource hog (yeah, I know, “I told you so” just ain’t saying it). Therefore, I took a closer look at the command line tools from the Android SDK to replace it (at least part of the time).

To convert an existing Eclipse Android project to an Ant project that you can build from the command line, use the “android” tool from the SDK:

android update project --target <android-target> --path <path-to-project>

You can see a list of possible values for the <android-target> parameter with the command “android list targets”:

Continue reading Android Development Without Eclipse

First Steps with Processing for Android

I got into mobile development recently. I ported an SDL-based game to the Openmoko FreeRunner and to an iPhone 3GS. Now I’m planning to do some Android development on a Motorola Milestone (the European version of the Droid).

I have previously been using the Processing programming environment on the desktop, so I was delighted to learn that it is now available for Android.

(“Processing is an open source programming language and environment for people who want to create images, animations, and interactions.” It won a Prix Ars Electronica award in the “Net Vision / Net Excellence” category in 2005.)

I am by no means an Android or Processing expert. This article serves mainly as documentation for myself about the steps to build a simple application. If it is also useful for others, so much the better.

Continue reading First Steps with Processing for Android

Stitching Panoramas With Hugin and Some GIMP Post-Processing

Here’s how I created the panoramas that I posted to my GUADEC 2010 photo album (on Flickr and on Picasa).

I used the Hugin panorama photo stitcher and did some post-processing using GIMP. Hugin should be available in the repositories of your GNU/Linux distro, and it’s dead simple to use.

These are four pictures I took at the intersection of Grote Halstraat, Hoogstraat, and Gravenstraat in The Hague with a camera phone. I made sure that the pictures have enough overlap. (The Hugin tutorial recommends 20% to 30% overlap.)

Continue reading Stitching Panoramas With Hugin and Some GIMP Post-Processing

Introduction To New-Style Classes In Python

Orignally published: May 8, 2005

Table of Contents

  1. Why New-Style Classes?
  2. Properties
  3. Static Methods
  4. Class Methods
  5. Descriptors
  6. Attribute Slots
  7. The Constructor __new__
  8. Cooperative Super Call
  9. Conclusion
  10. References

Why New-Style Classes?

New-style classes are part of an effort to unify built-in types and user-defined classes in the Python programming language. New-style classes have been around since Python 2.2 (not that new anymore), so it’s definitely time to take advantage of the new possibilities.

Continue reading Introduction To New-Style Classes In Python

Python and C++, GNU/Linux, computer stuff…