Python Enum Gotcha

There is plenty of useful information in Why You Should Use More Enums In Python – A gentle introduction to enumerations in Python. After reading it I decided to look into using Enums more. Unfortunately I hit a major Gotcha quite quickly.

Basically, comparisons don’t work with vanilla Enum (unlike IntEnum). Checking the official Python documentation seemed to confirm this understanding:

“Comparisons against non-enumeration values will always compare not equal (again, IntEnum was explicitly designed to behave differently …)” —

In the snippet below, Pieces is subclasses vanilla Enum and has, at least from my point of view, very unexpected results. Pieces2 is based on IntEnum and behaves as might be expected.

import enum

class Pieces(enum.Enum):
    PAWN = 8
    ROOK = 2
    BISHOP = 2

print(2 == Pieces.ROOK) ## False WAT?! Always not equal to non enums
print(Pieces.ROOK == 2) ## False WAT?!
print(Pieces.BISHOP == Pieces.ROOK) ## True
print(Pieces.PAWN == Pieces.ROOK) ## False

class Pieces2(enum.IntEnum):
    PAWN = 8
    ROOK = 2
    BISHOP = 2

print(2 == Pieces2.ROOK) ## True
print(Pieces2.ROOK == 2) ## True
print(Pieces2.BISHOP == Pieces2.ROOK) ## True
print(Pieces2.PAWN == Pieces2.ROOK) ## False

It is easy to imagine this behaviour creating baffling bugs. Interesting.

Shotwell not copying when importing

I use Shotwell for managing my photos and generally it works really well as a replacement for f-spot on my Ubuntu system. My work flow is to put photos in a folder ready to import, import them into Shotwell, and then empty the folder. I discovered very early on that it is important to copy photos rather than accept the default of linking to them otherwise they disappear from Shotwell after the input folder is cleared.

Unfortunately, after an upgrade, Shotwell stopped presenting the option of copy vs link and I imported anyway. Big mistake ;-). It seems to be important to locate the input folder (if using a work flow of add to input->import->clear input) outside of the folder Shotwell is storing its Photos under. E.g. if Shotwell is putting its images under ~/Pictures then don’t have your input folder anywhere like ~/Pictures/images to import.

Fixing the problem was a bit complicated because deleting the imported photos from Shotwell, relocating the input folder so it was outside the Shotwell storage path, and reimporting the images didn’t actually copy them even when the copy option was selected. It seems that Shotwell treats the image as already having been imported and so only does a link. Looking at the source code (yay for open source) gave some useful hints about how Shotwell was actually working esp

The solution was to delete all records from the PhotoTable table in ~/.local/share/shotwell/data/photo.db Sqlite database where the filename field started with the poorly-located input folder. Obviously I copied the database file first ;-). E.g.

DELETE FROM PhotoTable WHERE filename LIKE '/home/username/Pictures/0 images to import/%'

Reimporting worked as it should have after this step. “DB Browser for SQLite” is brilliant, BTW.

USB 2 on VirtualBox

Get the appropriate version of the VirtualBox extension pack installed – I found the correct version in here: Download VirtualBox (Old Builds): VirtualBox 4.2

Add self to vboxusers group as per Set up USB for Virtualbox. Must reboot for change to take effect.

sudo usermod -aG vboxusers

Then follow the additional steps. For Windows XP at least, must add a filter before opening so that the USB is recognised by the File Manager and displayed as an extra drive.

Increase size of VirtualBox image (current state)

I had a Vista image that I foolishly set to 10GB. That was OK until I added the Java Runtime Engine, Eclipse, PyDev, Python, SOFA Statistics, another user for testing purposes (with non-English characters in their user name and home directory) etc. And once I ran the space-greedy system updates, I was out of disk space. A working solution was found in these two posts: Increase/Expand the Size of Windows 7 vdi file on VirtualBox Mac, and Cloning a VM with multiple snapshots?. If you want to copy the whole thing up to a particular snapshot (NB the result with be collapsed into one single VDI, unlike the original), get the hexUUID of the snapshot.

Step 1 – make a new and larger VDI. I did so by making a new virtual machine (testVista) with a new virtual disk image (testVista.vdi). An alternative approach would have been to make the vdi on its own but I couldn’t find a menu item to allow that. It was no great burden doing it as part of making a new virtual machine though.

Step 2 – clone the current state version of the old (and small) vdi. You need to find the hexUUID value of the snapshot you want. See Cloning a VM with multiple snapshots? for details. It gives guidance on how to tell which uuid you need.

The command that worked for me was (the path depends on where you are running the command from and your setup of course):

VBoxManage clonehd 0db42252-9240-4a0a-9393-a7d79778364d "../testVista/testVista.vdi" --existing

Note – testVista was the new VDI I was trying to clone into. It should take a few minutes to run.

Step 3 – disconnect to the old vdi and connect your machine to the larger vdi.

Step 4 – Boot into your system and resize the partition as per Increase/Expand the Size of Windows 7 vdi file on VirtualBox Mac On XP I needed EaseUS Partition. For a Ubuntu guest I downloaded the Gparted iso, added that to storage as a live CD, and booted in. Resized everything and exited. Then unlinked from GParted iso.

Step 5 – Success!

[Update – needed to do this again on another vdi – thus the clarified instructions here]

Telecom USB modem on Lucid Ubuntu

My mother runs Ubuntu (Lucid) on her laptop and she needed to make her NZ Telecom USB modem work. Getting an XT USB Modem working in Ubuntu provided a great starting point but there were a few things I had to do differently. I recommend printing the Rob the Geek article and then following this post for extra guidance.

The key issue is:

[these particular devices] respond to the system by default as a CD drive or USB hub so Ubuntu (with the stick in it’s native form) doesn’t see it as a modem device.
Getting an XT USB Modem working in Ubuntu

Fortunately, I had the option of dual booting into Windows XP so I could modify the modem (thus enabling Ubuntu to detect it as such). The missing step in the documentation for me was that I needed to install the modem under Windows first before I could see it in the Device Manager. In Windows XP, it was Control Panel>Classic View>System>Hardware Device Manager. I discovered the relevant port in my mother’s case was COM5. Yours may be different.

I used Hyperterminal for the next step. It wasn’t exactly intuitive to me but I got it to work. My main problem was I couldn’t see the interface responding as I typed in the command – at least not until it was entered. The command IIRC was AT+ZCDRUN=8

Hyperterminal command

I received:

>> Close autorun
state result (0: FAIL 1: SUCCESS): 1
>> OK

I could then reboot into Ubuntu. The Ubuntu steps (look at Rob the Geek’s instructions) “Just Worked” and the modem just works. I was also successful getting another Telecom XT mobile going for my father-in-law on his eeebuntu netbook. So this was not a fluke.

In conclusion, there was only one small thing stopping everything else from working. Once fixed – complete success and an easy and intuitive user interface.

ACID test for ODF needed to ensure interoperability

The ODF (Open Document Format) Alliance has revealed the many ways Microsoft’s support for ODF interoperability falls sort. See

“Support for ODF represents an important and ongoing test of Microsoft’s commitment to real-world interoperability,” said ODF Alliance managing director Marino Marcich.

I liked the Alliance’s focus on real world interoperability rather than minimum compliance with standards. As has been pointed out numerous times, standards are not designed to make it impossible for a vendor to act in bad faith.

The standard is just fine for any honest company trying to make a product that works. It just wasn’t written as an ironclad legal contract to keep MS from playing dumb and intentionally breaking compatibility.

A valuable suggestion is to make an equivalent of the HTML ACID test. That would help frame performance in terms people could appreciate.


Using cfdisk to prepare a USB HDD to store backups

I bought 2 1TB hard drives to use as backup storage. These couldn’t be mounted immediately because they had not been formatted. So what to do next to prepare them for use?

NB the convention for labelling devices has apparently changed and may differ between kernel versions – so what is true for me now (Intrepid kernel 2.6.27-7) may not apply to you. So double check everything you do. You really do not want to wipe the wrong disk 😉 . I repeat, you really, really, really do not want to wipe the wrong disk. I am not an expert so double check everything.

