Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, May 19, 2009

Python surprises - scope of variables

Yesterday while debugging one place in some function I found one "surprise" in Python - language that I thought was designed to avoid surprises as much as possible(Principle of least astonishment).

if 1 == 1:
    print dir()
    found = True
    print dir()

print dir()
print "found:", found

Output:
['__builtins__', '__doc__', '__file__', '__name__', '__package__']
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'found']
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'found']
found: True


As you would have guessed coming from C++, here we had variable used out of its scope. But in Python variables live in special dictionaries - one is global where global variables and other stuff are stored and another one is local, for keeping local symbols inside the function. So all variables you create inside "for" loop or "if" scope will be stored in that local dictionary and will only be removed from there when Python's garbage collector decides.

That surprise cost me 30 minutes of wondering why some polygons on a map suddenly went nuts and lost their shapes :)

Wednesday, June 25, 2008

Yandex plugin for Maemo mapper

After patching maemo-mapper application to support Yandex maps Max Lapan, their employee suggested his script that worked as CGI script and converted mmapper's links to yandex's ones. That was really awesome idea as there is no need to update application and contacting its developer all the time, we can just run script on 127.0.0.1 and pass parameters like city ID and x/y/z to it. Note, that Yandex is not that simple as other map vendors - there is no world map with all cities on it. Each city has its own ID and its own set of dimensions - Xs,Ys of upper left and bottom right corners of each rectangle. So I think we(interested people from Russia and Ukraine) will have to create list of supported cities with verified set of dimensions and IDs. So each city will have it's own repository in maemo mapper(see screenshot below).

I'm open to discussions about this implementation as GPS and other features are unlikely to work seamlessly and road traffic service is currenly unsupported. It's not possible to add it without patching maemo-mapper itself. But for me and probably for other people map is the most important feature.

How to setup a new repository:
URL for the map is "http://127.0.0.1:8000/?map=NUMBER&layer=1&x=%d&y=%d&z=%d"
where NUMBER is a special code for each city(Kiev=1600, Moscow=2000, St.Petersburg=500, and so on)
Download Zoom Steps: 1
View Zoom steps: 1
Downloadable zooms: min: 6, max: 14

So then we may download map for offline use as usual. I use 11,9,7 zoom levels + 6 for city center. It's about 50mb on a flash. It's better to move data file from "/home/user/MyDocs/.documents/Maps" to somewhere at /media/mmc* and create a symlink instead.

Python script can be downloaded following this link. To run simply put it somewhere in home dir and:

$ python yandexmap.py

Roadmap for the script:

  1. Configuration file with a set of cities, dimensions, etc...
  2. deb package in extras repository
  3. install file on http://maemo.org/downloads unless I receive too many complaints about how stupid my script is.
  4. Easy way of running script - probably it will be launched by maemo-mapper's .desktop shortcut and killed when mmapper exits.
Screenshots(clickable):






Thursday, May 15, 2008

Labyrinth - bugfix release

It was too early to release the first version of Labyrinth without proper checking. Even considering "alpha" status of the project. The issue was found almost immediately, though I did not have a time to fix it.

Pygtk in OS2008 and in SDK 4.0 does not support method `to_string` for gtk.gdk.Color, only attributes: "pixel", "red", "green" and "blue". The method is used to store RGB color values like 127/64/0 in hex text "#007f0040000". Can someone advise why it's not working on maemo? The web site says "This method is available in PyGTK 2.12 and above.", however OS2008 contains:


>>> import gtk
>>> gtk.ver
(2, 14, 0)

This was the workaround for the issue:

def color_to_string(color):
return "#00%02x00%02x00%02x" % (color.red, color.green, color.blue)

Now as the saving is fixed the only porting task left is maemo's Virtual Keyboard support ;)

Tuesday, March 18, 2008

Back to 80s - console pacman game

Some time ago I had an interview for AI developer and the task was to write a Pacman game. I was not in mood to create a nice GUI or pretty 2D/sprites graphics, the main thing to do was to show ghosts running after the player. Used a standard non-blind path-finding algorithm for this. Here is the screenshot of the game(it's ugly, I know!):

Actually that's not the first game I've ever made. The first one was "Mine Sweeper" written in BASIC when I was 14. And I can assure you python is just awesome! May be anyone can remember GOTO and GOSUB/RETURN operators. Now having all these OOP, exceptions, libraries it's like damn nightmare. I definitely will prefer python to BASIC when teaching my children ;)

The source code(in python, of course) can be found following the link