文章列表
 
您正在查看 "python / django" 分类下的文章

2009年06月14日 星期日 上午 11:14
作者:老王

Python中的easy_install工具很爽,它的作用类似于Php中的pear,或者Ruby中的gem,或者Perl中的cpan。

如果想使用easy_install工具,可能需要先安装setuptools,不过更酷的方法是使用ez_setup.py脚本:

wget -q http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py

安装完后,最好确保easy_install所在目录已经被加到PATH环境变量里:

Windows: C:\Python25\Scripts
Linux: /usr/local/bin

比如说要安装Python的MySQL支持,可以执行如下命令,系统会自动在pypi网站列表里查找相关软件包:

easy_install MySQL-python

如果你在Windows+python2.5上执行如上命令的话,可能会出现如下错误:

Processing MySQL-python-1.2.3c1.tar.gz
Running MySQL-python-1.2.3c1\setup.py -q bdist_egg --dist-dir c:\docume~1\...
\locals~1\temp\easy_install-fvvfve\MySQL-python-1.2.3c1\egg-dist-tmp-q9moxf
error: The system cannot find the file specified

出现这类错误的原因是选错了版本,针对这个案列,我们可以显式指定软件包的版本号:

easy_install "MySQL-python==1.2.2"

通过easy_install安装软件,相关安装信息会保存到easy-install.pth文件里,路径类似如下形式:

Windows:C:\Python25\Lib\site-packages\easy-install.pth
Linux:/usr/local/lib/python25/site-packages/easy-install.pth

如果想删除通过easy_install安装的软件包,比如说:MySQL-python,可以执行命令:

easy_install -m MySQL-python

此操作会从easy-install.pth文件里把MySQL-python的相关信息抹去,剩下的egg文件,你可以手动删除。

参考文档:可爱的 Python: 使用 setuptools 孵化 Python egg
 
2009年02月23日 星期一 下午 7:57
作者:老王

Python号称“万物皆对象”,所以说“类”也是对象!类的实例叫对象,元类的实例叫类。也就是说,元类是类的类。这对Ruby程序员来说很好理解,因为Ruby里虚类的概念基本等同于元类,不过对于PHP程序员来说就不好理解了,下面看看语法:

先看看在Python2.6里的用法:

>>> class Foo(type):
        def __str__(self):
       
    return "foo"

>>> class Bar(object):
   
    __metaclass__ = Foo

        def __str__(self):
            return "bar"

>>> type(Bar)
<class '__main__.Foo'>
>>> type(Bar())
<class '__main__.Bar'>

>>> print(Bar)
foo
>>> print(Bar())
bar


再看看在Python3.0里的语法:

>>> class Foo(type):
        def __str__(self):
       
    return "foo"

>>> class Bar(object, metaclass = Foo):
        def __str__(self):
   
        return "bar"

>>> type(Bar)
<class '__main__.Foo'>
>>> type(Bar())
<class '__main__.Bar'>

>>> print(Bar)
foo
>>> print(Bar())
bar


元类必须从type继承,类声明的时候Python2.6和Python3.0不同:在2.6里是通过类变量__metaclass__来设置的,在3.0里是通过关键字参数metaclass来设置的。尽量使用Python3.0吧,更具体的信息可以参考手册
 
2009年02月05日 星期四 下午 3:38
作者:老王

Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this),用于传递对象本身,而在调用的时候则不必显式传递,系统会自动传递。

举一个很常见的例子:

>>> class Foo:
    def bar(self, message):
        print(message)

>>> Foo().bar("Hello, World.")
Hello, World.


当存在继承关系的时候,有时候需要在子类中调用父类的方法,此时最简单的方法是把对象调用转换成类调用,需要注意的是这时self参数需要显式传递,例如:

>>> class FooParent:
   
    def bar(self, message):
       
    print(message)

>>> class FooChild(FooParent):
        def bar(self, message):
   
        FooParent.bar(self, message)

>>> FooChild().bar("Hello, World.")
Hello, World.


这样做有一些缺点,比如说如果修改了父类名称,那么在子类中会涉及多处修改,另外,Python是允许多继承的语言,如上所示的方法在多继承时就需要重复写多次,显得累赘。为了解决这些问题,Python引入了super()机制,例子代码如下:

>>> class FooParent:
        def bar(self, message):
       
    print(message)

       
>>> class FooChild(FooParent):
   
    def bar(self, message):
       
    super(FooChild, self).bar(message)

       
>>> FooChild().bar("Hello, World.")
Hello, World.


表面上看 super(FooChild, self).bar(message)方法和FooParent.bar(self, message)方法的结果是一致的,实际上这两种方法的内部处理机制大大不同,当涉及多继承情况时,就会表现出明显的差异来,直接给例子:

代码一:

class A:
    def __init__(self):
        print("Enter A")
        print("Leave A")

