Skip to content

Commit 6c5096c

Browse files
author
bianxiaokai
committed
模块
1 parent b2232ab commit 6c5096c

File tree

12 files changed

+236
-20
lines changed

12 files changed

+236
-20
lines changed

ex.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
PI = 3.14
3+
4+
5+
def get_sum(lst):
6+
total = 0
7+
for v in lst:
8+
total = total + v
9+
return total
10+

module/binary.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�Z�8��J

module/ex.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
PI = 3.14
3+
4+
5+
def get_sum(lst):
6+
total = 0
7+
for v in lst:
8+
total = total + v
9+
return total

module/file.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# f = open('test.txt', 'w+')
2+
# f.write('hello world. morning.')
3+
# f.seek(0)
4+
# print(f.read()) # hello world.
5+
#
6+
# f = open('test.txt', 'r')
7+
# lines = f.readlines()
8+
# print(lines)
9+
# f.close()
10+
11+
# 事实上,我们可以将 f 放在一个循环中,得到它每一行的内容:
12+
# f = open('test.txt')
13+
# for line in f:
14+
# print(line)
15+
# f.close()
16+
17+
import os
18+
import math
19+
20+
21+
# f = open('binary.bin', 'wb')
22+
# f.write(os.urandom(10))
23+
# f.close()
24+
25+
# while True:
26+
# try:
27+
# text = input('>')
28+
# if text[0] == 'q':
29+
# break
30+
# x = float(text)
31+
# y = 1 / math.log10(x)
32+
# print('y', y)
33+
# print("1/log10({0}) = {1}".format(x, y))
34+
# except ValueError:
35+
# print("value must bigger than 0")
36+
# except ZeroDivisionError:
37+
# print("the value must not be 1")
38+
39+
class CommandError(ValueError):
40+
print("bad command operation. must input 'start', 'stop', 'pause'")
41+
42+
43+
valid_commands = {'start', 'stop', 'pause'}
44+
while True:
45+
command = input('>')
46+
if command == 'q':
47+
break
48+
try:
49+
if command.lower() not in valid_commands:
50+
raise CommandError('Invaild command: %s' % command)
51+
print('input command:', command)
52+
except CommandError:
53+
print("bad command string: %s" % command)
54+
finally:
55+
print('finally was called.')
56+

module/line.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from time import time
2+
3+
4+
def main():
5+
total = 0
6+
number_list = [x for x in range(1, 100000001)]
7+
start = time()
8+
for number in number_list:
9+
total += number
10+
print(total)
11+
end = time()
12+
print('Execution time: %.3fs' % (end - start))
13+
14+
15+
main()

module/module1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def foo():
2+
print('hello, world! -- by module1')

module/module2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def foo2():
2+
print('goodbye, world! -- by module2')

module/module_test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import sys
5+
import os
6+
from module1 import foo
7+
from module2 import foo2
8+
9+
10+
# from ex import PI, get_sum
11+
12+
' a test module '
13+
14+
__author__ = 'Michael Liao'
15+
16+
print(foo())
17+
print(foo2())
18+
19+
# print(PI)
20+
# print(get_sum([1, 2, 3, 4, 5]))
21+
22+
# os.remove('ex.py')
23+
with open('ex.py', 'w') as f:
24+
f.write("""
25+
PI = 3.14
26+
27+
28+
def get_sum(lst):
29+
total = 0
30+
for v in lst:
31+
total = total + v
32+
return total
33+
""")
34+
35+
36+
def test():
37+
args = sys.argv
38+
if len(args) == 1:
39+
print('Hello World')
40+
elif len(args) == 2:
41+
print('Hello, %s!' % args[1])
42+
else:
43+
print('Too many arguments!')
44+
45+
46+
print('__name__', __name__)
47+
48+
if __name__ == '__main__':
49+
test()

module/stys.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

module/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hello world.
2+
morning.

multi_process.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from multiprocessing import Process, Queue
2+
from time import time
3+
4+
core_num = 4
5+
6+
7+
def task_handler(curr_list, result_queue):
8+
print("start")
9+
total = 0
10+
for number in curr_list:
11+
total += number
12+
result_queue.put(total)
13+
14+
15+
def main():
16+
processes = []
17+
number_list = [x for x in range(1, 100000001)]
18+
result_queue = Queue()
19+
index = 0
20+
# 启动core_num(8)个进程将数据切片后进行运算
21+
index_batch = int(100000000 / core_num)
22+
for _ in range(core_num):
23+
p = Process(target=task_handler,
24+
args=(number_list[index:index + index_batch], result_queue))
25+
index += index_batch
26+
processes.append(p)
27+
p.start()
28+
# 开始记录所有进程执行完成花费的时间
29+
start = time()
30+
for p in processes:
31+
p.join()
32+
# 合并执行结果
33+
total = 0
34+
while not result_queue.empty():
35+
total += result_queue.get()
36+
print(total)
37+
end = time()
38+
print('Execution time: ', (end - start), 's', sep='')
39+
40+
41+
print('__name__', __name__)
42+
43+
if __name__ == '__main__':
44+
main()

multi_thread.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import time
5+
import tkinter
6+
import tkinter.messagebox
7+
from threading import Thread
8+
9+
10+
def main():
11+
12+
class DownloadTaskHandler(Thread):
13+
14+
def run(self):
15+
time.sleep(5)
16+
tkinter.messagebox.showinfo('提示', '下载完成!')
17+
# 启用下载按钮
18+
button1.config(state=tkinter.NORMAL)
19+
20+
def download():
21+
# 禁用下载按钮
22+
button1.config(state=tkinter.DISABLED)
23+
# 通过daemon参数将线程设置为守护线程(主程序退出就不再保留执行)
24+
# 在线程中处理耗时间的下载任务
25+
DownloadTaskHandler(daemon=True).start()
26+
27+
def show_about():
28+
tkinter.messagebox.showinfo('关于', '作者: 123(v1.0)')
29+
30+
top = tkinter.Tk()
31+
top.title('多线程')
32+
top.geometry('400x400')
33+
top.wm_attributes('-topmost', 1)
34+
35+
panel = tkinter.Frame(top)
36+
button1 = tkinter.Button(panel, text='下载', command=download)
37+
button1.pack(side='left')
38+
button2 = tkinter.Button(panel, text='关于', command=show_about)
39+
button2.pack(side='right')
40+
panel.pack(side='bottom')
41+
42+
tkinter.mainloop()
43+
44+
45+
if __name__ == '__main__':
46+
main()

0 commit comments

Comments
 (0)