Differences between revisions 11 and 18 (spanning 7 versions)
Revision 11 as of 2006-06-09 04:59:07
Size: 2018
Editor: xiaobolee
Comment:
Revision 18 as of 2009-12-25 07:08:41
Size: 6393
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
Email: [[MailTo([email protected])]] Email: <<MailTo([email protected])>>
Line 7: Line 7:
== Python System Modules: == == 系统工具 ==
=
== Python System Modules: ===
{{{
Line 10: Line 11:
== os Modules == }}}
=
== os Modules ===
{{{
Line 19: Line 22:
}}}
=== stdin用法 ===
{{{
sys.stdin.isatty()可用来判断是否从控制台输入(个人认为只要是用命令行指定的都是从控制台输入,而用管道等方法输入的不是控制台输入)。sys.stdin.read()可以用来读入命令,比如用户没有输入参数时,可以用sys.stdin.read()来从console读出,经典的用法可以读出管道文件,如果此时用sys.stdin.isatty()就可以知道不是从控制台输入的。
}}}
=== 如何遍历一个目录 ===
{{{
三种方法:1。os.popen(‘dir or ls’),从这里可以看出该方法在不同平台上无法方便的移植,与具体平台命令相关。2。glob.glob(‘*/?*_[]’),从这里可以看出该方法与平台无关。而且可以很方便的进行一些模糊匹配。3。os.listdir(os.curdir)
 遍历目录则用os.path.walk() 或者 os.walk()
}}}
=== 引用还是拷贝? ===
==== 由from .... import ... 和 import....说起 ====
先看代码:
{{{#mytest.py
a=[1,2]
b=3
}}}
在交互式命令行中测试如下:
{{{
>>> from mytest import a,b
>>> a
[1, 2]
>>> b
3
>>> a[0]=5
>>> b=5
>>> id(a)
14106256
>>> id(b)
3629168
>>> import mytest
>>> mytest.a
[5, 2]
>>> mytest.b
3
>>> id(mytest.a)
14106256
>>> id(mytest.b)
3629192
>>> mytest.a[1]=5
>>> a
[5, 5]
>>> mytest.b=18
>>> b
5
>>> from mytest import a,b
>>> a
[5, 5]
>>> b
18
}}}
{{{
可以看出由于b是不可变变量,所以改变以后import mytest(引入mytest模块变量)之后,只有可变变量(list、dic)受到影响,也就是说a的值能够随着改变,此时在mytest模块变量中,对mytest对象的属性改变也同理只有可变变量才会直接受到影响。如何才能反映到from先前引入的变量呢?只能再做一次from,这样由于mytest对象已经存在,所以直接就可以把mytest对象的各个变量引入,也就是说a,b是对mytest对象进行了赋值。
}}}
==== 两个对比 ====
下面内容为转载(http://starship.python.net/crew/mwh/hacks/objectthink.html,如有侵权,我会及时删掉):
两个例子:
{{{
example first:
>>> dict = {'a':1,'b':2}
>>> list = dict.values()
>>> list
[1, 2]
>>> dict['a']=3
>>> list
[1, 2]
>>> dict
{'a': 3, 'b': 2}
}}}
{{{
example second:
>>> dict = {'a':[1],'b':[2]}
>>> list = dict.values()
>>> list
[[1], [2]]
>>> dict['a'].append(3)
>>> dict
{'a': [1, 3], 'b': [2]}
>>> list
[[1, 3], [2]]
}}}
为什么list的值会改变?我只看了原文图释部分。
{{{
首先搞清楚几个概念:name 、binding 、object
Names look like this:
    ,-----.
    | foo |
    `-----'
Bindings look like this:
Line 20: Line 112:
["source"]     ------------>
Objects look like this:

    +-------+
    | "bar" |
    +-------+
}}}
{{{
example first:
     ------. +-------+
    | dict |------>|+-----+| +---+
    `------' || "a" |+---->| 1 |
                   |+-----+| +---+
                   |+-----+| +---+
                   || "b" |+---->| 2 |
                   |+-----+| +---+
                   +-------+
 
       ------. +-------+
    | dict |------>|+-----+| +---+
    `------' || "a" |+------------>| 1 |
                   |+-----+| +---+
                   |+-----+| /\
                   || "b" |+-----. ,---'
                   |+-----+| | |
                   +-------+ `----+----.
                                      | |
    ,------. +-----+ | \/
    | list |------>| [0]-+------------' +---+
    `------' | [1]-+--------------->| 2 |
                   +-----+ +---+

}}}
== 并行系统工具 ==
{{{有两种内置的方法能够使任务在同一时间段内运行,Process forks 和 Spawned thread。其中Process forks不能运行在Windows平台上。}}}
=== Process forks ===
{{{
理论上讲,一个fork进程得到的是原始进程的整个内存拷贝,包括文件描述。所以 全局对象,例如文件就在每一个子进程中保存有一份相同的值,但是最重要的就是要记住,只是内存的拷贝,而不是共享,如果子进程改变了全局的对象,那么它仅仅是改变了它自己的那一份,而不会影响到其他进程。
}}}
[[source]]

一些零散笔记

Email: <[email protected]>


系统工具

Python System Modules:

 . Sys与Os是python系统工具的核心,很多标准模块是从他们,原则上,sys导出与Python解释器相关的部件(例如模块的搜索路径),而os则包含了Python所运行的平台上的函数以及变量的映射。但这种界限比较模糊,例如标准的输入输出流是在Sys中的,它们明显是要和平台紧密联系的。

os Modules

os.curdir…….os.getcwd()//得到当前目录
        1、os.path模块
        提供了大量的关于目录的工具。例如:os.path.isdir()……isfile()……exists()…..getsize()….
……split()…..join()….basename()….dirname()……splittext()….. 
        2、在脚本文件中执行shell命令
        os.system :很简单的执行一条shell命令行
os.popen: 能连接到标准输入输出流,这样就可以实现命令的交互。用popen可以参考open()的用法,也是popen(commandStr),返回一个对象
        注意:在system使用时,Linux环境下在命令后+“&”才能实现并行执行命令,而在windows环境下,在命令前+“start”。

stdin用法

sys.stdin.isatty()可用来判断是否从控制台输入(个人认为只要是用命令行指定的都是从控制台输入,而用管道等方法输入的不是控制台输入)。sys.stdin.read()可以用来读入命令,比如用户没有输入参数时,可以用sys.stdin.read()来从console读出,经典的用法可以读出管道文件,如果此时用sys.stdin.isatty()就可以知道不是从控制台输入的。

如何遍历一个目录

三种方法:1。os.popen(‘dir or ls’),从这里可以看出该方法在不同平台上无法方便的移植,与具体平台命令相关。2。glob.glob(‘*/?*_[]’),从这里可以看出该方法与平台无关。而且可以很方便的进行一些模糊匹配。3。os.listdir(os.curdir)
        遍历目录则用os.path.walk() 或者 os.walk()

引用还是拷贝?

由from .... import ... 和 import....说起

先看代码: {{{#mytest.py a=[1,2] b=3 }}} 在交互式命令行中测试如下:

>>> from mytest import a,b
>>> a
[1, 2]
>>> b
3
>>> a[0]=5
>>> b=5
>>> id(a)
14106256
>>> id(b)
3629168
>>> import mytest
>>> mytest.a
[5, 2]
>>> mytest.b
3
>>> id(mytest.a)
14106256
>>> id(mytest.b)
3629192
>>> mytest.a[1]=5
>>> a
[5, 5]
>>> mytest.b=18
>>> b
5
>>> from mytest import a,b
>>> a
[5, 5]
>>> b
18

可以看出由于b是不可变变量,所以改变以后import  mytest(引入mytest模块变量)之后,只有可变变量(list、dic)受到影响,也就是说a的值能够随着改变,此时在mytest模块变量中,对mytest对象的属性改变也同理只有可变变量才会直接受到影响。如何才能反映到from先前引入的变量呢?只能再做一次from,这样由于mytest对象已经存在,所以直接就可以把mytest对象的各个变量引入,也就是说a,b是对mytest对象进行了赋值。

两个对比

下面内容为转载(http://starship.python.net/crew/mwh/hacks/objectthink.html,如有侵权,我会及时删掉): 两个例子:

example first:
>>> dict = {'a':1,'b':2}
>>> list = dict.values()
>>> list
[1, 2]
>>> dict['a']=3
>>> list
[1, 2]
>>> dict
{'a': 3, 'b': 2}

example second:
>>> dict = {'a':[1],'b':[2]}
>>> list = dict.values()
>>> list
[[1], [2]]
>>> dict['a'].append(3)
>>> dict
{'a': [1, 3], 'b': [2]}
>>> list
[[1, 3], [2]]

为什么list的值会改变?我只看了原文图释部分。

首先搞清楚几个概念:name 、binding 、object
Names look like this:
    ,-----.
    | foo |
    `-----'
Bindings look like this:

    ------------>
Objects look like this:

    +-------+
    | "bar" |
    +-------+

example first:
     ------.       +-------+
    | dict |------>|+-----+|     +---+
    `------'       || "a" |+---->| 1 |
                   |+-----+|     +---+
                   |+-----+|     +---+
                   || "b" |+---->| 2 |
                   |+-----+|     +---+
                   +-------+
 
       ------.       +-------+
    | dict |------>|+-----+|             +---+
    `------'       || "a" |+------------>| 1 |
                   |+-----+|             +---+
                   |+-----+|              /\
                   || "b" |+-----.    ,---'
                   |+-----+|     |    |
                   +-------+     `----+----.
                                      |    |
    ,------.       +-----+            |    \/
    | list |------>| [0]-+------------'   +---+
    `------'       | [1]-+--------------->| 2 |
                   +-----+                +---+

并行系统工具

有两种内置的方法能够使任务在同一时间段内运行,Process forks 和 Spawned thread。其中Process forks不能运行在Windows平台上。

Process forks

理论上讲,一个fork进程得到的是原始进程的整个内存拷贝,包括文件描述。所以 全局对象,例如文件就在每一个子进程中保存有一份相同的值,但是最重要的就是要记住,只是内存的拷贝,而不是共享,如果子进程改变了全局的对象,那么它仅仅是改变了它自己的那一份,而不会影响到其他进程。

source

待整理

#test_init.py
class test:
    def __init__(self):
        print "test.__init__"
        self.reset()
    def reset(self):
        print "test.reset()"

#dirive.py
from test_init import test
class dirive(test):
    def __init__(self):
        print "dirive.__init__"
        test.__init__(self)
    def reset(self):
        print "dirive.reset()"

if __name__ == "__main__":
    my_dirive = dirive()
#输出:
#dirive.__init__
#test.__init__
#dirive.reset()

如果dirive代码换成以下情况

#dirive.py
from test_init import test
class dirive(test):
    def reset(self):
        print "dirive.reset()"

if __name__ == "__main__":
    my_dirive = dirive()
#输出:
#test.__init__
#dirive.reset()

xiaobolee (last edited 2009-12-25 07:08:41 by localhost)