Asleep from Day

June 19, 2008

generate object methods at runtime

Filed under: programming, python — John @ 3:17 pm

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

Filed under: python — John @ 4:10 am

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.

June 6, 2008

Intel onboard graphic chip + compiz

Filed under: Linux hardware — John @ 3:06 pm

it’s a really bad combination. not so long ago the driver changed default from XAA to EXA, thus my fonts disappeared. after the last update, now compiz just keeps telling me it cannot load cpp plugin and fails to start.

guess the compiz guys put this chip(8086:2992) in their blacklist for a reason. damn, I really need compiz!

June 5, 2008

git first, git-svn later.

Filed under: scm — John @ 5:18 pm

the whole story is that I started a project locally and I used git as my SCM. now I’m going to put it into a svn repository with full history but I still want to use git.

normally you should make this decision at the beginning. that means you
git-svn clone http://svn.somewhere.com/myproject
first, then you use git as usual, do git-svn rebase and git-svn dcommit. you have to do this because git-svn must know where to start, namely you should have at least one git-svn-id in your git log to start with.

here is how I add svn support into an existing git repository. basically it’s easy, you just
git-svn init http://svn.somewhere.com/myproject
git-svn fetch

now git branch -r should tell you there is a branch called git-svn. you git rebase git-svn your current master. if it succeeded then you’re all set.

however, in order to do this, there must be a point back in time that these two branches are the same. if it’s not the case, you’re in trouble. you have to use git-svn set-tree to force a svn commit to be your starting point. in my case, the svn repository started out empty, so I forced the first commit in my git. after that git rebase succeeded like I expected.

Blog at WordPress.com.