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 :)

Monday, February 2, 2009

Amazon sells N810 almost for free!

Hi all,

Well, "free" in this case is actually $227, but anyway it means that those of us who can't wait until Maemo5-equipped device is released can have something right now.

I've been following the news about buy.com prices, but it was not available for non-US people. Amazon fixed this injustice :)

Friday, December 19, 2008

Nice feature of snprintf()

Hi all,

Yesterday I learned a feature which I'm sure should be described in every C book. To be honest, 'man snprintf' describes it as a part of C99, but I've never read the article completely.

Often when you construct a string using 'snprintf()' you need to allocate enough space for the buffer. But it's not possible to know how much you may need, so usually we just do something like this:


char buf1[1024];
/* or */
char *buf2 = malloc(1024);

which is not correct even if you are 100% sure that you string is just a "Hello world". Btw, you are wasting your stack space in first example.

So the trick here is to call 'snprintf()' twice:

int len = 1 + snprintf(NULL, 0, "%s, %s\n", "Hello", "world") ;
char *buf = malloc(len);
snprintf(buf, len, "%s, %s\n", "Hello", "world");

snprintf's behavior in C99 is to allow buffer to be NULL, but return number of bytes that would have been written to buffer. Note, second parameter should be zero too :)

Thursday, July 17, 2008

Fring for Maemo

Hi all!

Great news - Fring is now available for maemo platform. Fring is an application that combines a number of protocols such as Skype, ICQ, GTalk into one handy program. You may find it following this link. Here is the screenshot I took from Fring's russian blog. It should work for both chinook and diablo. The announce says about N810, but I'll give it a try tomorrow on my N800.

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, June 19, 2008

Maemo mapper + Yandex maps

Guys, I need map!

After googling for Maemo mapper format I found only this very useful link at internettablettalk with the list of supported servers. But unfortunately it's not that simple for us, Ukrainians. Google Maps does not have streets map even of our capital, Kiev. Russian yandex's map service is still the best, but its format is not that simple as google's:

Yandex Maps:
http://maps.yandex.ua/map.xml?mapID=1600&mapX=%d&mapY=%d&scale=%d
http://maps.yandex.ua/map.xml?mapID=1600&mapX=3397917&mapY=6491641&scale=4&slices=1

mapsID - city's id(from the set of cities)
mapX/Y - special coordinates in wild format:
Map is a 2^31 2-D array, coordinates are central point + shift * 2^(zoom level+8)

Google Maps:
http://mt.google.com/mt?x=%d&y=%d&zoom=%d
http://maps.google.com/?ie=UTF8&ll=50.352471,30.256348&spn=4.052146,12.44751&z=7

Very simple: long, lat and zoom :)

That's why is not that simple with Maemo mapper.

May be in a few days I'll test and polish what I got now and create special .deb package for Yandex-only mapper, because there are some changes now which break all other map repositories.

Below is a screenshot(click to see 800x480) of what's working at the moment. To be continued!

Tuesday, June 17, 2008

Arcanoid

3 days ago I was forced to leave civilized city and go to the place without Internet(which is 95% of Ukraine, I think). My precious N800 is a perfect toy to spent time with. What I wanted then is to have simple, easy, old, ancient, forgotten. Arcanoid game and I did not find it in . Only via ZX Spectrum emulators, etc...

It's a pity. Had to play Bomberman and NumptyPhysics again all day. So I promise to all who read this. I'll find native arcanoid/krakout and port it to maemo. Because it's classics. It's like to have Windows without solitaire :)

Any idea of what to look for first?