Size: 25067
Comment:
|
Size: 28382
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 657: | Line 657: |
1、下载karrigell,解压后,根据默认设置直接就可以运行了,但一般修改conf/下Karrigell.ini中的port=8081,表示使用端口8081,保存 2、将msg拷贝至webapps/,并在index.pih中增加链接<a href='msg/'> Message</a> 3、编辑msg中的index.ks,完成所需功能 4、cd至karrigell所在目录,输入python Karrigell.py运行后,在浏览器地址栏中输入localhost:8081就可以看到页面,点击Message链接即可到达。 |
1. 下载karrigell,解压后,根据默认设置直接就可以运行了,但一般修改conf/下Karrigell.ini中的port=8081,表示使用端口8081,保存 1. 将msg拷贝至webapps/,并在index.pih中增加链接<a href='msg/'> Message</a> 1. 编辑msg中的index.ks,完成所需功能 1. cd至karrigell所在目录,输入python Karrigell.py运行后,在浏览器地址栏中输入localhost:8081就可以看到页面,点击Message链接即可到达。 |
Line 736: | Line 736: |
#coding:utf-8 '''cdays+3-exercise-1.py using Thread and RLock ''' from threading import Thread from threading import RLock import time class myThread(Thread): '''myThread user-defined thread ''' def __init__(self, threadname): Thread.__init__(self, name = threadname) def run(self): global share_var#共享一全局变量 lock.acquire()#调用lock的acquire,获得锁 share_var += 1#修改共享变量 #time.sleep(2) print share_var lock.release()#释放 if __name__ == "__main__": share_var = 0 lock = RLock() threadlist = [] for i in range(10):#产生10个线程 my = myThread('Thread%d' % i) threadlist.append(my) for i in threadlist:#开始10个线程 i.start() |
|
Line 741: | Line 773: |
#coding:utf-8 '''cdays+3-exercise-2.py using Thread and Queue @author: U{shengyan<mailto:[email protected]>} @version:$Id$ ''' from threading import Thread import Queue import time class Input(Thread): '''Input Thread: read one string from stdio, then add this string into queue ''' def __init__(self, threadname): Thread.__init__(self, name = threadname) def run(self): some_string = raw_input('please input something for thread %s:' % self.getName())#输入一个字符串 global queue queue.put((self.getName(), some_string))#加入到队列 #time.sleep(5)#延时一段时间 class Output(Thread): '''Output Thread: read one string from queue, then print it out ''' def __init__(self, threadname): Thread.__init__(self, name = threadname) def run(self): global queue (iThread, something) = queue.get()#从queue中读取 print 'Thread %s get "%s" from Thread %s' % (self.getName(), something, iThread)#输出 if __name__ == "__main__": queue = Queue.Queue()#创建Queue对象 inputlist = [] outputlist = [] for i in range(10): il = Input('InputThread%d' % i) #输入线程列表 inputlist.append(il) ol = Output('outputThread%d' % i)#输出线程列表 outputlist.append(ol) for i in inputlist: i.start()#依次开始输入线程 i.join()#等待 for i in outputlist: i.start()#依次开始输出线程 #i.join() |
|
Line 746: | Line 823: |
#coding:utf-8 '''cdays+3-exercise-3.py using Thread and Event ''' from threading import Thread from threading import Event import time class myThread(Thread): '''myThread user-defined thread ''' def __init__(self, threadname): Thread.__init__(self, name = threadname) def run(self): global event global share_var if event.isSet():#判断event的信号标志 event.clear()#若设置了,则清除 event.wait()#并调用wait方法 #time.sleep(2) share_var += 1#修改共享变量 print '%s ==> %d' % (self.getName(), share_var) else: share_var += 1#未设置,则直接修改 print '%s ==> %d' % (self.getName(), share_var) #time.sleep(1) event.set()#设置信号标志 if __name__ == "__main__": share_var = 0 event = Event()#创建Event对象 event.set()#设置内部信号标志为真 threadlist = [] for i in range(10):#创建10个线程 my = myThread('Thread%d' % i) threadlist.append(my) for i in threadlist:#开启10个线程 i.start() |
status |
校对 |
完成度70% |
1. 故事练习解答
1.1. CDays
1.1.1. CDays-5
- 计算今年是闰年嘛?
- 相关代码:
1 #coding:utf-8
2 '''cdays-5-exercise-1.py this year is a leap year or not
3 '''
4 #导入time模块
5 import time
6 #获取当前年份
7 thisyear = time.localtime()[0]
8 #判断闰年条件,满足模400为0,或者模4为0但模100不为0
9 if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0:
10 print 'this year %s is a leap year' % thisyear
11 else:
12 print 'this year %s is not a leap year' % thisyear
利用python作为科学计算器。熟悉Python中的常用运算符,并分别求出表达式12*34+78-132/6、(12*(34+78)-132)/6、(8.6/4)**5的值。并利用math模块进行数学计算,分别求出145/23的余数,0.5的sin和cos值等等,详细可参考help(math)。
- 相关代码:
1 #coding:utf-8
2 '''cdays-5-exercise-2.py basic operation and math library
3 '''
4 #表达式计算
5 x = 12*34+78-132/6
6 y = (12*(34+78)-132)/6
7 z = (8.6/4)**5
8 print '12*34+78-132/6 = %d' % x
9 print '(12*(34+78)-132)/6 = %d' % y
10 print '(8.6/4)**5 = %f' % z
11 #导入数学计算模块
12 import math
13 #求余函数
14 a = math.fmod(145, 23)
15 #正弦函数
16 b = math.sin(0.5)
17 #余弦函数
18 c = math.cos(0.5)
19 print '145/23的余数 = %d' % a
20 print 'sin(0.5) = %f' %b
21 print 'cos(0.5) = %f' %c
- 编写程序,在屏幕上打印出如下图案:
attachment:cdays-5-exercise-3.png
- 相关代码:
截图
1.1.2. CDays-4
os 模块中还有哪些功能可以使用? -- 提示使用 dir() help()
- os.error, os.path, os.popen, os.stat_result, os.sys, os.system等等等
详细可参见dir(os)和python帮助文档help(os)
open() 还有哪些模式可以使用?
- 'r': 以只读方式打开已存在文件,若文件不存在则抛出异常。此方式是默认方式
- 'U'或者'rU': python惯例构造了通用换行支持;提供'U'模式以文本方式打开一个文件,但是行可能随时结束:Unix的结束符规定为'\n',苹果系统则为'\r',还有windows规定为'\r\n',所有这些规定在python程序中统一为'\n'.如果python不构造一个通用换行支持模式'u'来用统一对待正常文本模式的话
- 'w': 以可写方式打开存在或者不存在的文件,若文件不存在则先新建该文件,若文件存在则覆盖该文件
- 'a': 用于追加,对unix系统而言,所有的内容都将追加到文件末尾而不管指针的当前位置如何
- 'b': 以二进制方式打开。打开一个二进制文件必须用该模式。增加'b'模式是用来兼容系统对当二进制和文本文件的处理不同
- 'r+','w+'和'a+'以更新方式打开文件(注意'w+'覆盖文件)
尝试for .. in .. 循环可以对哪些数据类型进行操作?
- for..in循环对于任何序列(列表,元组,字符串)都适用。但从广义说来可以使用任何种类的由任何对象组成的序列
- 格式化声明,还有哪些格式可以进行约定?
详细参考:[http://docs.python.org/lib/typesseq-strings.html typesseq-strings]
- d Signed integer decimal.
- i Signed integer decimal.
- o Unsigned octal.
- u Unsigned decimal.
- x Unsigned hexadecimal (lowercase).
- X Unsigned hexadecimal (uppercase).
- e Floating point exponential format (lowercase).
- E Floating point exponential format (uppercase).
- f Floating point decimal format.
- F Floating point decimal format.
- g Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise.
- G Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise.
- c Single character (accepts integer or single character string).
- r String (converts any python object using repr()).
- s String (converts any python object using str()).
- % No argument is converted, results in a "%" character in the result.
- 现在的写入文件模式好嘛? 有改进的余地?
CDay-4-5.py 好在哪里? :
CDay-4-6.py 又更加好在哪里? :
CDay-4-5.py中使用了字符串的+连接,而CDay-4-6.py中是利用join。字符串的join要比'+'操作效率高。因为对象的反复+,比一次性内建处理,要浪费更多的资源。
- 读取文件cdays-4-test.txt内容,去除空行和注释行后,以行为单位进行排序,并将结果输出为cdays-4-result.txt。
1 #coding:utf-8
2 '''cdays-4-exercise-6.py file operation
3 '''
4 #以读方式打开文件
5 f = open('cdays-4-test.txt', 'r')
6 result = list()
7 #依次读取每行
8 for line in f.readlines():
9 #去掉每行头尾空白
10 line = line.strip()
11 #判断是否是空行或注释行
12 if not len(line) or line.startswith('#'):
13 #是的话,跳过不处理
14 continue
15 result.append(line)
16 #排序结果
17 result.sort()
18 #保存入结果文件
19 open('cdays-4-result.txt', 'w').write('%s' % '\n'.join(result))
1.1.3. CDays-3
根据 [http://www.woodpecker.org.cn/diveintopython/scripts_and_streams/command_line_arguments.html DiPy 10.6. 处理命令行参数] 使用getopt.getopt() 优化当前功能函式。
1 # coding=utf-8
2 '''Lovely Python -3 PyDay
3 PyCDC v0.3
4 '''
5 import os,sys
6 import getopt
7
8 CDROM = '/media/cdrom0'
9 def cdWalker(cdrom,cdcfile):
10 export = ""
11 for root, dirs, files in os.walk(cdrom):
12 export+="\n %s;%s;%s" % (root,dirs,files)
13 open(cdcfile, 'w').write(export)
14
15 def usage():
16 print '''PyCDC 使用方式:
17 python cdays-3-exercise-1.py -d cdc -k 中国火
18 #搜索 cdc 目录中的光盘信息,寻找有“中国火”字样的文件或是目录,在哪张光盘中
19 '''
20
21 try:
22 opts, args = getopt.getopt(sys.argv[1:], 'hd:e:k:')
23 except getopt.GetoptError:
24 usage()
25 sys.exit()
26
27 if len(opts) == 0:
28 usage()
29 sys.exit()
30
31 c_path = ''
32 for opt, arg in opts:
33 if opt in ('-h', '--help'):
34 usage()
35 sys.exit()
36 elif opt == '-e':
37 #判别sys.argv[2]中是否有目录,以便进行自动创建
38 #cdWalker(CDROM, arg)
39 print "记录光盘信息到 %s" % arg
40 elif opt == '-d':
41 c_path = arg
42 elif opt == '-k':
43 if not c_path:
44 usage()
45 sys.exit()
46 #进行文件搜索
读取某一简单索引文件cdays-3-test.txt,其每行格式为文档序号 关键词,现需根据这些信息转化为倒排索引,即统计关键词在哪些文档中,格式如下:包含该关键词的文档数 关键词 => 文档序号。其中,原索引文件作为命令行参数传入主程序,并设计一个collect函数统计关键字-序号结果对,最后在主程序中输出结果至屏幕。
1 #coding:utf-8
2 '''cdays-3-exercise-2.py using sys.args, dict and function invoke
3 '''
4 #导入sys模块
5 import sys
6
7 def collect(file):
8 ''' function collect: change the key-value to value-key
9 @param file: file object
10 @return: a dict type, get the value-key pairs
11 '''
12 result = {}
13 #依次读取每行
14 for line in file.readlines():
15 #将一行以空格分割为左右两部分
16 left, right = line.split()
17 #判断是否已经含有right值对应的key
18 if result.has_key(right):
19 #若有,直接添加到result[right]的值列表
20 result[right].append(left)
21 else:
22 #没有,则新建result[right]的值列表
23 result[right] = [left]
24 return result
25
26 if __name__ == "__main__":
27 #判断参数个数
28 if len(sys.argv) == 1:
29 print 'usage:\n\tpython cdays-3-exercise-2.py cdays-3-test.txt'
30 else:
31 #调用collect函数,返回结果
32 result = collect(open(sys.argv[1], 'r'))
33 #输出结果
34 for (right, lefts) in result.items():
35 print "%d '%s'\t=>\t%s" % (len(lefts), right, lefts)
- 八皇后问题。在8*8的棋盘上,放置8个皇后,使得任两个皇后不在同行同列同正负对角线上。
1 #coding:utf-8
2 '''cdays-3-exercise-3.py using global varibles, invoke function recursively
3 '''
4 #定义一些全局变量
5 global col
6 global row
7 global pos_diag
8 global nag_diag
9 global count
10
11 def output():
12 ''' function output: print out one state
13 '''
14 global count
15 print row
16 count += 1
17
18 def do_queen(i):
19 ''' function do_queen: generate all states of queens' position
20 @param i: the number of queen
21 '''
22 #依次尝试0~7位置
23 for j in range(0, 8):
24 #若该行,正对角线,负对角线上都没有皇后,则放入i皇后
25 if col[j] == 1 and pos_diag[i-j+7] == 1 and nag_diag[i+j] == 1:
26 row[i] = j
27 #调整各个列表状态
28 col[j] = 0
29 pos_diag[i-j+7] = 0
30 nag_diag[i+j] = 0
31 if i < 7:
32 #可递增或递减
33 do_queen(i+1)
34 else:
35 #产生一个结果,输出
36 output()
37 #恢复各个列表状态为之前的
38 col[j] = 1
39 pos_diag[i-j+7] = 1
40 nag_diag[i+j] = 1
41
42 if __name__ == '__main__':
43 #矩阵列的列表,存储皇后所在列,若该列有皇后,则相应置为1,反之则0
44 col = []
45 #矩阵行的列表,存放每行皇后所在的位置,随着程序的执行,在不断的变化中,之间输出结果
46 row = []
47 #正对角线,i-j恒定,-7~0~7,并且b(i)+7统一到0~14
48 pos_diag = []
49 #负对角线,i+j恒定,0~14
50 nag_diag = []
51 count = 0
52 #一些初始化工作
53 for index in range(0, 8):
54 col.append(1)
55 row.append(0)
56 for index in range(0, 15):
57 pos_diag.append(1)
58 nag_diag.append(1)
59 #开始递归,先放一个,依次递增,反过来,从7开始递减也可
60 do_queen(0)
61 print 'Totally have %d solutions!' % count
1.1.4. CDays-2
把最后探索出的 cdcGrep()嵌入 pycdc-v0.5.py 实现完成版本的 PyCDC
1 # coding= utf-8
2 '''pycdc-v0.5.py
3 Lovely Python -2 PyDay
4 '''
5 import sys, cmd
6 from cdctools import *
7 class PyCDC(cmd.Cmd):
8 def __init__(self):
9 cmd.Cmd.__init__(self) # initialize the base class
10 self.CDROM = '/media/cdrom0'
11 self.CDDIR = 'cdc/'
12 self.prompt="(PyCDC)>"
13 self.intro = '''PyCDC0.5 使用说明:
14 dir 目录名 #指定保存和搜索目录,默认是 "cdc"
15 walk 文件名 #指定光盘信息文件名,使用 "*.cdc"
16 find 关键词 #遍历搜索目录中所有.cdc文件,输出含有关键词的行
17 ? # 查询
18 EOF # 退出系统,也可以使用Crtl+D(Unix)|Ctrl+Z(dos/windows)
19 '''
20
21 def help_EOF(self):
22 print "退出程序 Quits the program"
23 def do_EOF(self, line):
24 sys.exit()
25
26 def help_walk(self):
27 print "扫描光盘内容 walk cd and export into *.cdc"
28 def do_walk(self, filename):
29 if filename == "":filename = raw_input("输入cdc文件名:: ")
30 print "扫描光盘内容保存到:'%s'" % filename
31 cdWalker(self.CDROM,self.CDDIR+filename)
32
33 def help_dir(self):
34 print "指定保存/搜索目录"
35 def do_dir(self, pathname):
36 if pathname == "": pathname = raw_input("输入指定保存/搜索目录: ")
37 self.CDDIR = pathname
38 print "指定保存/搜索目录:'%s' ;默认是:'%s'" % (pathname,self.CDDIR)
39
40 def help_find(self):
41 print "搜索关键词"
42 def do_find(self, keyword):
43 if keyword == "": keyword = raw_input("输入搜索关键字: ")
44 print "搜索关键词:'%s'" % keyword
45 cdcGrep(self.CDDIR,keyword)
46
47 if __name__ == '__main__': # this way the module can be
48 cdc = PyCDC() # imported by other programs as well
49 cdc.cmdloop()
- 编写一个类,实现简单的栈。数据的操作按照先进后出(FILO)的顺序。主要成员函数为put(item),实现数据item插入栈中;get(),实现从栈中取一个数据。
1 #coding:utf-8
2 '''cdays-2-exercise-2.py using class and object
3 '''
4
5 class MyStack(object):
6 '''MyStack
7 user-defined stack, has basic stack operations, such as put(), get() and isEmpty()
8 '''
9 def __init__(self, max):
10 '''
11 init the stack: the head of stack and empty the stack
12 @param max: specify the max length of stack
13 '''
14 self.head = -1
15 self.stack = list()
16 self.max = max
17 for i in range(self.max):
18 self.stack.append(0)
19
20 def put(self, item):
21 '''
22 put item into the stack
23 @param item: anything want to put the current stack
24 '''
25 if self.head >= self.max:#判断当前栈是否满了
26 return 'Put Error: The Stack is Overflow!'#提示栈溢出
27 else:
28 self.head += 1#不满,则将item入栈,调整栈顶指针
29 self.stack[self.head] = item
30
31 def get(self):
32 '''
33 get the top item of current stack
34 @return: the top item
35 '''
36 if self.head < 0:#判断当前栈是否为空
37 return 'Get Error: The Stack is Empty!' #提示栈空
38 else:
39 self.head -= 1 #出栈,返回栈顶元素,并调整栈顶指针
40 return self.stack[self.head+1]
41
42 def isEmpty(self):
43 '''
44 get the state of current stack
45 @return: True(the stack is empty) or False(else)
46 '''
47 if self.head < -1:
48 return True
49 return False
50
51 if __name__ == "__main__":
52 mystack = MyStack(100)
53 mystack.put('a')
54 mystack.put('b')
55 print mystack.get()
56 mystack.put('c')
57 print mystack.get()
58 print mystack.get()
59 print mystack.get()
1.1.5. CDays-1
- 自动判定你自个儿的Blog 是什么编码的?
1 #coding:utf-8
2 '''cdays-1-exercise-1.py using chardet and urllib2
3 '''
4
5 import sys
6 import urllib2
7 import chardet
8
9 def blog_detect(blogurl):
10 '''
11 detect the coding of blog
12 @param blogurl: some url string
13 '''
14 try:
15 fp = urllib2.urlopen(blogurl)#尝试打开给定url
16 except Exception, e:#若产生异常,则给出相关提示并返回
17 print e
18 print 'download exception %s' % blogurl
19 return 0
20 blog = fp.read()#读取内容
21 codedetect = chardet.detect(blog)["encoding"]#检测得到编码方式
22 print '%s\t<-\t%s' % (blogurl, codedetect)
23 fp.close()#关闭
24 return 1
25
26 if __name__ == "__main__":
27 if len(sys.argv) == 1:
28 print 'usage:\n\tpython cdays-1-exercise-1.py http://xxxx.com'
29 else:
30 blog_detect(sys.argv[1])
- 在题一基础上,如果blog的编码不是utf-8,编写小程序自动将其转换成utf-8 编码保存到本地?
1 #coding:utf-8
2 '''cdays-1-exercise-2.py using chardet, urllib2
3 '''
4 import sys
5 import urllib2
6 import chardet
7
8 def blog_detect(blogurl):
9 '''
10 detect the coding of blog and save the blog in utf-8
11 @param blogurl: some url string
12 '''
13 try:
14 fp = urllib2.urlopen(blogurl)#尝试打开给定url
15 except Exception, e:#若产生异常,则给出相关提示并返回
16 print e
17 print 'download exception %s' % blogurl
18 return 0
19 blog = fp.read()#读取内容
20 fp.close()#关闭
21 codedetect = chardet.detect(blog)["encoding"]#检测得到编码方式
22 if codedetect <> 'utf-8':#是否是utf-8
23 try:
24 blog = unicode(blog, codedetect) #不是的话,则尝试转换
25 blog = blog.encode('utf8')
26 except:
27 print u"bad unicode encode try!"
28 return 0
29 filename = '%s_utf-8' % blogurl[7:]#保存入文件
30 filename = filename.replace('/', '_')
31 open(filename, 'w').write('%s' % blog)
32 print 'save to file %s' % filename
33 return 1
34
35 if __name__ == "__main__":
36 if len(sys.argv) == 1:
37 print 'usage:\n\tpython cdays-1-exercise-2.py http://xxxx.com'
38 else:
39 blog_detect(sys.argv[1])
1.1.6. CDays-0
- 请根据软件发布的流程和软件开发的编码规范,将读者之前章节写的程序修改并发布出去。另外,可以查找下除了epydoc外还有哪些较好的py文档生成器?
- 步骤:
- 编写epydoc的配置文件,如下cdays0-epydoc.cfg。
- 在终端中输入epydoc --config cdays0-epydoc.cfg,即可生成文档。
[epydoc] # Epydoc section marker (required by ConfigParser) # Information about the project. name: MyStack url: http://wiki.woodpecker.org.cn/moin/ObpLovelyPython # The list of modules to document. Modules can be named using # dotted names, module filenames, or package directory names. # This option may be repeated. modules: ./cdays0-exercise-1.py # Write html output to the directory "apidocs" output: html target: apidocs/ # Include all automatically generated graphs. These graphs are # generated using Graphviz dot. graph: all dotpath: /usr/bin/dot
1.1.7. CDays+1
- 编程实现以下功能并进行最大化的优化:遍历指定目录下的所有文件,找出其中占用空间最大的前3个文件。
1 #coding:utf-8
2 '''cdays+1-exercise-1.py using os.stat
3 '''
4 import sys
5 import os
6
7 def get_top_three(path):
8 ''' get the three big size of files in the path
9 @param path: specify the files of path
10 @return one list contains item like (size, filename)
11 '''
12 all_file = {}
13 for root, dirs, files in os.walk(path):#遍历path
14 for onefile in files:
15 fname = os.path.join(root, onefile)#获得当前处理文件的完整名字
16 fsize = os.stat(fname).st_size#获得当前处理文件大小
17 if all_file.has_key(fsize):#按照文件大小存储
18 all_file[fsize].append(fname)
19 else:
20 all_file[fsize] = [fname]
21 fsize_key = all_file.keys()#得到所有的文件大小
22 fsize_key.sort()#排序,从小到大
23 result = []
24 for i in [-1, -2, -3]:#依次取最大的三个
25 for j in all_file[fsize_key[i]]:#保存
26 result.append((fsize_key[i], j))
27 return result[:3]#返回前三个
28
29 if __name__ == "__main__":
30 if len(sys.argv) == 1:
31 print 'usage:\n\tpython cdays+1-exercise-1.py path'
32 else:
33 abs_path = os.path.abspath(sys.argv[1])#得到绝对路径
34 if not os.path.isdir(abs_path): #判断所给的路径是否存在
35 print '%s is not exist' % abs_path
36 else:
37 top = get_top_three(abs_path)
38 for (s, f) in top:
39 print '%s\t->\t%s' % (s, f)
利用ConfigParser,将上述题目中产生的结果按照cdays+1-my.ini格式存储到文件cdays+1-result.txt中。
1 #coding:utf-8
2 '''cdays+1-exercise-2.py using ConfigParser
3 '''
4 import os
5 import sys
6 from ConfigParser import RawConfigParser
7
8 def iniTT(size_file):
9 ''' 按照.ini的格式,存储size_file
10 '''
11 cfg = RawConfigParser()
12 print size_file
13 index = 1
14 for (s, f) in size_file:
15 cfg.add_section("%d" % index)#增加一个section
16 cfg.set("%d" % index, 'Filename', f)#在该section设置Filename及其值
17 cfg.set("%d" % index, 'FileSize', s)#在该section设置FileSize及其值
18 index += 1
19
20 cfg.write(open('cdays+1-result.txt',"w"))
21
22 def gtt(path):
23 ''' get the three big size of files in the path
24 @param path: specify the files of path
25 @return one list contains item like (size, filename)
26 '''
27 all_file = {}
28 for root, dirs, files in os.walk(path): #遍历path
29 for onefile in files:
30 fname = os.path.join(root, onefile)#获得当前处理文件的完整名字
31 fsize = os.stat(fname).st_size#获得当前处理文件大小
32 if all_file.has_key(fsize):#按照文件大小存储
33 all_file[fsize].append(fname)
34 else:
35 all_file[fsize] = [fname]
36 fsize_key = all_file.keys()#得到所有的文件大小
37 fsize_key.sort()#排序,从小到大
38 result = []
39 for i in [-1, -2, -3]:#依次取最大的三个
40 for j in all_file[fsize_key[i]]:#保存
41 result.append((fsize_key[i], j))
42 return result[:3]#返回前三个
43
44 if __name__ == "__main__":
45 if len(sys.argv) == 1:
46 print 'usage:\n\tpython cdays+1-exercise-2.py path'
47 else:
48 abs_path = os.path.abspath(sys.argv[1])
49 if not os.path.isdir(abs_path):
50 print '%s is not exist' % abs_path
51 else:
52 #from cdays+1-exercise-1 import get_top_three as gtt
53 iniTT(gtt(abs_path))
1.1.8. CDays+2
- 如果在Karrigell 实例中,不复制 cdctools.py 到webapp 目录中,也可以令 index.ks 引用到?
- 修改python的环境变量PYTHONPATH,把cdctools.py的所在目录路径加入
- 在程序里动态的修改sys.path
,如下所示
- 经过本章Karrigell的初步学习,实现一个简易的web留言系统。主要利用Karrigell_quick_form实现提交留言并显示出来。
- 具体步骤:
- 下载karrigell,解压后,根据默认设置直接就可以运行了,但一般修改conf/下Karrigell.ini中的port=8081,表示使用端口8081,保存
将msg拷贝至webapps/,并在index.pih中增加链接<a href='msg/'> Message</a>
- 编辑msg中的index.ks,完成所需功能
- cd至karrigell所在目录,输入python Karrigell.py运行后,在浏览器地址栏中输入localhost:8081就可以看到页面,点击Message链接即可到达。
- 具体步骤:
1 # -*- coding: utf-8 -*-
2
3 import os,sys
4 import pickle # 神奇的序列化模块
5 from HTMLTags import * # Karrigell 提供页面输出支持模块
6 from Karrigell_QuickForm import Karrigell_QuickForm as KQF
7
8 def _htmhead(title):
9 '''默认页面头声明
10 @note: 为了复用,特别的组织成独立函式,根据Karrigell 非页面访问约定,函式名称前加"_"
11 @param title: 页面标题信息
12 @return: 标准的HTML代码
13 '''
14 htm = """<html><HEAD>
15 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
16 <title>%s</title></HEAD>
17 <body>"""%title
18 return htm
19 ## 默认页面尾声明
20 htmfoot="""
21 <h5>design by:<a href="mailto:[email protected]">lizzie</a>
22 powered by : <a href="http://python.org">Python</a> +
23 <a href="http://karrigell.sourceforge.net"> KARRIGELL 2.4.0</a>
24 </h5>
25 </body></html>"""
26
27 def index(**args):
28 '''默认主
29 @note: 使用简单的表单/链接操作来完成原有功能的界面化
30 @param args: 数组化的不定参数
31 @return: 标准的HTML页面
32 '''
33 print _htmhead("Leave Messages")
34 p = KQF('fm_message','POST',"index","Message")
35 p.addHtmNode('text','yname','Name',{'size':20,'maxlength':20})
36 p.addTextArea('Words','10','90')
37 p.addGroup(["submit","btn_submit","Submit","btn"])
38 p.display()
39
40 if 0 == len(QUERY):
41 pass
42 else:
43 if "Submit" in QUERY['btn_submit']:
44 yname = QUERY['yname']
45 ywords = QUERY['Words']
46 if 0 == len(ywords):
47 print H3("please say something!")
48 else:
49 if 0 == len(yname):
50 yname = 'somebody'
51 try:
52 msg = pickle.load(open("message.dump"))
53 except:
54 msg = []
55 msg.append((yname, ywords))
56 index = len(msg)-1
57 while index >= 0:
58 print H5(msg[index][0]+' says: ')
59 print P('------ '+msg[index][1])
60 index -= 1
61 pickle.dump(msg,open("message.dump","w"))
62 else:
63 pass
64 print htmfoot
1.1.9. CDays+3
- 熟悉线程相关知识后,利用Lock和RLock实现线程间的简单同步,使得10个线程对同一共享变量进行递增操作,使用加锁机制保证变量结果的正确。
1 #coding:utf-8
2 '''cdays+3-exercise-1.py using Thread and RLock
3 '''
4 from threading import Thread
5 from threading import RLock
6 import time
7
8 class myThread(Thread):
9 '''myThread
10 user-defined thread
11 '''
12 def __init__(self, threadname):
13 Thread.__init__(self, name = threadname)
14
15 def run(self):
16 global share_var#共享一全局变量
17 lock.acquire()#调用lock的acquire,获得锁
18 share_var += 1#修改共享变量
19 #time.sleep(2)
20 print share_var
21 lock.release()#释放
22
23 if __name__ == "__main__":
24 share_var = 0
25 lock = RLock()
26 threadlist = []
27
28 for i in range(10):#产生10个线程
29 my = myThread('Thread%d' % i)
30 threadlist.append(my)
31 for i in threadlist:#开始10个线程
32 i.start()
- 使用Queue实现多线程间的同步。比如说,十个输入线程从终端输入字符串,另十个输出线程依次获取字符串并输出到屏幕。
1 #coding:utf-8
2 '''cdays+3-exercise-2.py using Thread and Queue
3 @author: U{shengyan<mailto:[email protected]>}
4 @version:$Id$
5 '''
6 from threading import Thread
7 import Queue
8 import time
9
10 class Input(Thread):
11 '''Input Thread: read one string from stdio, then add this string into queue
12 '''
13 def __init__(self, threadname):
14 Thread.__init__(self, name = threadname)
15 def run(self):
16 some_string = raw_input('please input something for thread %s:' % self.getName())#输入一个字符串
17 global queue
18 queue.put((self.getName(), some_string))#加入到队列
19 #time.sleep(5)#延时一段时间
20
21 class Output(Thread):
22 '''Output Thread: read one string from queue, then print it out
23 '''
24 def __init__(self, threadname):
25 Thread.__init__(self, name = threadname)
26 def run(self):
27 global queue
28 (iThread, something) = queue.get()#从queue中读取
29 print 'Thread %s get "%s" from Thread %s' % (self.getName(), something, iThread)#输出
30
31 if __name__ == "__main__":
32 queue = Queue.Queue()#创建Queue对象
33 inputlist = []
34 outputlist = []
35 for i in range(10):
36 il = Input('InputThread%d' % i) #输入线程列表
37 inputlist.append(il)
38 ol = Output('outputThread%d' % i)#输出线程列表
39 outputlist.append(ol)
40 for i in inputlist:
41 i.start()#依次开始输入线程
42 i.join()#等待
43 for i in outputlist:
44 i.start()#依次开始输出线程
45 #i.join()
- Python中的Event是用于线程间的相互通信,主要利用信号量机制。修改题一的程序,利用信号量重新实现多线程对同一共享变量进行递增操作。
1 #coding:utf-8
2 '''cdays+3-exercise-3.py using Thread and Event
3 '''
4 from threading import Thread
5 from threading import Event
6 import time
7
8 class myThread(Thread):
9 '''myThread
10 user-defined thread
11 '''
12 def __init__(self, threadname):
13 Thread.__init__(self, name = threadname)
14
15 def run(self):
16 global event
17 global share_var
18 if event.isSet():#判断event的信号标志
19 event.clear()#若设置了,则清除
20 event.wait()#并调用wait方法
21 #time.sleep(2)
22 share_var += 1#修改共享变量
23 print '%s ==> %d' % (self.getName(), share_var)
24 else:
25 share_var += 1#未设置,则直接修改
26 print '%s ==> %d' % (self.getName(), share_var)
27 #time.sleep(1)
28 event.set()#设置信号标志
29
30 if __name__ == "__main__":
31 share_var = 0
32 event = Event()#创建Event对象
33 event.set()#设置内部信号标志为真
34 threadlist = []
35
36 for i in range(10):#创建10个线程
37 my = myThread('Thread%d' % i)
38 threadlist.append(my)
39 for i in threadlist:#开启10个线程
40 i.start()
1.2. KDays
1.3. 小结