class B(A):
    def __init__(self):
        print("Enter B")
        A.__init__(self)
        print("Leave B")

class C(A):
    def __init__(self):
        print("Enter C")
        A.__init__(self)
        print("Leave C")

class D(A):
    def __init__(self):
        print("Enter D")
        A.__init__(self)
        print("Leave D")

class E(B, C, D):
    def __init__(self):
        print("Enter E")
        B.__init__(self)
        C.__init__(self)
        D.__init__(self)
        print("Leave E")

E()


结果:

Enter E
Enter B
Enter A
Leave A
Leave B
Enter C
Enter A
Leave A
Leave C
Enter D
Enter A
Leave A
Leave D
Leave E


执行顺序很好理解,唯一需要注意的是公共父类A被执行了多次。

代码二:

class A:
    def __init__(self):
        print("Enter A")
        print("Leave A")

class B(A):
    def __init__(self):
        print("Enter B")
        super(B, self).__init__()
        print("Leave B")

class C(A):
    def __init__(self):
        print("Enter C")
        super(C, self).__init__()
        print("Leave C")

class D(A):
    def __init__(self):
        print("Enter D")
        super(D, self).__init__()
        print("Leave D")

class E(B, C, D):
    def __init__(self):
        print("Enter E")
        super(E, self).__init__()
        print("Leave E")

E()


结果:

Enter E
Enter B
Enter C
Enter D
Enter A
Leave A
Leave D
Leave C
Leave B
Leave E


在super机制里可以保证公共父类仅被执行一次,至于执行的顺序,是按照mro进行的(E.__mro__)。


参考链接:http://hi.baidu.com/isno/blog/item/32899358e2a6a4d99d820419.html
 
2008年12月23日 星期二 下午 7:35
作者:老王

前段时间受Erlang诱惑,疏远了Python。不过我已经迷途知返,今天聊聊Python与中文。

先看看Python3.0里面的情况:

首先,创建文件c:\chinese.py,文件编码是utf-8,文件内容如下:

print("中文")

在IDLE里执行:

>>> import sys
>>> sys.path.append("c:\\")
>>> import chinese
中文

一切都很完美!

再试试其它编码,把chinese.py的文件编码改成gbk,再执行上面的操作,结果报错:

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
    import chinese
File "c:\chinese.py", line 1
SyntaxError: Non-UTF-8 code starting with '\xd6' in file c:\chinese.py on line 1,
but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

看来,要想一个办法声明编码才行,按照PEP0263的介绍修改chinese.py文件内容:

# coding=gbk
print("中文")

再执行上面的操作,一切顺利。

很多时候,你可能会发现其它形式的编码声明方式,如下:

# -*- coding: <encoding name> -*-
# vim: set fileencoding=<encoding name> :

这两种方式同样符合PEP0263的要求,并且在设定了python编码的同时还顺带设定了emacs / vim的编码。

再看看Python2.6里面的情况:

一样的chinese.py文件,如果不声明编码的话,不管是utf-8还是gbk,都会报错:

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
    import chinese
File "c:\chinese.py", line 1
SyntaxError: Non-ASCII character '\xd6' in file c:\chinese.py on line 1,
but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (chinese.py, line 1)

总结:


使用非ASCII字符时,在Python3.0里面,如果是utf-8编码,可以不用声明编码。在Python2.X里面,不管什么编码都要声明编码!

========================================

Python资料:

http://wiki.woodpecker.org.cn/moin/
http://www.elias.cn/Develop/Python/
 
2008年10月29日 星期三 下午 9:18
作者:老王

Pygame让你可以用简单的Python代码编写游戏软件,相当强悍。对于我这样的初学者来说,最重要的就是要快点看到效果,Pygame这点做得不错,安装相当简单,以我的Windows XP操作系统为例,以前已经装过了Python2.5,那么只要再下载以下安装包,一路Next安装即可:

pygame-1.8.1.win32-py2.5.msi

如果想快点看到演示,接着安装:

pygame-1.8-docs-setup.exe

浏览演示程序的时候唯一需要注意的是:在Windows操作系统,你不要用鼠标双击,而要在CMD命令行执行,比如:

C:\Program Files\Pygame 1.8 Documents and Examples\examples>python aliens.py

这样就能但到一个射击游戏(空格是发射子弹),代码大约三百行左右,相当炫,官方网站还提供了一些游戏截图,大家可以欣赏一下,而且,这些游戏的源代码也是可以下载的。





网上能找到不少资料,比如:

Beginning Game Development with Python and Pygame.pdf

在电驴上能下载到。
 
   
 
 
文章存档
 
     
 
最新文章评论
  

[表情]
 

不错!
 

linux大师之路,www.linuxmr.com
 

引导一直没有整明白说。
 

[表情]
   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu