用PIL生成图片真是太简单了

April 11th, 2009 firenet Posted in python | No Comments »

今天早上起来没啥事,折腾了下LSP,然后就开始研究火星时间,算法挺简单的,就是放在虚拟主机的时候有些问题。londit.cn的fastcgi不会用,fastdomain.com的PIL库又没有,真郁闷。只好跳过,去看了看如何把计算结果生成图片,用PIL库挺简单的。代码的HTML版本在这儿

#!/usr/bin/evn python
# -*- coding:utf-8 -*-
import sys
import Image
import ImageDraw
import ImageFont
def generate(god="Stra"):
    im = Image.new('RGB', (300, 100), 0xFFFFFF)
    draw = ImageDraw.Draw(im)
    string = "Orz %s..." %(god)
    font = ImageFont.truetype("/usr/share/fonts/win/Consolab.ttf", 16, encoding="unic")
    draw.text((50,20), string, fill=0x444444, font=font)
    draw.text((100,50), "You are our god!", fill=0x333333, font=font)
    draw.text((150,80), "--A rural worker", fill=0x222222, font=font)
    del draw
    im.save("a.png")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        generate()
    else:
        generate(sys.argv[1])

emacs daemon

March 31st, 2009 firenet Posted in emacs, linux | 4 Comments »

最近,我开始在启动gdm的过程中使用emacs –daemon在后台运行一个emacs server,使用emacsclient -c打开一个图形化的frame,或者emacsclient -t在终端内打开一个frame,退出当前frame使用C-x 5 0。除了启动的时间稍微长一点以外,确实很方便。

但是,我最近发现了一个问题,有时候我没有关闭emacs server就直接关闭gdm甚至关机,仿佛有些工作被直接跳过了,比如 ido-save-history,显然这样很不好。

今天早上来到俱乐部,没什么事儿,于是研究了一下,基本解决了问题。

在 .emacs中自定义两个函数:

(defun my-kill-terminal ()
(interactive)
(ido-save-history)
(save-buffers-kill-terminal)
)
(global-set-key (kbd “C-x C-c”) ‘my-kill-terminal)
(defun my-kill-emacs ()
(remove-hook ‘kill-buffer-query-functions ’server-kill-buffer-query-function)
(remove-hook ‘kill-emacs-query-functions ’server-kill-emacs-query-function)
(save-buffers-kill-emacs)
)

第一个函数重定义了C-x C-c的关闭terminal函数,首先保存ido-list;第二个函数取消两个hook,使得emacsclient 关闭emacs server时,不需要两次确认。

然后就是需要找个地方,定义gdm关闭时的动作,加入运行脚本。

google 了一下:在debian中,/etc/gdm/PostSession/Default脚步会在gdm关闭时被运行

于是加入:

su username -c “emacsclient -e \”(my-kill-emacs)\”"

这样就可以以username身份运行emacsclient了。


xrandr:简单的linux双屏工具

February 23rd, 2009 firenet Posted in linux | 5 Comments »

原来一直在windows下用双显示器,最近在俱乐部有条件享受双显示器的待遇了,研究了一下linux下的双显示器配置,真的好简单啊。只需要添加红色的字段到/etc/X11/xorg.conf中,重启X。

Section “Screen”
Identifier  “Default Screen”
Monitor     “Configured Monitor”
SubSection “Display”
Virtual 2048 2048
EndSubSection

EndSection
然后在命令行下输入

xrandr –output LVDS –left-of VGA –dpi 96就可以了,需要说明两点:

  • 我的电脑是X61,外接一个1024*768的液晶显示器,上面的分辨率等参数是针对我的机器而设定的。
  • 最先使用命令的时候,我没有设置dpi参数,导致部分程序的字体看着很别扭,尤其是emacs23中的xft字体,完全和以前的不一样的观感。设置dpi参数后,正常。

Firefox addon: Context Search

February 17th, 2009 firenet Posted in 小记 | No Comments »

我给自己做了一个在线代理,方便访问国外网站,使用着还算凑合,就是有个问题,用起来太繁琐了。复制URL,打开代理页面,粘贴URL,回车,真的太复杂了。

