Friday, December 19, 2008

FilePick

A couple days ago one wise guy said me that software cannot be perfect, so it is never too early to share it. That's a good point, really.

I decided to start sharing my stuff I wrote for myself and by myself even though I'm not quite satisfied with its quality or the style or whatever else.

The first piece is a standalone File Open dialog. It's not quite my idea--I saw something similar on somebody's Mac. I can't even describe how much time this little thing saves me on such a routine operation as opening a file in the text editor. Definitely more than I spent writing it.

I'm a Komodo IDE user and I'm pretty happy with it, except the File Open dialog, which sucks. So I wrote a macro adding FilePick to Komodo IDE. I could do the same for Vim if I used it more or even (God forbid!) to Eclipse, but only if I was forced to use it.

The program is actually a tiny Python/Tkinter script. It has no dependencies except Python, of course.

It looks like this:


Initially it show the whole list of files in the directory specified by the command line parameter. Then you type in few letters and it makes the list shorter. Then you select the file, click Enter and that's it. It rarely takes more than 10 letters to locate a file in the project I'm working on (about 5000 files).

Grab FilePick here:
http://www.bitbucket.org/mikeivanov/filepick/src/

P.S. Bitbucket is just GREAT!

Wednesday, December 17, 2008

Just get rid of threads

I just couldn't resist the urge citing this:
...
Python could be better off doing what tcl does. Allow each process to embed multiple interpreters; run each interpreter in its own thread. Implement a fast message-passing system between the interpreters (e.g. copy-on-write by making communicated objects immutable), and Python would be closer to Erlang than Java.


I thus think the main offender is the thread and threading modules - not the GIL. Without thread support in the interpreter, there would be no threads. Without threads, there would be no need for a GIL. Both sources of evil can be removed by just removing thread support from the Python interpreter. In addition, it would make Python faster at executing linear code. Just copy the concurrency model of Erlang instead of Java and get rid of those nasty threads. In the meanwhile, I'll continue to experiment with multiprocessing.
...

http://mail.python.org/pipermail/python-dev/2008-December/084265.html

I can't agree more.

Why Tk matters

Tk probably is one of the most underlooked GUI toolkits. It is a nice small toolkit which is really, I mean REALLY simple to use.

Tk by the way, has something common with Vi. Oh no, it's not that it beeps all the time as possessed; I meant it works everywhere. I bet you can find Tk ported to any single platform having a GUI and it will work consistently on all of them.

Tk is ubiquitous. Guess what? Almost certainly you have Tk already installed on your computer. Got Python? Go look into /usr/lib/python2.5/lib-tk, it's right there!

How to use it? It's very easy. Look, here is a 'Hello, world' program in Python (using a Tk binding called Tkinter):
from Tkinter import *
Label(text='Hi there').pack()
The program apparently pops up a window with some text inside. Yes, it's that trivial. And this is the area where Tk shines: quick, small GUI tools.

Yet Tk is very powerful. People create big sophisticated systems using just this toolkit. The most prominent example is definitely AC3D, a 3D modeling program.

Tk, however, has some issues. I think nobody will argue that Tk looks ugly on the Linux platform. While the Windows and Mac versions have native look, the Linux port looks shokingly disgusting. Some work is being done in this department but it's not quite completed.

Somebody might ask why use Tk if there is wxPython. Well, for the same reason why there are bicycles and airplanes. They are good for different purposes. Tk is way more lightweight and much easier to learn and use than wxPython.

Tk is very well documented. Here is the official Tkinter documentation with links to tutorials and such:
If you're going to learn Tk, it's worth to mention that Tk is actually a part of something called Tcl/Tk. Tcl stands for Tool Command Language, a Shell-like scripting language easy to learn and fun to use. Even though all you want might be just Tkinter, some knowledge of Tcl will be rewarding.

Tuesday, December 16, 2008

How to minimize the street garbage?

The ultimate answer is here: http://semorfe.com/jw/.

This is a really simple game I wrote just to prove the browser platform is good enough for action games. It won't run Crysis though, but for simple 2d toys it's just perfect.

From the technical point of view it is a 100% pure DHTML, no Flash, no Canvas. It works in IE6/7. I used Prototype library because it really makes JavaScript a little bit more pleasant language.

Well, that's about it.

Enjoy the game.

Tuesday, July 1, 2008

Installing Tsearch2 on Postgres 8.2

Before we start I assume you know how to create new PostgresSQL databases and have access to the root and postgres system accounts.

Among other new shiny features, PostgreSQL 8.3 has Tsearch2 as a contrib package; that is, full text search is available in 8.3 out of the box. That’s really awesome news because enabling it manually on 8.2 databases can be tricky. If you have any chance to upgrade to 8.3, do it. This also makes sense because the latest version runs much, much faster.

Tsearch2 is a part of the postgresql-contrib package. Assuming your operating system is Ubuntu, simply run sudo apt-get install postgresql-contrib and that’s it.

We need a database to start with. It’s really a good idea to try it first on a separate test database. If something goes wrong, you always can drop it and create a new test database without losing any valuable data.

So, let’s create our test database:

$ createdb -E utf8 test

I always use utf8 as the database encoding unless I have a really good reason not to do that. Using utf8 will save you lots of time and probably some gray hair. Trust me, utf8 is your friend; I’ve learned that the hard way.

Now, if you are a happy 8.3 user then all is ready to go. If for some reason the upgrade is not possible, you will have to configure your databases manually to enable Tsearch2.

This is how. First, enable plpgsql, one of the stored procedure languages:

$ sudo -u postgres psql test -c "create language plpgsql"

Now, create service Tsearch2 tables and all necessary supporting functions, data types, etc.

$ sudo -u postgres psql test < /usr/share/postgresql/8.2/contrib/Tsearch2.sql

You have to do both things as a postgres user because otherwise you will get a bunch of permission errors. Unfortunately, all the new objects will be owned by the postrgres user, which makes them essentially unusable by anyone else. To solve this problem, use Eugene Morozov’s grantall.sh script.

#!/bin/sh
# Usage: grantall database role
OBJECTS=$(psql -t -c '\dt' $1 | cut -f 4 -d ' ')
OBJECTS="$OBJECTS $(psql -t -c '\ds' $1 | cut -f 4 -d ' ')"
OBJECTS=$(echo $OBJECTS | sed 's/ /, /g')
SQL="GRANT ALL ON $OBJECTS TO $2"
psql -c "$SQL" $1
psql -c "GRANT ALL ON SCHEMA public TO $2" $1

This script is so useful, so I’d suggest saving it as ~/bin/grantall.sh. Now make it executable:

$ chmod a+x ~/bin/grantall.sh

Then run it.

$ sudo -u postgres ~/bin/grantall.sh test user

User is your database user name, which almost certainly matches your system user name.

Doing this every time you want to enable full text search in a database can be annoying. There is a neat trick that can help. When PostgeSQL creates a new database it actually makes a copy of a special template database, template1 by default. If you install Tsearch2 there, every time you create a new database, it will automatically copy Tsearch2 objects from there.

Unfortunately, the copied objects will retain their original permissions, which means even though you installed Tsearch2 into the template1 database, you still have to run the grantall.sh script.

Now it’s time to start a PostgreSQL command line. Let’s say ‘test’ is the database’s name.

$ psql test
Welcome to psql 8.3.1, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

test=>

Let’s check that it is working properly.

test=> select to_tsvector('simple', 'full text search') @@ to_tsquery('simple', 'text');
?column?
----------
t
(1 row)

We’re there, Hurray!