星期二, 4月 15, 2008

[tips] upgrading to gentoo 2008.0

Well just as usuals, you can upgrades to gentoo 2008.0 quickly. (beta?)

#eselect profile list
Available profile symlink targets:
[1] default-linux/x86/2006.1
[2] default-linux/x86/no-nptl
[3] default-linux/x86/2006.1/desktop
[4] default-linux/x86/2007.0
[5] default-linux/x86/2007.0/desktop
[6] hardened/x86/2.6
[7] selinux/2007.0/x86
[8] selinux/2007.0/x86/hardened
[9] default/linux/x86/2008.0
[10] default/linux/x86/2008.0/desktop *
[11] default/linux/x86/2008.0/developer
[12] default/linux/x86/2008.0/no-nptl
[13] default/linux/x86/2008.0/server
[14] hardened/linux/x86

For my case, just type 'eselect profile set 10'
and emerge world again.

I'm not migrating to OpenRC and baselayout2 yet, however I'm using KDE4.0.3 now, so I think I have enough un-stable-ness to worry about. ;0

[tips] using pyrex to do the argv[0] trick in linux using python.

A trick to rename python process in output of 'top' and 'ps'.


set_argv.c:

#include
#include
#include

int get_argc(){
int argc;
char **argv;
Py_GetArgcArgv(&argc, &argv);
return argc;
}

void set_argv_via_number(int num,char *str){
int argc;
char **argv;
Py_GetArgcArgv(&argc, &argv);
strncpy(argv[num], str , strlen(str));
memset(&argv[num][strlen(str)], '\0', strlen(&argv[num][strlen(str)]));
}


set_processname.pyx:

#Warning: this module is not safe and may involve some dirty hacks.
import sys,dl,glob
cdef extern int get_argc()
cdef extern void set_argv_via_number(int num,char *buf)
def set_argv0(thename):
set_argv_via_number(0,thename)

def set_argv(thename):
argc = get_argc()
print argc
for i in xrange(argc+1):
if i==0:
set_argv0(thename)
else:
pass
def set_name(thename):
if sys.platform == 'linux2':
# Set process name. Only works on Linux >= 2.1.57.
try:
libc_loc = glob.glob("/lib/libc.so*")[0]
libc = dl.open(libc_loc)
libc.call('prctl', 15, thename, 0, 0, 0) # 15 is PR_SET_NAME
except:
pass

def set_processname(thename):
space_padding_num = len(sys.argv[0])-len(thename)
if space_padding_num < 0:
#thename is longer than original name
newname = thename[:len(sys.argv[0])]
else:
#thename is shorter. add padding.
newname = thename + " " * space_padding_num

set_name(newname)
set_argv(newname)

compile.sh:
#!/bin/sh
CC=mipsel-linux-gcc

pyrexc set_processname.pyx
$CC -c -fPIC -I/usr/include/python2.5/ set_argv.c
$CC -c -fPIC -I/usr/include/python2.5/ set_processname.c
$CC -shared set_argv.o set_processname.o -o set_processname.so

[tips] building psyco on non-x86 platform. (ie. linux mips little endian)

The trick is to build psyco in ivm mode. The mode slower than the native mode(ivm mode involves a prolog generated-vm, you guess), however it's the only way to compile psyco on non-x86 system. And cross-compiling with distutils is not a trivial stuff, so I just compile it directly.


mipsel-linux-gcc -pthread -fno-strict-aliasing -DNDEBUG -fPIC -DALL_STATIC=1 -Ic/ivm -I/usr/include/python2.5 -c c/psyco.c -o build/temp.linux-mipsel-2.5/c/psyco.o
mipsel-linux-gcc -pthread -fno-strict-aliasing -DNDEBUG -fPIC -DALL_STATIC=1 -Ic/ivm -I/usr/include/python2.5 -c c/platform.c -o build/temp.linux-mipsel-2.5/c/platform.o
mipsel-linux-gcc -pthread -shared build/temp.linux-i686-2.5/c/psyco.o build/temp.linux-mipsel-2.5/c/platform.o -L/usr/lib -lpython2.5 -o build/lib.linux-i686-2.5/psyco/_psyco.so


