I wanted to move this blog to blogger for more flexible css and java script manipulation, so I wrote this py-w2b project to do that. So far I’m able to transfer blog posts, but not comments, since every comment posted by an authenticated session will become my own comments instead of the original authors. It’s trivial to use so I’ll skip the introduction here.
October 27, 2008
August 20, 2008
bug in python-opengl + mesa
Since like forever, I have never succeed in executing python-opengl demos on debian lenny. It always segfault during glutInitDisplayMode. After some debugging, here is why.
One should be able to call glGetError at any time. If there’s nothing wrong, it should just return something like GLNOERROR. Any raw (native) function call in python-opengl, no matter it belongs to GL, GLU or GLUT, will always call glGetError right after each call to check for error. I think this is wrong since functions like glutInitDisplayMode have NOTHING to do with glGetError. With mesa 7.0.3-5 in my system, the call to glGetError without glInit will cause segfault. I check the same thing on Ubuntu, which has an older version of mesa and python-opengl, and it does not happen.
python-opengl enabled error checking by default with OpenGL.ERROR_CHECKING, which is set to True in OpenGL/__init__.py. This code snippet can disable it:
import OpenGL OpenGL.ERROR_CHECKING = False # import other stuffs such as OpenGL.GL, etc.
or you can just modify the __init__.py to disable it by default.
Given the current status of python-opengl and low level x protocol support in python, I think the best language to do 3D in FOSS world will still be pure simple C.
June 19, 2008
generate object methods at runtime
I have been working on a dialer button class since yesterday. It makes sense to use the command design pattern here, and I want to separate the commands and the buttons so I can change the functionality of every button at runtime.
So we are talking about something like this:
class DialerButtons:
def __init__(self):
self.command_table = [
self.numpad_1, self.numpad_2, self.numpad_3,
self.numpad_4, self.numpad_5, self.numpad_6,
self.numpad_7, self.numpad_8, self.numpad_9,
self.cancel, self.numpad_0, self.dial]
def numpad_0(self):
self.text.append('0')
def execute(self, n):
self.command_table[n]()
<snipped>
and you can use this class like this:
dialer = DialerButtons() dialer.execute(0) dialer.execute(8) ....
Now obviously define numpad_0 to numpad_9 is a boring task. What happens if you need to define numpad 0 to 99? So, I came out with this code piece:
@classmethod
def _numpad_commands_factory(cls):
for n in xrange(0, 10):
setattr(cls, 'numpad_%d' % n, lambda self: self.text.append(str(n)))
...
DialerButtons._numpad_commands_factory()
This way you initialize DialerButtons AFTER you start the program and make methods numpad_0 to 9 on the fly. At least that’s what I was trying to do. However, it didn’t come out as I expected. Every numpad method will just add ‘9′ to self.text, instead of the respective ‘0′ to ‘9′. Why?
The reason is that the context of numpad_0, for example, is actually “f(self): self.text.append(str(n)))” instead of “f(self): self.text.append(’0′)”. so, what it does here is that it refers to the variable n inside _numpad_commands_factory, and the value of n is 9 after you executed it.
The correct code piece is:
@classmethod
def _numpad_commands_factory(cls):
def f(chr):
return lambda self: self.text.append(chr)
for n in xrange(0, 10):
setattr(cls, 'numpad_%d' % n, f(str(n)))
This way we can evaluate the value of str(n) first, then generate the appropriate function and assign it to numpad_n.
June 14, 2008
python-efl on ubuntu gutsy
hey if you have a extremely slow laptop (Pentium III) like me yet you still want to use python-efl on it, here is how.
first, compiling the whole e17 from scratch is not an option. it will take forever. there’s a ubuntu package repository for e17:
$ cat /etc/apt/sources.list.d/e17.list deb http://e17.dunnewind.net/ubuntu gutsy e17 deb-src http://e17.dunnewind.net/ubuntu gutsy e17 $ sudo apt-get update $ sudo apt-get install efl-dev libecore-imf-evas-dev
this can save a lot of time.
the other problem is that the latest cython release (0.9.8) does NOT work with python-efl. you have to install 0.9.6.14 instead. don’t forget to install python-pyrex as well.
now you’re all set, get python-efl.
cvs -d :pserver:anonymous@anoncvs.enlightenment.org:/var/cvs/e login cvs -z3 -d :pserver:anonymous@anoncvs.enlightenment.org:/var/cvs/e co e17
it’s under e17/proto/python-efl. another funny gotcha is that the binary packages I installed were built on 20080309. so the latest python-efl will not build successfully.
cvs -z3 update -dP -D 20080309
fixs this.
the default build-all.sh under python-efl directory builds evas ecore edje emotion e_dbus epsilon. that’s too many. for me I just need evas ecore edje e_dbus.
September 20, 2007
A small tip for PIL
PIL stands for Python Imaging Library. It is fairly useful when it comes to handling images under python. However, it comes with a pet peeve to me: the show() method of a Image object will execute sh xv <imagefile>I don’t have xv installed on my system, neither do I want to since it’s shareware.
Now I really don’t know why I haven’t came out with this idea before: simply create a symbolic link /usr/local/bin/xv and link it to the display utility that comes with the common image handling package imagemagick then everything will work just fine.
September 13, 2007
Hello World?
Guess this is appropriate for a blog about software.
>>> print "Hello, world!" Hello, world!