Tuple和List的功能相近,主要目的是用来存放一组对象。但是,它们有一个最大的不同点:Tuple是不可变的!
对于元组的定义,可以使用小括号“()”来完成。对于其中的元素,需要使用逗号分隔。需要注意的一点是:定义元组时,小括号是可选的!但是为了防止产生歧义,强烈推荐在定义元组时使用小括号。定义元组的示例如下:
tup = ('one','two','three')
对于元组的定义,有两种情况需要注意:元组中的元素数量为0或1时。定义一个元素数为0的元组:zero_tup = ()。定义一个元素数为1的元组:one_item_tup = (1,)。对于一个元素的元组的定义,需要在元素后加一个逗号。原因在于如果没有括号,就会和改变运算优先级的括号产生歧义。
对于元组的访问,可以使用像数组一样,使用索引来完成:
print(tup[1]
#结果是:two
需要注意的是,在Python中,下表都是从0开始的。
元组还有另外 ......
Set是简单对象的无需集合。在set中,没有重复元素。通常在对集合中元素的顺序和出现的次数没有什么要求时使用。对于set,有一些函数可以帮助求解set之间的关系,例如:包含关系,交集关系等。
定义一个set:s = set([1,2,3,4])。使用set函数来定义一个set。注意,set中没有充分元素,如果定义set时其中包含重复元素,那该元素也仅会出现一次。
可以使用in关键字来判定某个对象是否属于一个set:
s = set([1,2,3])
print(1 in s)
#False
可以使用copy函数来拷贝一个set:
old_set = set([1,2,3])
new_set = old_set.copy()
可以使用add和remove来向set中添加或删除元素:
s = set([1,2,3])
s.add(4)
s.remove(1)
print(s)
#{2,3,4}
可以使用&、|求两个set的交集、并集:
s1 = set([1,2,3])
s2 = set([2,4])
......
import SocketServer, time, select, sys
from threading import Thread
COMMAND_HELLO = 1
COMMAND_QUIT = 2
# The SimpleRequestHandler class uses this to parse command lines.
class SimpleCommandProcessor:
def __init__(self):
pass
def process(self, line, request):
"""Process a command"""
args = line.split(' ')
command = args[0].lower()
args = args[1:]
if command == 'hello':
request.send('HELLO TO YOU TO!\n\r')
return COMMAND_HELLO
elif command == 'quit':
&nb ......
一个别人写的模块,可以直接用来把一个普通进程改为守护进程。
并且自动把标准输出重定向到日志文件,很实用啊。
view plaincopy to clipboardprint?
<BR>
'''''<BR>
This module is used to fork the current process into a daemon.<BR>
Almost none of this is necessary (or advisable) if your daemon <BR>
is being started by inetd. In that case, stdin, stdout and stderr are <BR>
all set up for you to refer to the network connection, and the fork()s <BR>
and session manipulation should not be done (to avoid&n ......
一个别人写的模块,可以直接用来把一个普通进程改为守护进程。
并且自动把标准输出重定向到日志文件,很实用啊。
view plaincopy to clipboardprint?
<BR>
'''''<BR>
This module is used to fork the current process into a daemon.<BR>
Almost none of this is necessary (or advisable) if your daemon <BR>
is being started by inetd. In that case, stdin, stdout and stderr are <BR>
all set up for you to refer to the network connection, and the fork()s <BR>
and session manipulation should not be done (to avoid&n ......
最近在看Python Web方面的开发,初步接触了Django和web.py(注:和web2py完全不相关)两个框架。
如果结合Apache部署需要一些研究,可能对CGI的不熟悉吧。虽然mod_python也可以,但似乎更流行mod_wsgi
,我是在Ubuntu安装的,本来想源码编译的,可是make的时候总是报错,最后$sudo apt-get install libapache2-mod-wsgi ,倒也简单。
注:如果采用编译的方式,需要预装apxs2(apxs is the tool for building modules for Apache (apxs2 is for apache2),
$sudo apt-get install apache2-threaded-dev
装好就是修改配置文件了,主要是修改/etc/apache2/sites-available/default文件:
基本照着向导
来就可以,因为是英文的,这里简要介绍如下:
在VirtualHost内添加
#采用守护模式而非内嵌模式
WSGIDaemonProcess example.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup example.com
#创建python脚本执行的别名,推荐程序脚本放在独立的目录而非DocumentRoot
WSGIScriptAlias /mywebpy /usr/local/mywebpy/index.py
<Directory /usr/local/m ......