Python
cloudhe
这个作者很懒,什么都没留下…
展开
-
Amazing Python 1: "yield"
"yield" is used for Generater (below 2.2) or seperately (2.2 or above) in Python. "yield" mainly works as "return", but it makes a function able to have multiple return values step by step . Al...原创 2008-07-23 11:37:49 · 122 阅读 · 0 评论 -
Python 模块之 threading: 在 Python 中使用多线程
Python 是支持多线程的,并且是 native 的线程。主要是通过 thread 和 threading 这两个模块来实现的。thread是比较底层的模块,threading 是对 thread 做了一些包装的,可以更加方便的被使用。这里需要提一下的是 Python 对线程的支持还不够完善,不能利用多 CPU,但是下个版本的 Python 中已经考虑改进这点,让我们拭目以待。 t...原创 2008-08-15 14:25:52 · 116 阅读 · 0 评论 -
Python 中使用 pyrex 生成 Linux 可执行文件
这个问题是相当有意义的,如果有了比较好的方法,Python 完全可以用来开发商业软件,而不用担心源代码泄露。 前两天我在网上看了看,有很多的人在问这个问题。大部分的人都在用 py2exe,这是个对 Python 程序打包的东东,实际上只是在 Python 程序中找 出依赖的模块,然后发布这个程序。好处在于可以让这个程序脱离 Python 的环境来执行,不用安装 Python 解释器。...原创 2008-08-15 14:30:27 · 494 阅读 · 0 评论 -
A problem in unpacking strings
If a string to be packed is shorter than its format, it'll be followed by '\x00's when unpacked. One example, in which every length of the string fits the format: python 代码 >>> import ...2007-12-07 11:11:49 · 67 阅读 · 0 评论 -
Installing python on maemo and Nokia N800
Python2.5 is needed when using pygtk on maemo, otherwise the default python2.3 may be enough. Add "deb http://repository.maemo.org/extras bora free non-free" to /scratchbox/users/maemo/target/SDK_ARME...2007-12-07 15:07:27 · 144 阅读 · 0 评论 -
Bewildering service names of python application on maemo
A simple file structure of python applications on maemo/n800: helloword helloword.desktop helloword.service helloword.png setup.py Note: 1. Main source file helloworld is written in python, but it ...2007-12-07 15:51:35 · 73 阅读 · 0 评论 -
[Copy] 有限状态自动机(FSM)的简单实现
[i][color=red][size=11]Sometimes, a quite simple concept comes out with lots of related theories that make beginners have to spend an exausting time before figure out the basic one. Watching something...2007-12-12 11:17:15 · 203 阅读 · 0 评论 -
Feel Pythonic: The Zen of Python
Let's feel about what "pythonic" is! >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Compl...原创 2008-09-24 14:20:25 · 116 阅读 · 0 评论 -
What's Pythonic?
If you're not willing to read English, here is a translation: http://blog.csdn.net/lanphaday/archive/2008/08/03/2762251.aspx What the heck does "pythonic" mean? This was a question asked a few...原创 2008-09-24 14:30:28 · 165 阅读 · 0 评论 -
Python and Ruby
Python 和 Ruby 的相同点: 都强调语法简单,都具有更一般的表达方式。 Python 是缩进,Ruby 是类 Basic 的表达。 都大量减少了符号。 都是动态数据类型。 都是有丰富的数据结构。 都具有 C 语言扩展能力,都具有可移植性,比 perl 的可移植性更好。也都可以作为嵌入语言。 都是面向对象的语言,都可以作为大...原创 2008-10-06 16:13:28 · 102 阅读 · 0 评论 -
Python 模块之 ConfigParser: 用 Python 解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做一些介绍。 ConfigParser 解析的配置文件的格式比较象 ini 的配置文件格式,就是文件中由多个 section 构成,每个 section 下又有多个配置项,比如: [db...原创 2008-08-15 14:23:11 · 101 阅读 · 0 评论 -
Python 中怎么 copy 文件和目录
>>> import shutil >>> dir(shutil) ['Error', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '_samefi le', 'abspath', 'copy', 'copy2', 'copyfile', 'copyfileobj', 'copym...原创 2008-08-15 14:20:14 · 151 阅读 · 0 评论 -
Python 中使用 FTP
import ftplib ftp = ftplib.FTP() ftp.connect("ftp.freebsdchina.org",21) ftp.login("anonymous", "123") ftp.dir() ftp.quit() 下载用这个: ftp.retrbinary('RETR '+filename, open(filename,'wb').write) 上传用这个...原创 2008-08-15 14:19:09 · 150 阅读 · 0 评论 -
Amazing Python 2: "exec/eval/repr"
1. exec: Executing command from string. class PlayerBase(): player_name = None def talk(self): print self.player_name, ": It takes five." class Kobe(Pl...原创 2008-07-25 14:35:38 · 77 阅读 · 0 评论 -
Amusing Python 1: "*"
Take a look at the three different "lists" below, guess the outputs: (a) >>> lists = [ [], [], [] ] >>> lists [[], [], []] >>> lists[1].append("Hi there!") >>原创 2008-07-25 15:15:19 · 103 阅读 · 0 评论 -
用 Python 下载网页,超级简单!
from urllib import urlopen webdata = urlopen("").read() print webdata From http://www.pythonid.com/html/fenleiwenzhang/network/20070910/173.html See more in "Dive Into Python"原创 2008-08-15 13:42:14 · 242 阅读 · 0 评论 -
Amazing Python 3: "@"
1. This an interesting usage that I don't know how to call it. See example: def hehe(tt): return 'hehe'+tt() @hehe def test(): return 'test' print test The output is same as: def hehe...原创 2008-08-15 13:44:31 · 94 阅读 · 0 评论 -
Python2.5 中的新类型:set (集合)
Python2.5 中新增加了集合内容,让我想起了初中数学。看看: >>> b = set() >>> b.add(1) >>> b.add(2) >>> b.add(3) >>> b.add(4) >>> >>>原创 2008-08-15 13:46:56 · 81 阅读 · 0 评论 -
在 Python 代码中进行自动测试
Python 有一个叫 doctest 的模块,很有意思,它可以让你在代码的注释中写入一些特殊的注释,这些注释是测试程序。Python 可以自动对它进行测试。 import doctest def myadd(a, b): ''' >>> myadd(1, 2) 3 >>> myadd(4, 5) 9 ''' ...原创 2008-08-15 13:49:07 · 139 阅读 · 0 评论 -
Amusing Python 2: range/xrange
这两个基本上都是在循环的时候用。 for i in range(0, 100): print i for i in xrange(0, 100): print i 这两个输出的结果都是一样的,实际上有很多不同,range会直接生成一个list对象: >>> a = range(0, 10) >>> type(a) <type ...原创 2008-08-15 14:03:07 · 84 阅读 · 0 评论 -
用 C 为 Python 写个简单的模块
Python 语言是支持用 C 来它写模块的,其实现有的很多模块也是用 C 写的。这里我做个简单的介绍。先决条件:1. 在 linux 上编写时,需要自己编译出 Python的动态连接库。也就是要有 libpython2.5.so 这样的东西。2. 在 windows 上时,则需要 mingw 这个编译环境。其实只要你安装了 Dev-Cpp 就有了。当然还安装了 windows 版的 Python...原创 2008-08-15 14:09:41 · 105 阅读 · 0 评论 -
Python 中列出目录中所有文件的方法
import string, os, sys dir = '/var' print '----------- no sub dir' files = os.listdir(dir) for f in files: print dir + os.sep + f print '----------- all dir' for root, dirs, files in os.walk(d...原创 2008-08-15 14:17:16 · 858 阅读 · 0 评论 -
Syntax highlights of Python in UltraEdit
Open \UEStudio 06\wordfile.txt (or find in UE menu: 高级->配置->编辑器显示->语法加亮->打开). Append following to the file: (the "/L14" in line one must be changed to the existing last number in your ow...2008-05-09 16:39:57 · 78 阅读 · 0 评论