If using an external hard drive it will be called something like /dev/sdb
Your main HDD will be something like /dev/sda

In older times IDE devices would have been HD … but now it is apparently consolidated on SD …

One test that may be useful is
sudo fdisk -l
My output was:


Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x0005bd91

Device Boot Start End Blocks Id System
/dev/sda1 * 1 120845 970687431 83 Linux
/dev/sda2 120846 121601 6072570 5 Extended
/dev/sda5 120846 121601 6072538+ 82 Linux swap / Solaris

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn’t contain a valid partition table


is also useful for final confirmation

OK now to do the deed (remember earlier warnings about checking):

sudo cfdisk /dev/sdb

cfdisk when no existing partitions

to create New partition
to create new primary partition
to accept the size
Bootable is OK – no harm leaving it selected or selecting it.
Type should be Linux (83)
for Write
yes (not y) to proceed (NB warnings about checking earlier).
It should say it wrote partition table to disk.
for Quit.

If we rerun
sudo fdisk -l
we might get something like:


Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x0005bd91

Device Boot Start End Blocks Id System
/dev/sda1 * 1 120845 970687431 83 Linux
/dev/sda2 120846 121601 6072570 5 Extended
/dev/sda5 120846 121601 6072538+ 82 Linux swap / Solaris

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 121601 976760001 83 Linux


Note that sdb no longer lacks a valid partition table.

Then we need to format the disk (in my case I chose ext3 but ext4 works too):

sudo mkfs.ext3 /dev/sdb1

The system will then write the inode tables. The disk should then be unplugged and replugged to get it to mount.

The new HDD was owned by root so I needed to add some folders and give my user permission to add/edit data etc.
sudo chown username -R /media/disk/
Then open drive and add folders etc

Ubuntu Intrepid (Beta) with Creative Audigy2 and Surround Sound

Set Sound Events and Music and Movies to “Audigy 2 ….. Multichannel Playback (ALSA)”
Then double clicked on speaker icon on top panel – Under Playback Master and PCM controlled overall volume. Under Preferences needed to add Front, Surround, Centre and LFE so the sliders appeared. Set them all quite high except for LFE (about a third volume only or even off or it sounds too boomy etc)
The 6th speaker (rear middle) is still a mystery but we are mainly there 🙂
Hmmm changed a small switch on the back where all the speaker connections came out. Changed 5.1/6.1 to 5.1->6.1 UPMIX. Could that have been the kids? Will check to see if it works the same under XP.

MPG to AVI in Ubuntu Using mencoder

A friend had to get a 120 MPG file down to a smaller size so he could email a message to a friend for a celebration. Ubuntu to the rescue! I found the following:

mencoder INPUTFILE -ofps 25 -ovc xvid -oac mp3lame -lameopts abr:br=192 -srate 48000 -xvidencopts fixed_quant=4 -o OUTPUT.avi

The srate (sound) could be dropped to 16000 if necessary but the big savings were with framerate dropping from -ofps 25 to 15 and the size reduced by adding:


There are likely to be much better ways of doing these things but it worked in this case and the file was 3MB instead of 120MB. The result could be played successfully in VLC (but not necessarily in Windows Media Player or Real Player).

Apache and PHP on Ubuntu – Very Easy Installation

This was very easy – just install php and apache from synaptic and you are good to go. At least for local testing purposes like I am interested in. And if, like myself, you want your test scripts in /home/user/public_html you only need to do the following:

Copy the default website as a starting point.
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mysite
Edit the new configuration file
sudo gedit /etc/apache2/sites-available/mysite
Change the DocumentRoot to point to the new location. For example, /home/user/public_html/
Change the Directory directive, replace <Directory /var/www/> to <Directory /home/user/public_html/>

Now, we must deactivate the old site, and activate our new one. Ubuntu provides two small utilities that take care of this: a2ensite (apache2enable site) and a2dissite (apache2disable site).
sudo a2dissite default && sudo a2ensite mysite
Finally, we restart Apache2:
sudo /etc/init.d/apache2 restart

NB to install support for mysql (mysqli) use synaptic and search for mysqli.