Saturday, December 1, 2007

Tablet Python #1 - Relocatable Software

Python is becoming more and more popular for development on the Nokia internet tablets. It is indeed a very powerful and clear language and makes it easy to write applications that work on all internet tablet versions.

With this post I'm starting a new series on nice Python tricks which might be interesting to tablet software developers. So, if you're doing something in Python on your tablet, this series is for you!

In this first episode I will talk about writing relocatable software. This is software which works no matter where it's installed. There are no hardwired absolute paths to resources in relocatable software.

Knowing where you are

Every Python module knows the path where it's installed. You can retrieve the module's path by reading the __file__ variable.

print "This module resides at:", __file__

This makes it easy to find the directory where your app is installed:

import os
path = os.path.dirname(__file__)

Finding your resources

So if you want to load bundled resources into your application, you know where to find them:

path = os.path.dirname(__file__)
image_file = os.path.join(path, "images", "foo.png")
img = gtk.Image()

Starting the application

This is the way I use to make my programs executable on the tablet. All of the program files and resources are within a subdirectory (e.g. /usr/lib/myapp/) together with the executable start module, which could look like this:

#! /usr/bin/env python

from mediabox.App import App

app = App()

Let's assume this file is /usr/lib/mediabox/MediaBox. Then I make a symbolic link to this file as /usr/bin/MediaBox, and it's all done.

If I move my software to another place, I would just have to update this link.


Luciano Wolf said...

Nice tutorial. I've put a link to this post at PyMaemo (documentation page).

gmmmocom said...

Are you fighting in Final Fantasy XIV for FFXIV Gil or Final Fantasy XIV Gil?
Can you suffer yourself being called newbie in FFXIV Gil game?
Are you seeking unofficial Buy FFXIV Gil cheats or Final Fantasy XIV guides in order to make Final Fantasy XIV Gil faster?
Can you get millions of Cheap FFXIV Gil in one day?
Even if you know how to farm Buy Final Fantasy XIV Gil you have to prepare enough Final Fantasy XIV Power Leveling first to buy height class Final Fantasy XIV Items, to upgrade your Final Fantasy XIV characters.
FF14 Gil Then why not Buy FFXIV Gil from us?
In Final Fantasy XIV it's the fastest way FF14 Gil,for you to get rich. We are online 24 hours a day ready and 7 days one week to power up your FFXIV Gil accounts with FFXIV Gil. Here is the best place for the Final Fantasy XIV Online players to buy your Final Fantasy XIV Gil.
We are the professional website in FFXIV GIL sale.FFXIV GIL here,We are professional FFXIV Power Leveling online. We update price every single day to make sure we are the lowest in the market.(we don't compare price with scam sites which uses unbelievable low price to deceive.)
Our slogan:Cheapest price, Fastest delivery, Best service! Final Fantasy Xiv Gil
In the 2 years we are in this field , FFXIV Gilbuilt many business with tens of thousands of customers. They are very satisfied with our service.So if you want to get a log of Cheap FFXIV Gil ,no doubt ,come to our website to buy. Our customer service is ready for you on line now!(WWW,GM MMO,COM Sell FFXIV Gil)