今天下午就想争取简化一下过程,回寝室就看了一会儿greasemonkey,用些麻烦,不懂DOM和javascript……我就想在右键菜单加入一个按钮,一点击选中的URL就自动编码通过代理访问。搜索了一会儿,嘿嘿找到了Context Search,通过右键菜单才选择搜索引擎,稍加动点儿手脚,在/usr/share/iceweasel/searchplugins目录中添加一个firenet.xml(点击下载),就可以了。

使用方法:

  • 选中要访问的URL,右键在弹出的菜单中选择 Search for “……” -> firenet就会在后台新tab打开URL
  • 或者在地址栏 f空格URL就会在当前tab打开URL

Python Challenge

December 27th, 2008 firenet Posted in python | No Comments »

Python Challenge is a game in which each level can be solved by a bit of Python programming.

很久以前就看到bbs上有人推荐这个,一直没有时间做,今天比较闲,找出来看了看,过了level 0和level 1,感觉还是挺有意思的。

level 0 不用写代码 直接 2**38就可以得到答案。

level 1 代码如下:

#!/usr/bin/env python

fd = open('Q1.text')
data = fd.read()
lenth = len(data)
result = []
for i in range(0, lenth):
    if data[i].isalpha():
        if data[i] == 'y':
            result.append('a')
        elif data[i] == 'z':
            result.append('b')
        else:
            result.append(chr(ord(data[i]) + 2))
    else:
        result.append(data[i])

    i += 1

print ''.join(result) 

有一点和推荐做法不一样,推荐用string.maketrans方法, 但是string类仿佛用得越来越少了,我都没有接触过这个,没办法,山寨做法了……


Spaces instead of Tabs

December 9th, 2008 firenet Posted in emacs | 1 Comment »

在写程序的时候,许多人,包括我,都习惯于以一个tab进行缩进,但是这样有一个很严重的问题,每个人可以定义不同的tab宽度,这样,相同的程序会呈现不同的效果。所以,很多编程规范建议我们采取空格做缩进,但是一下子敲入4个,8个空格实在太烦人了。

Emacs可以帮助我们……

(setq c-basic-offset 4)
(setq tab-width 4)
(setq indent-tabs-mode nil)

这样设置以后,缩进为4个字符,tab-width的宽度为4,在c-mode中 按一下tab可以缩进两个空格,而C-q C-i可以插入一个制表符,宽度为4。解释一下,最后一条语句的效果是禁止在缩进中插入制表符。如果没有这一句,可能是两种情况:

  1. c-basic-offset >= tab-width:如果这样设置,比如 c-basic-offset == 4 , tab-width == 2,这样emacs就会插入两个tab进行缩进;c-basic-offset ==6 , tab-width == 4 ,此时emacs会插入一个制表符,两个空格。也就是说emacs会优先插入制表符。
  2. c-basic-offset < tab-width: emacs 按照 c-basic-offset指定的宽度缩进,全部插入空格。

所以,如果我们期望缩进宽度和制表符宽度一致,必须加入最后那条语句。
还有一点需要说明,多数mode都会使用c-basic-offset这个变量来控制缩进,但是python-mode是个例外。

(setq py-smart-indentation nil)
(setq tab-width 4)
(setq-default py-indent-offset 4)
(setq indent-tabs-mode nil)

python-mode使用py-indent-offset来控制缩进,其他的原理同上。


Note about s2mem and s2disk under Linux

October 5th, 2008 firenet Posted in linux | No Comments »

With the help of thinkwiki, s2mem cost a little time to work fine. If your computer resume immediately after s2mem, you should try:

modprobe -r uhci_hcd

modprobe -r ehci_hcd

This remove the modules which would awake the computer aumatically. Surely, after your computer resume, you must load these modules for your USB devices.

S2mem is easy and S2disk is not.

First, I got a error “write error: no such device” and i didn’t understand it . Which device is needed? Once I reboot the computer and found a message “Unable find swap signature”. Oh , I havn’t allocate swap space.

Such a stupid error. So

mkswap /dev/sda7

swapon -v /dev/sda7

