Sunday, December 30, 2007

Tablet Python #2 - Resource Modules


In this second episode of Tablet Python, I'm going to tell you about an easy way to make resource modules. A resource module is a Python module which you can import for loading resources such as images, fonts, or whatever you want.

For example:

import graphics

img = gtk.Image()


In episode #1 we talked about writing relocatable software. We are going to apply the same technique for resource modules, since they will be relocatable as well.

Our resource modules are Python packages, i.e. a subdirectory with a file in it. The name of the directory will be the module's name. Be careful to only use valid characters (characters valid for Python variables) in the directory name and the filenames of the resource files (e.g., my_icon.png is OK, while my-icon.png would be not!).

Let's assume the following filesystem structure for our example:

+ icon.png
+ logo.png
+ background.png

The package directory will also contain the resource files. In order to access them, we'll have to put them into the module namespace. Write the following into graphics/

import os
import gtk

_RESOURCE_PATH = os.path.dirname(__file__)

def _load_resources():

# Find all .png files in the resource directory. This construct is called
# "list comprehension" and will be covered in detail in episode #3.
# This returns a list of the names of all files in the resource directory
# ending with ".png".
resources = [ f for f in os.listdir(_RESOURCE_PATH)
if f.endswith(".png") ]

# load resources into module namespace
for r in resources:

# the filename without extension will be the name to access the
# resource, so we strip off the extension
name = os.path.splitext(r)[0]

# this is the full path to the resource file
path = os.path.join(_RESOURCE_PATH, r)

# Now we can load the resource into the module namespace.
# globals() gives us the dictionary of the module-global variables,
# and we can easily extend it by assigning new keys.
globals()[name] = gtk.gdk.pixbuf_new_from_file(path)

#end for

# load the resources when importing the module

That's all. Let's test it with a tiny test program

import gtk
import graphics

win = gtk.Window(gtk.WINDOW_TOPLEVEL)
img = gtk.Image()


You may put as many resource files as you want into the resource directory and simply access them by their name. The files are loaded only once when the module gets imported the first time. Subsequent import statements reuse the module without reloading the resources. I'm going to talk about the "singleton" nature of Python modules in a later episode.

A more sophisticated example of resource modules can be found in the theming engine of MediaBox (the subdirectory called theme).


Anonymous said...

Weekends to peopleig2tmean that they can have a two-day wowgold4europe good rest. For example, people gameusdcan go out to enjoy themselves or get meinwowgoldtogether with relatives and friends to talk with each storeingameother or watch interesting video tapes with the speebiewhole family.
Everyone spends agamegoldweekends in his ownmmoflyway. Within two days,some people can relax themselves by listening to music, reading novels,or watchingogeworld films. Others perhaps are more active by playing basketball,wimming ormmorpgvipdancing. Different people have different gamesavorrelaxations.
I often spend weekends withoggsalemy family or my friends. Sometimes my parents take me on a visit to their old friends. Sometimesgamersell I go to the library to study or borrow some books tommovirtexgain much knowledge. I also go to see various exhibition to broadenrpg tradermy vision. An excursion to seashore or mountain resorts is my favorite way of spending weekends. Weekends are always enjoyable for me.
igxe swagvaultoforu wowgold-usaignmax wowgoldlivebrogame thsaleGoldRockU

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)