Friday, December 19, 2008

PyGTK natively on Mac

I recently found out that the GTK+ GUI widget set had been ported to run natively (i.e. without the use of X11) on Mac OS X. Information about this project can be found on this page. Now, I like GTK+ but life is too short to be coding in C, so I imediately started looking into the possibility of using PyGTK together with this native Mac version of GTK+ and that is indeed possible. To get a fully functional version of GTK+ and PyGTK installed onto your Mac follow the instructions on Imendio's page and then after finishing the installation add the following command:
jhbuild build pygtk

This will build libglade, pygtk, pycairo and some other stuff and install the necessary files into your site-packages folder of your local Python.

Building all of this from source is a lengthy process, and I have therefore created a .dmg image (Intel or PPC) of a GTK+-with-PyGTK that you may use as a shortcut :-) Using this approach will give you a fully functional native PyGTK to play with in less than two minutes.

Friday, December 5, 2008

Building PythonCE from source

I have recently had the need to compile PythonCE from source, which turned out to be a quite complex task. Therefore I have decided to write this blog post, so that others may avoid wasting the many hours that I wasted trying to get it to build.

My setup for building PythonCE was this: Windows XP SP2, Microsoft eMbedded Visual C++ 4 with SP4, Pocket PC 2003 SDK, and Python 2.5.

To actually build the thing you need to download the PythonCE source, unzip it, enter the PCBuild/WinCE directory, and typing “scons”. This failed on my machine though, which I later found was due to a missing registry key in my Pocket PC 2003 SDK install (or something of the likes). To get it to compile i edited the file PCbuild/WinCE/msevc.py and made a small correction to the get_msevc_paths function - this is shown below. The addition I made was in the except-clause, where I manually set the exe_path.



def get_msevc_paths(env):
...
try:
value, value_type = SCons.Util.RegQueryValueEx(hkey, 'Path Dirs')
exe_path = str(value.decode('utf_16_le').rstrip(u'\x00'))
except (SCons.Util.RegError, SCons.Errors.InternalError):
exe_path="C:\\Program Files\\Microsoft eMbedded C++ 4.0\\"\
"EVC\\wce420\\bin;C:\\Program Files\\"\
"Microsoft eMbedded C++ 4.0\\Common\\EVC\\Bin"

Tuesday, August 19, 2008

Type checking in Python

I love working with Python, and most of the time I love the fact that it is a dynamically typed language. But, sometimes type checking would be nice to have; especially when debugging larger projects. Therefore I decided to look into how function/method decorators could help me there, and I very quickly found that it is possible to implement simple type checking quite easily using such decorators. So I have written a small Python module that adds the possibility of doing run-time type checking on functions and methods. The code for this module is here:

def decorator_with_args(decorator):
def new(*args, **kwargs):
def new2(fn):
return decorator(fn, *args, **kwargs)
return new2
return new

@decorator_with_args
def typecheck(fn, *decorator_args):
def new(*args):
if len(decorator_args) != len(args):
raise Exception('Wrong number of arguments given to\
decorator.')
for x in range(0, len(args)):
if type(args[x]) != decorator_args[x]:
raise TypeError('Argument %i is of wrong type.\
%s expected, %s received.'%\
(x+1, str(decorator_args[x]),
str(type(args[x]))))
return fn(*args)
return new

The reason that two decorators are created in the above code is, that the typecheck decorator is decorated by the decorator_with_args decorator to enable the decorator to take arguments - which is not otherwise possible.

Using the typecheck decorator is as simple as:

@typecheck(int, int)
def add(x, y):
return x+y

Now, whenever the add function is called, it will be checked whether the arguments x and y are indeed integers, and if not a TypeError exception will be thrown.

Feel free to copy the code into your own projects - consider it released under a BSD licence :-)