soledad penadés
repeat 4[fd 100 rt 90]

Common GUI design mistake, fixed in Ubuntu

20080514 by sole

Common GUI design mistake, fixed in Ubuntu

He ought to be happy now!

Split files into folders by letter

20080513 by sole

I had a lot of files in one folder. It is not very practical to browse the folder that way, so I decided to create a little script which would split the files into different folders, using the first letter of the file for naming the folder, as in a, b, c, d… but using Python this time!

import os
import shutil
import sys

if len(sys.argv) > 1:
        folder = sys.argv[1]
else:
        folder = '.'

for item in os.listdir(folder):

        full_path = os.path.join(folder, item)
       
        if os.path.isdir(full_path):
                continue
       
        dst_folder = os.path.join(folder, item[0].lower())
       
        if not os.path.exists(dst_folder):
                os.mkdir(dst_folder)
               
        shutil.move(full_path, os.path.join(dst_folder, item))

Download from svn (this should always be the most up to date version)

To use simply go to your favourite terminal window and type

python split_files_into_folders_by_letter.py /path/to/messy/folder

It will pick the first letter of each file, create a folder with the lowercased letter (if it doesn't exist yet) and move the file to the folder. It won't move folders! (because of the os.path.isdir check).

If there's any pythonista in the audience with ideas for improving this, feel free to leave your suggestions in the comments, thanks! :-)

Note

There wasn't any special reason for not doing it with Ruby; I simply wanted to know what the differences would be. And while the semantics aren't very different (e.g. where it says os.path.join we would use File.join in Ruby), there is a huge difference between them and it's called documentation. Python documentation is hosted in Python's website, it is reviewed (and mostly written too) by Guido van Rossum (Python's author) and it's up to date and very easy to browse and read. Whereas Ruby docs were hosted elsewhere until very recently, are a pain to browse and pretty much a dump of automatically generated docs from source, which I find very uncomfortable to use. It still feels a little bit weird to use python's indenting style but I'm really liking it. A pity there isn't a Hpricot in python :-)

Bye bye, Burnaby! Hello, Laurel!

20080504 by sole

\o/

If you're reading this, it means my new server is working properly

It's been a long process, there were several websites to migrate and there were several issues too. First, the old data is in utf8 but the database is latin-1, which I solved with some smart scripting to update text and char columns. Second, and a bit more tedious, the database for this website was in a quite disastrous state. I suspect from a risky move from myself: I dared to use a trunk version of wordpress past year and it messed a bit with the categories, but I didn't notice until it was a tad late.

With the latest split from categories into terms, taxonomies and relationships WP2.5, it just went even worse. There were several empty terms, some taxonomies weren't related to any object at all, but their count was not zero and so they were shown in the list and when you accessed them, you got a nice 404 error, etc etc. That was relatively easy to fix with a bit of scripting again. And to top it all, some posts had lost their categories! So, lots of fun adding them again!

I had also been toying with a new theme which would use all the latest css goodness bits from newer browsers such as Safari and … er … Konqueror? Box shadows aren't implemented in Firefox yet so that basically ruins my theme idea. I also wanted to have gravatars, but plain gravatars aren't very surprising per se. I finally decided that if I couldn't have shadows and nice effects, then I would have a lo-res theme: only two colours, gravatars included. So if you leave comments here and you have a gravatar associated with your e-mail address, it will be abducted and reduced to a black and white dithered image which nicely fits with this spartan theme. Isn't it great?

I thought I would miss the old server (Burnaby), but I changed my mind as soon as I played with the new one. The admin interface is better and more intuitive, the response times are shorter and the technology is also newer (a shared accelerator versus the old shared hosting scheme). Hopefully there won't be any downtimes or they will be also shorter.

Hope that explains the lack of updates and activity in my online presence of late. And now I want to see your dithered faces in the comments ;-)

Building a universal library in Mac OS X

20080421 by sole

If you need to use a library in a universal binary you'll need to use a universal library too.

Let's use FMOD as an example; libfmod.a is the PPC compiled version, libfmodx86.a is the intel version.

For combining both into a universal library:

lipo -create libfmodx86.a libfmod.a -output libfmod-universal.a
ranlib libfmod-universal.a

In the case of FMOD they provide you the with precompiled static libraries (the .a files). But if you build libraries from source code, there's a magic compiler option called -arch which will help you in this. Unfortunately I don't have an example handy right now. I'll put it whenever I find it again, but it roughly goes on the lines of -arch i386 -arch ppc :-)

I'm not sure that naming a library with something as *-universal.a is a good idea, specially if you have code which uses -Llibraryname for linking libraries — I guess you might need to change that parameter. I am explicitly adding the libraries to the project in XCode so it's working fine for me.

Suggestions and better practices are welcome!

Mental note about #ifndef's

20080408 by sole

When using #ifndef for #include guards:

#ifndef BLAH_H
#define BLAH_H

// code

#endif

make sure that #ifndef and #define are the first lines of the archive!

I had placed some comments before them and that made me lose several hours, trying to find out why class forward declarations didn't work as they should. One could expect the compiler to ignore comments automatically but that's not the case.

I also looked for something as #pragma once for GCC, since I remembered that from when I used Visual C++, but although it was supported at some time, it is now deprecated, so the only solution is placing the #ifndef where it must be (at the very beginning).