OK, let me try

echo disk > /sys/power/state

Now, my computer can suspend. But when I press the power button to resume, it boot as like it never suspend. Why?”Unable find swap signature” reminds me. I should modify /etc/initramfs-tools/conf.d/resume. It should be like this: RESUME=/dev/sda7. Then

update-initramfs -u

OK, after these, my computer suspend and resume fine.^_^


Got my first notebook thinkpad x61!

September 27th, 2008 firenet Posted in linux | No Comments »

Yestaday,  I got my first notebook thinkpad x61. It has a T8100 CPU, 2GB memory and 250GB HD. (You can find more detail at here. The first one is my type.) 9:25a.m, I got to the Harbin’ Branch of Digital China. There are two nice boys and they help me finish the process for getting the computer. Nearly one hour later, I got back to my school with the thinkpad.

After lunch, I started to deal with the system: Windows Vista is there but I don’t like it. I prefer to linux, whick i am more familiar with. I use Debian lenny at usual time and you know I spend much time to configure it as I wish. So I want to move the whole system on my PC to the thinkpad. For that, I follow these steps:

  1. First of all, I pack the system as a file, a big file. Use this command: tar cvpzf backup.tgz –exclude=’/proc/*’ –exclude=/lost+found –exclude=/ba
    ckup.tgz –exclude=’/mnt/*’ –exclude=’/sys/*’ / . It would create a file named backup.tgz at the root directory.  Wait a while^_^.
  2. Then copy the file to the partition which I got ready for linux root directory at the X61. I used Acronis software to split the partition and size it 50GB and format it to ext3 fs. After the preparation, I copy the file to the partition. How to copy? you have many choices. i.e. , under Windows Vista you can use usb device or LAN to copy the file to NTFS fs partition(fat32 is not supposed because 4G filesize limit is end~), then use ext2ifs or other software to move the file to the ext3 partition. But I didn’t do it like that. I use a use disk whick is bootable. I installed slax over the usb disk. After booting from the usb disk, mount the ext3 fs, copy the file to it. Very easy.
  3. The last step, even easier! Pack the big file, and you can guess it will cost some time. Use the command: tar xvpfz backup.tgz -C /. After that you should fix the grub. Everyone can do it!. Get to the directories which you packed and find the command–grub. Run it and get into the grub command interface. Then “root (hd0, 5)” and “setup (hd0)”, at last “reboot”.Notice : here I supposed the ext3 fs locate on /dev/*da6.

Finish the three steps and you can boot linux at your X61~Even better, the system is configured as you wish!Of cource, some drivers are different between your X61 and PC.That’s next……


ntfs-3g的效率如何?

September 16th, 2008 firenet Posted in linux | No Comments »

还是virtualbox崩溃引来的破事儿,重新安装了virtualbox 2.0非OSE版,接着就是安装windows 2003。随手抓了一张03的安装光盘,安装成功后再安装Office 2k3,令人郁闷的事情发生了:virtualbox提示文件大小超出限制~刚开始还没有反应过来,以前都是这么安装的呀?后来突然想到,我把虚拟机文件放到了fat 32分区,查看文件大小,确实4G了。以前安装成功,估计是那个版本的windows 2003精简过~无奈,格式化为ntfs,考虑到还需要在windows下访问这个分区。用ntfs-3g挂载分区后,开始安装。不知道是不是ntfs-3g的效率问题,CPU占用很高,速度还很慢,copy文件后的配置,平时20分钟就能完成,这次40分钟也搞不定。尝试了好几次,没办法,绕过这个问题吧,搞成ext3。如此这般,5次安装2003后,终于成功了。


Search for proxies via python 续

September 16th, 2008 firenet Posted in python | No Comments »

前几天virtualbox崩溃了,一直使用的花刺代理自然也不能使用了。教育网没有代理可怎么活?没办法,只好自己搞定了。上网搜索了一下,找到一个网友的代码,拿过来,稍微修改了一下,给经常使用的代理更高的优先级,添加保存所有的下载代理功能(原来的脚步只会下载验证成功的代理)。放在46上,好像还能用~