cloudhe -- Code rules.
cloudhe · 共 39 篇文章
Python and Ruby
Python 和 Ruby 的相同点: 都强调语法简单,都具有更一般的表达方式。 Python 是缩进,Ruby 是类 Basic 的表达。 都大量减少了符号。 都是动态数据类型。 都是有丰富的数据结构。 都具有 C 语言扩展能力,都具有可移植性,比 perl 的可移植性更好。也都可以作为嵌入语言。 都是面向对象的语言,都可以作为大项目的开发工具。 都有丰富的库支持。 也有最宽松的版权许可,除了一些工具属于GNU世界。 都有 lisp 特色的 eval 函数,也都能把函数作为参数。 也有图形界面的Ruby的专门编辑器。 都获得了广泛的 C 库的支持,如 Qt、Gtk、Tk、SDL、FOX 等…
2008-10-06 16:13 · Python · 评论(2)
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 months ago, on, of all places, the EuroPython mailing list, which is mainly used to plan the EuroPython co…
2008-09-24 14:30 · Python · 评论(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. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Spec…
2008-09-24 14:20 · Python · 评论(0)
What's WiMAX?
究竟何谓WiMAX? WiMAX全称为World Interoperability for Microwave Access,即全球微波接入互操作性。WiMAX的另一个名字是802.16。 IEEE802.16标准,又称WiMAX,或广带无线接入(Broadband Wireless Access,BWA)标准。它是一项无线城域网(WMAN)技术,是针对微波和毫米波频段提出的一种新的空中接口标准。它用于将802.11a无线接入热点连接到互联网,也可连结公司与家庭等环境至有线骨干线路。它可作为线缆和DSL的无线扩展技术,从而实现无线宽带接入。 WiMAX是一项新兴的无线通信技术,能提供面向互联…
2008-09-24 13:20 · Other · 评论(0)
Python 中使用 pyrex 生成 Linux 可执行文件
这个问题是相当有意义的,如果有了比较好的方法,Python 完全可以用来开发商业软件,而不用担心源代码泄露。 前两天我在网上看了看,有很多的人在问这个问题。大部分的人都在用 py2exe,这是个对 Python 程序打包的东东,实际上只是在 Python 程序中找 出依赖的模块,然后发布这个程序。好处在于可以让这个程序脱离 Python 的环境来执行,不用安装 Python 解释器。但实际上并不能保护源代码,因为源 代码其实也包含在其中了。更好的办法是使用 Pyrex。在 http: //www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ 可…
2008-08-15 14:30 · Python · 评论(0)
Python 模块之 threading: 在 Python 中使用多线程
Python 是支持多线程的,并且是 native 的线程。主要是通过 thread 和 threading 这两个模块来实现的。thread是比较底层的模块,threading 是对 thread 做了一些包装的,可以更加方便的被使用。这里需要提一下的是 Python 对线程的支持还不够完善,不能利用多 CPU,但是下个版本的 Python 中已经考虑改进这点,让我们拭目以待。 threading 模块里面主要是对一些线程的操作对象化了,创建了叫 Thread 的 class。一般来说,使用线程有两种模式,一种是创建线程要执行的函数,把这个函数传递进 Thread 对象里,让它来执行;另一…
2008-08-15 14:25 · Python · 评论(0)
Python 模块之 ConfigParser: 用 Python 解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做一些介绍。 ConfigParser 解析的配置文件的格式比较象 ini 的配置文件格式,就是文件中由多个 section 构成,每个 section 下又有多个配置项,比如: [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass=password [concurrent] thread=10 processor=20 假设上面的配置文…
2008-08-15 14:23 · Python · 评论(0)
Python 中怎么 copy 文件和目录
>>> import shutil >>> dir(shutil) ['Error', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '_samefi le', 'abspath', 'copy', 'copy2', 'copyfile', 'copyfileobj', 'copymode', 'copysta t', 'copytree', 'destinsrc', 'move', 'os', 'rmtree', 'stat', 'sys'] >>> shutil.copyfile('d:/c.sql','d:/…
2008-08-15 14:20 · Python · 评论(1)
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) 上传用这个: ftp.storbinary("STOR "+filename, open(filename, 'rb')) from http://www.pythonid.com/html/fenleiwenzhan…
2008-08-15 14:19 · Python · 评论(1)
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(dir): for name in files: print os.path.join(root, name) 前面的 os.listdir 可以列出 dir 里面的所有文件和目录,但不包括子目录中的内容。os.wa…
2008-08-15 14:17 · Python · 评论(0)
用 C 为 Python 写个简单的模块
Python 语言是支持用 C 来它写模块的,其实现有的很多模块也是用 C 写的。这里我做个简单的介绍。 先决条件: 1. 在 linux 上编写时,需要自己编译出 Python的动态连接库。也就是要有 libpython2.5.so 这样的东西。 2. 在 windows 上时,则需要 mingw 这个编译环境。其实只要你安装了 Dev-Cpp 就有了。当然还安装了 windows 版的 Python. 先把源代码帖上来,很简单,假设保存为 hello.c: #include <Python.h> static PyObject * hello_echo(PyObject *self, P…
2008-08-15 14:09 · Python · 评论(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 'list'> >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> a[0] 0 而xrange则不会直接生成一个list,而是每次调用返回其中的一个值: >>> a = xrange(0, 10) >>> type(a) <type 'x…
2008-08-15 14:03 · Python · 评论(2)
在 Python 代码中进行自动测试
Python 有一个叫 doctest 的模块,很有意思,它可以让你在代码的注释中写入一些特殊的注释, 这些注释是测试程序。Python 可以自动对它进行测试。 import doctest def myadd(a, b): ''' >>> myadd(1, 2) 3 >>> myadd(4, 5) 9 ''' return a+b if __name__ == '__main__': doctest.testmod() 输出: D:\>e2.py -v Trying: myadd(1, 2) Expecting: 3 ok Trying: myadd(4, 5) Expecting: 9 …
2008-08-15 13:49 · Python · 评论(0)
Python2.5 中的新类型:set (集合)
Python2.5 中新增加了集合内容,让我想起了初中数学。 看看: >>> b = set() >>> b.add(1) >>> b.add(2) >>> b.add(3) >>> b.add(4) >>> >>> c = set() >>> c.add(4) >>> c.add(5) >>> c.add(6) >>> d = b.difference(c) >>> print d set([1, 2, 3]) >>> print b set([1, 2, 3, 4]) >>> print c set([4, 5, 6]) >>> d = b.intersection(c) >>> pri…
2008-08-15 13:46 · Python · 评论(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(tt): return 'hehe'+tt() def test(): return 'test' test = hehe(test) print test Output: hehetest P.S: It enables suc…
2008-08-15 13:44 · Python · 评论(0)