This generates the C-module extension, and just copy all of py files to correct path and enjoy psyco speed-up! (well, not quite a case for me. It just use way too much memory on our embedded system. But I think maybe it'll useful in other project.)

ps: I should mentioned that I'm doing this on a uclibc linux system.

[tips] if as a pure function in python

http://okmij.org/ftp/Scheme/if-function.txt

here is a trick that we could rewrite a customized not-a-special-form IF in python.


iftrick.py:

#!/usr/bin/env python
def func_true():
print "true!"

def func_false():
print "false!"

def my_if(my_cond,then_func,else_func):
eval((then_func, else_func)[not my_cond].func_code,vars())

condition = True

my_if(condition,func_true,func_false)

condition = False

my_if(condition,func_true,func_false)

===
#python iftrick.py
true!
false!

星期三, 2月 06, 2008

Am I getting old or ....The lenny is coming this year???

根據 http://lwn.net/Articles/267722/的說法,

下一個版本的 debian (lenny) 將在今年9月release.

這可真是出乎我預期的快... etch... 不是去年四月才出的嗎?

**<小孩勿看>** 大便 難道真的落屎了嗎? **< / 小孩勿看>**

然後話說 /bin/sh link to dash 有了ubuntu帶頭衝之後 還真的debian就跟進了.

我的預言(詛咒)成真?! 看來lloyd大又要怨恨了 :P

(話說 難道再下版inittab也是難逃upstart之手了嗎??? ...有待下回茅房分解...)

這一行果然是很難混的啊.... XD

還有python 2.5.X 啥時要給我進default啊... 時間也該到了吧...

星期四, 1月 31, 2008

Arc is released. and is it a Blub?

http://arclanguage.org/

這兩天幾乎網路上所有lisp programmers都在討論Arc,
這個著名lisp hacker, Paul Graham所發明的lisp dialect, 一度被認為是vaporware的語言,在前天release了.

Paul Graham對於lisp社群的貢獻是無庸致疑的,
但這次他所公開release的Arc則是讓不少人大失所望.
畢竟這是他從他首次宣稱Arc is a better lisp以來已經過了6-7年之後釋出的版本,
"just another lisp dialect"似乎是不少lisp programmers的一致意見,
另外此次版本不支援unicode只支援ascii也引發大波瀾.
我自己最近時間不多, 但也小試了一下, and just didn't 'get' it.
我當然理解PG的"Syntax Do Matters",
但我也不完全同意less typing是higher level programming language唯一的路,
非常希望時間能證明, 這只是我們這些 "Blub" programmers的愚昧, 而Arc is indeed a better lisp and a better programming language.

註: Blub是PG發明的字眼. 在著名的Beating the Averages一文裡用來回答一個有趣的問題: "如果lisp這麼好,為什麼都沒人用?" 而描述出一個所謂的Blub弔詭的情境:
Blub是任一種介於比lisp低階 而比machine language高階之間的一個一般強度(power)的程式語言. (比如: python,java,ruby,perl,C/C++,haskell,ML-family,erlang,php,javascript,fortran,cobol.....anything not in lisp-family)
Blub programmer則是Blub programming language的支持/使用者,
Blub programmer的想法是, Blub語言 擁有某些特性X,
他無法想像更低階語言怎麼能夠沒有特性X而能拿來認真的使用.
同一時間他則認為Blub語言已經擁有所有他所需要的特性, 覺得所有比Blub語言更高階的特性全是無用而浪費時間的.

註2: 我的標題意思是Paul Graham在文章裡說他自認為他的Arc語言已經好到可以讓他不想用CL或scheme才release了, 是否也是陷入了Blub弔詭裡?

btw: you need patches to run current Arc on latest mzscheme(352+) or you're on windows(sadly),
see http://arclanguage.org/item?id=319 and http://jfkbits.blogspot.com/2008/01/digging-into-arc-in-24-macros-or-less.html

星期一, 1月 07, 2008

Python: programming language of 2007!

January Headline: TIOBE declares Python as programming language of 2007!

"""
Python has been declared as programming language of 2007. It was a close finish, but in the end Python appeared to have the largest increase in ratings in one year time (2.04%). There is no clear reason why Python made this huge jump in 2007. Last month Python surpassed Perl for the first time in history, which is an indication that Python has become the "de facto" glue language at system level. It is especially beloved by system administrators and build managers. Chances are high that Python's star will rise further in 2008, thanks to the upcoming release of Python 3.
"""

well, the reason is so obvious to me...it's all about gravity thingy ;)

星期四, 12月 13, 2007

python耶~~

今天同事跑來問我mips上有否uc版本的python,
一問之下原來是某版子的tv tuner附的sample application是日本人用python寫的, 沒python的話沒辦法試.
也剛剛好我也才剛編完mips uc版本的python 2.5.1沒多久,
所以就剛好直接拿過去用.
不過嚇我一跳,
以前都是得主動出擊之外還要忍受冷嘲熱諷,
這可是第一次有embedded業務上的code是要求"必須"有python的.
:D

星期五, 12月 07, 2007

設定 Logitech G5 in Linux(xwindow)

因為lloyd大大說他可愛的老婆說, 『不要因為便宜而買,然後嫌棄壞掉,還不如買好一點,用久一點來的划算』,於是就買了一支logitech的g5滑鼠, 但是linux下沒有windows上的setpoint driver, 所以就自己來了.

一插上就會自動有的功能包括左右中鍵滾輪上下跟硬體dpi設定,
不過滾輪向左向右水平滾動的功能未啟動,
另外大姆指鍵要自己做一些設定以符合自己的需求.

首先是/etc/X11/xorg.conf的基本配置


Section "ServerLayout"
Identifier "Default Layout"
Screen 0 "Screen0" 0 0
InputDevice "G5" "CorePointer"
InputDevice "Keyboard0" "CoreKeyboard"
EndSection

Section "InputDevice"
Identifier "G5"
Driver "evdev"
Option "Name" "Logitech USB Gaming Mouse"
Option "ZAxisMapping" "4 5"
Option "Emulate3Buttons" "no"
EndSection

再來是啟動大姆指鍵,將Xwindow中鍵貼上(2)功能bind在大姆指鍵(8), 此外預設的滾輪左右(67)是顛倒的(括號中的數字表按鍵編號):
~/.xmodmap

pointer = 1 8 3 4 5 7 6 2 9 10 11 12 13 14 15 16 17 18 19 20

最後則是啟動滾輪向左跟向右,讓他可以直接在我慣用的gnome terminal跟firefox裡移動分頁. 並在gnome-terminal裡將滾輪中鍵貼上的功能換成剪貼簿的貼上(即一般的Ctrl+V而非大姆指key的xwindow中鍵貼上)

~/.xbindkeysrc.scm

(xbindkey '("b:6") "/usr/bin/xvkbd -xsendevent -text '\\[Control_L]\\[Page_Up]'")
(xbindkey '("b:7") "/usr/bin/xvkbd -xsendevent -text '\\[Control_L]\\[Page_Down]'")
(xbindkey '("b:8") "/usr/bin/xvkbd -window gnome-terminal -xsendevent -text '\\[Control_L]\\[V]'")


另外還有在firefox中調整滾輪的速度, 首先先在網址頁輸入about:config,
然後找到mousewheel.withnokey.sysnumlines 設為false
mousewheel.withnokey.numlines則設為6行或你喜歡的數字.

如果以下3個套件沒裝的話要另外裝:
x11-misc/xbindkeys x11-misc/xvkbd x11-drivers/xf86-input-evdev
(xbindkeys最好將guile的use flag打開, 若需要預設設定檔可以用xbindkeys -dg > ~/.xbindkeysrc.scm 來初始化 真的看不習慣scheme/lisp的人可以考慮使用基本的config: xbindkeys -d > ~/.xbindkeysrc, 當然scheme設定檔會稍微彈性一點)

另外xmodmap ~/.xmodmap跟xbindkeys要設成在打開xwindows時啟動.

星期三, 12月 05, 2007

新手: 你怎麼飛的??? 答: import antigravity

這句可能會成為Python Programmers的本月(年?)最經典.

此句的出典為本月在駭客圈裡非常知名但原本推崇perl語言的XKCD網站漫畫 (xkcd的作者昨天學了python, 注意看的話會發現Tooltip上還寫著Perl, I'm Leaving you.):




隨即有網友作了T-shirt:



加上 TIOBE Programming Community Index 的本月頭條: December Headline: Python surpasses Perl for the first time in history.
這個月似乎python推廣的好消息不斷. (或是perl ain't cool anymore?!)

所以,

下次記得老闆要叫你"""飛給他看"""的時候,

python可是連反重力模組都幫你寫好了.

;)