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:

1
2
3
4
5
6
7
8
9
10
11
12
13
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]()

and you can use this class like this:

1
2
3
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:
1
2
3
4
5
6
    @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:

1
2
3
4
5
6
@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.

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:

1
2
3
4
5
$ 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.

1
2
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.

1
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.

git first, git-svn later.

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

1
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

1
2
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.

fonts disappeared

I’m using debian testing (lenny) and after the latest update my xserver no longer display fonts. This is not totally true because I can still see the fonts on gdm, but after logging in, no fonts whatsoever. It turns out the i810 Xorg driver is messed up and I have to set NoAccel to true to avoid it.

1
2
3
4
5
6
7
8
Section "Device"
Identifier "Intel Corporation 82Q963/Q965 Integrated Graphics Controller"
Driver "i810"
BusID "PCI:0:2:0"
Option "NoAccel" "true"
# Option "AccelMethod" "EXA"
Option "XAANoOffscreenPixmaps"
EndSection

BTW I can use compiz without NoAccel before if I delete the device id from the blacklist in compiz start up script.

我果然不懂 C 啊。

from gcc-4.2.info:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
5.34 An Inline Function is As Fast As a Macro

<..snipped..>

If you specify both `inline' and `extern' in the function definition,
then the definition is used only for inlining. In no case is the
function compiled on its own, not even if you refer to its address
explicitly. Such an address becomes an external reference, as if you
had only declared the function, and had not defined it.

This combination of `inline' and `extern' has almost the effect of a
macro. The way to use it is to put a function definition in a header
file with these keywords, and put another copy of the definition
(lacking `inline' and `extern') in a library file. The definition in
the header file will cause most calls to the function to be inlined.
If any uses of the function remain, they will refer to the single copy
in the library.

Since GCC 4.3 will implement ISO C99 semantics for inline functions,
it is simplest to use `static inline' only to guarantee compatibility.
(The existing semantics will remain available when `-std=gnu89' is
specified, but eventually the default will be `-std=gnu99'; that will
implement the C99 semantics, though it does not do so in versions of
GCC before 4.3\. After the default changes, the existing semantics will
still be available via the `-fgnu89-inline' option or the `gnu_inline'
function attribute.)

GCC does not inline any functions when not optimizing unless you
specify the `always_inline' attribute for the function, like this:

/* Prototype. */
inline void foo (const char) __attribute__((always_inline));

So, consider the following program:

gcctest.c:

1
2
3
4
5
6
7
#include "inline.h"

int main()
{
puts(externinline());
return 0;
}

inline.h:

1
2
3
4
__inline__ extern char *externinline()
{
return "inline";
}

lib.c:

1
2
3
4
char *externinline()
{
return "extern";
}

The behavior of the executables will be different with or without the -O option of gcc. Compile it without -O, the program will print “extern”. Compile it with -O, the program will print “intern”.

利用 ssh 連到 NAT 內的電腦

在 NAT 內的電腦 (暫名為 A) 上下指令:

1
autossh -R :1234:localhost:22 my.server.com
  1. autossh will automatically reconnect
  2. -R :1234:localhost:22 意思是連到 my.server.com,在 my.server.com 上頭開 port 1234 回連到 A。
  3. my.server.com 上頭 sshd_config 必須要設定 GatewayPorts clientspecified or yes

然後在外頭的 linux 電腦連到:

1
ssh -p 1234 my.server.com

這樣就可以連回 A 了。如此這般就算沒有 VPN 也可以在家工作。

git-svn

像 subversion (svn) 這類集中式的 scm 一定要有網路連線才能 commit ,這是很討厭的一件事情,尤其是很多人都會用 notebook 工作,不見得需要長時間連結網路,或者是並沒興趣修改 svn 上的版本。這時候像老牌的 monotone 或是現在的 git 以及 Mercurial 這種分散式的版本管理系統就很好用。這陣子在改一個 project,他已經有 svn 在 googlecode 上,看來已經很久沒人 maintain. 我當然還不需要 svn write 權限,但是我仍然希望在自己 local 端有版本管理,這樣我如果幹了什麼蠢事才救得回來。

此時 git-svn 這種工具就真的很好用。基本使用方式是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Clone a repo (like git clone):
git-svn clone http://svn.foo.org/project/trunk
# Enter the newly cloned directory:
cd trunk
# You should be on master branch, double-check with git-branch
git branch
# Do some work and commit locally to git:
git commit ...
# Something is committed to SVN, rebase your local changes against the
# latest changes in SVN:
git-svn rebase
# Now commit your changes (that were committed previously using git) to SVN,
# as well as automatically updating your working HEAD:
git-svn dcommit
# Append svn:ignore settings to the default git exclude file:
git-svn show-ignore >> .git/info/exclude

日後如果我想要把修改 commit 到 upstream 去,我在這段時間的修改記錄仍然可以保留。相當有彈性的作法。

DPI果然好用啊。

話說不知道從哪次更新之後,notebook的字型就一直很鳥,調到12號字還是有點小,更加上還很醜,因為其實很多相對應的效果都是針對一般預設10號字左右去調的。剛剛才想到,應該是因為DPI差太多的原因吧,於是拿出尺量了一下螢幕的大小,然後在xorg.conf中加入DisplaySize的設定:

1
2
3
4
5
6
7
Section "Monitor"
Identifier "Generic Monitor"
Option "DPMS"
HorizSync 28-49
VertRefresh 43-72
DisplaySize 245 185
EndSection

嘿,果然搞定。嚴格說起來,在不同的顯示器底下,理想狀態應該是同一個theme只要調整DPI以後都可以得到相同的視覺效果。Microsoft Windows在一開始就忽略掉DPI的設定,還把它誤導成「調整字型大小」用,實在不是很好的觀念。