30 Apr 2009

Python Tips - python流程图生成

中文ubuntu论坛python社区anticlockwise的文章(原文)
转载请注明版权.

This is a python flow chart drawing implementation.





#!/usr/bin/env python
# -*- coding: utf-8 -*-

# flow_parser.py
# author: Rongzhou Shen
# date: 2009-02-11

from pyparsing import *

# PYPARSING DEFINITION =========================
LSQUARE, RSQUARE, SEMI, ASSIGN, QUOTE = map(Suppress, '[];="')
LBRACE, RBRACE, AT, LBRACK, RBRACK = map(Suppress, '{}@()')

# Definition part of the flow_def file
node_id = Word(alphanums + "_")("id")
node_type = Word(alphas)("type")
declaration = node_type + LSQUARE + node_id + RSQUARE
value = QuotedString('"')
definition = Group(declaration("declaration") + ASSIGN + value("value") + SEMI)

# Flow part of the flow_def file
flow = Group(node_id + OneOrMore(Group('=>' + node_id)) + SEMI)

# Grammar definition of the whole file
flow_impl = OneOrMore(definition)("definitions") + OneOrMore(flow)("flows")
flow_def = AT + LBRACK + QuotedString('"')("flow_name") + RBRACK + LBRACE +\
flow_impl("flow_impl") + RBRACE
# END PYPARSING DEFINITION ==============================

#sample = """
#@("A test flow_chart") {
#    process[node1] = "This is a test process";
#    process[node2] = "This is another test process";
#    a => b => c;
#    ffew => fweji;
#}"""

def test_parse():
expected = ['process', '[', 'node1', ']', '=', '"',
'This is a test process', '"', ';']
test_def = 'process[node1] = "This is a test process";'
data = definition.parseString(test_def)
assert len(data) == len(expected)
assert all([x == y for x, y in zip(expected, data)])

expected = ['a', '=>', 'b', '=>', 'c', ';']
test_flow = 'a => b => c;'
data = flow.parseString(test_flow)
assert len(data) == len(expected)
assert all([x == y for x, y in zip(expected, data)])


用法如下:

Usage:

@("This is a test graph") {
process[p1] = "This is a test process";
process[p2] = "This is another process";
condition[c1] = "This is a condition";

p1 => p2;
c1 => p1;
c1 => p2;
}


结果如下:

The flow chart generated is:


Python Tips - python IDLE自动补全

python 2.6.2在windows下好好的自动补全,
到了ubuntu下就没有了作用,但是ipython这个改进的交互式python解释器则可以.
方法如下:

If you want to use auto complete function in ubuntu, you need to install ipython like:



sudo apt-get install ipython


--------------------------------------
ubuntu904+VMware+python2.6,尚未测试成功下面这个方法.
另外一个方法就是麻烦点,每次开python之后输入:

Another method is a little bit complicated:
(This methods hasn't been tested)

>>>import readline, rlcompleter
>>>readline.parse_and_bind("tab: complete")


保存为~/.pythonstartup.py之后,加入~/.bashrc

Then add it to your ~/.bashrc file:

export PYTHONSTARTUP=~/.pythonstartup.py

29 Apr 2009

Ubuntu Tips:安装vm tools

vmware虚拟ubuntu9。04之后出现一些问题,
对vm tools不再支持,安装之后下次重启,依然要求你继续安装,
发现是安装有问题,google得到的解答,需要安装open-vm-tools
我是用的是源码安装方法:

There're some problem to install vm tools for ubuntu9.04, which it always require to be installed although you have done so. The answer from google is to install open-vm-tools. You can download it from sourceforge.

##CONTINUE##


tar -xzf open-vm-tools-*.tar.gz
sudo ./configure --without-x
sudo make
sudo make install


如果编译碰到没有gettext,glib版本过低问题,运行下面的命令应该会搞定:

If you meet any problem whilst compiling open-vm-tools, run the following command in terminal:


sudo apt-get install libproc-dev libdumbnet-dev libicu-dev libglib2.0-dev libglib1.2-dev


如果编译安装完全正确,打包文件拷贝到vm tools安装目录:

If you have done the compiling and installing procedures, tar the following files then put them into vm tools directory as:


cd modules/linux

for i in *; do mv ${i} ${i}-only; tar -cf ${i}.tar ${i}-only; done
cd ../../../
mv -f open-vm-tools-*/modules/linux/*.tar vmware-tools-distrib/lib/modules/source/


最后重新安装vm tools:

Finally, execute the install command as:


sudo ./vmware-install.pl

26 Apr 2009

Python Tips - 获得网页超链接

获得网页所有超级链接

get all hyperlinks



#!/usr/bin/env python
# get all hyperlinks

from re import compile as reComp
from urllib import urlopen, basejoin

def getURL(http, num=10):
""" get all hyperlinks and print the first num"""
data = urlopen(http).read()
regex = reComp(r'href="([^"]+)"')
urls = regex.findall(data)
for url in urls[:num]:
print basejoin(http, url)
#print url

def main():
getURL("http://52xenos.blogspot.com",5)

if __name__ == '__main__': main()


输出结果如下:

Results:
http://52xenos.blogspot.com/feeds/posts/default
http://52xenos.blogspot.com/feeds/posts/default?alt=rss
http://www.blogger.com/feeds/7524190211354587233/posts/default
http://www.blogger.com/rsd.g?blogID=7524190211354587233
http://www.blogger.com/profile/07043019578098595151

23 Apr 2009

Ubuntu Tips - 右键添加“打开终端“

Ubuntu默认右键不能打开终端,记得学校里redhat的kde环境下是可以的,
非常方便,google了下,方法如下:

Under the kde environment in RedHat, it's easy to open terminal everywhere by rightclick. Here is the method to add this feature to your ubuntu:

##CONTINUE##


sudo apt-get install nautilus-open-terminal

Ubuntu9.04发布

Ubuntu9.04今日正式发布

The new Ubuntu9.04 has been released today.

看官网介绍,904的新特性不少,具体请看:

Follow this link to see the new features of ubuntu 9.04:

http://www.ubuntu.com/

下载地址:

Download page:

http://releases.ubuntu.com/releases/9.04/

贴几个图:

Some screenshots of ubuntu 9.04:

##CONTINUE##
登陆界面:

Log in:



桌面:

Desktop: