Skip to content

Commit 8148ad6

Browse files
committed
python examples
1 parent b5ba3dd commit 8148ad6

19 files changed

Lines changed: 531 additions & 0 deletions

classModule.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
3+
' a class module '
4+
5+
# from testclass import TestImport
6+
7+
import testclass
8+
a = testclass.TestImport().getName
9+
a()
10+
11+
12+
import testclass
13+
a = testclass.TestImport()
14+
a.getName()
15+
16+
17+
if __name__ == '__main__':
18+
pass
19+
print('good')

client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
import socket
3+
import json
4+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5+
# 建立连接:
6+
s.connect(('127.0.0.1', 9999))
7+
# 接收欢迎消息:
8+
print(s.recv(1024).decode('utf-8'))
9+
k = 0
10+
dict = {'one':1,'two':2,'three':3,'four':4}
11+
for data in [b'Michael', b'Tracy', b'Sarah',b'lmm']:
12+
# 发送数据:
13+
# s.send(data)
14+
s.send(json.dumps(dict))
15+
print(s.recv(1024))
16+
17+
s.send(b'exit')
18+
s.close()

coroutine.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def consumer():
2+
r = ''
3+
while True:
4+
n = yield r
5+
if not n:
6+
return
7+
print('[CONSUMER] Consuming %s...' % n)
8+
r = '200 OK'
9+
10+
def produce(c):
11+
c.send(None)
12+
n = 0
13+
while n < 5:
14+
n = n + 1
15+
print('[PRODUCER] Producing %s...' % n)
16+
r = c.send(n)
17+
print('[PRODUCER] Consumer return: %s' % r)
18+
c.close()
19+
20+
c = consumer()
21+
produce(c)

dict.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
#可变数据类型,当赋值给另一个变量时,其实是做了一次引用,两个变量指向同一对象
3+
a = {'one':1,'two':2}
4+
b = a
5+
b['three'] = 3
6+
print(b,a)
7+
8+
9+
c = ['one','two']
10+
d = c
11+
d.append('three')
12+
print(d,c)
13+
14+
str = '-'
15+
str = ''.join(c)
16+
print(str)

export.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
from file import *
3+
import pdb
4+
5+
6+
fromFile = 'open.txt'
7+
toFile = 'write.txt'
8+
9+
# inStr = input('please input your file name:')
10+
# toFile = inStr+'.txt'
11+
# print(toFile)
12+
# readFile(fromFile,toFile)
13+
14+
test_list()
15+
16+
#调用可变参数
17+
list = [1,2,3,4]
18+
# tup = (1,2)
19+
# name = input('姓名:')
20+
# age = input('年龄:')
21+
# height = input('身高:')
22+
# list = [name,age,height]
23+
print(changeArg(*list))
24+
25+
#调用关键字参数
26+
dict = {'height':180}
27+
persion('lmm',18,**dict)
28+
29+
# pdb.set_trace()#打断点
30+
31+
print(recur(3))
32+
33+
l = []
34+
n = 1
35+
while n<=10:
36+
37+
l.append(n)
38+
n += 1
39+
40+
print(type(l))

file.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
def readFile(fromFile,toFile):
3+
#python2.x版本不支持encoding参数
4+
# with open(fromFile,'r',encoding="UTF-8") as f:
5+
with open(fromFile,'r') as f:
6+
# content = f.read()
7+
for line in f.readlines():
8+
writeFile(toFile,line)
9+
10+
11+
def writeFile(fileName,content):
12+
# with open(fileName,'a',encoding="UTF-8") as op:
13+
with open(fileName,'a') as op:
14+
op.write(content)
15+
16+
17+
def test_list(L = []):
18+
L.append('END')
19+
print(L)
20+
# def test_list(L = None):
21+
# L.append('END')
22+
# print(L)
23+
# test_list([1,2,3])
24+
# test_list()
25+
26+
#定义可变参数和定义一个list或tuple参数相比,仅仅在参数前面加了一个*号。
27+
#在函数内部,参数numbers接收到的是一个tuple
28+
#调用可变参数
29+
def changeArg(*nums):
30+
print(type(nums))
31+
sum = 0
32+
for i in nums:
33+
sum += i*i
34+
return sum
35+
36+
#关键字参数
37+
def persion(name,age,**arg):
38+
print('name:',name,'age:',age,'other:',arg)
39+
40+
41+
#递归函数
42+
def recur(n):
43+
if n == 1:
44+
return 1
45+
return n*recur(n-1)

first.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
# print('请输入你的名字:');
3+
# name = input();
4+
# print("你的名字是:",name);
5+
6+
# nameList = ['one','two','three']
7+
# print(nameList);
8+
# print(len(nameList));
9+
10+
11+
' a test module '
12+
13+
__author__ = 'Michael Liao'
14+
15+
import sys
16+
17+
def test():
18+
args = sys.argv
19+
if len(args)==1:
20+
print('Hello, world!')
21+
print(args)
22+
elif len(args)==2:
23+
print('Hello, %s!' % args[1])
24+
print(args)
25+
else:
26+
print('Too many arguments!')
27+
28+
if __name__=='__main__':
29+
test()

main.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
' a test module '
4+
5+
__author__ = 'lmm'
6+
7+
# import module
8+
# from ../module import test
9+
import sys
10+
sys.path.append("..") #从上级目录导入模块
11+
from module import test
12+
test()
13+
14+
if __name__ == '__main__':
15+
print('this is main')
16+
pass
17+
# test()
18+
# print(__name__)

module.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
' a test module '
4+
5+
__author__ = 'lmm'
6+
import sys
7+
8+
def test():
9+
args = sys.argv
10+
print(args[0],args[1],args[2])
11+
num = len(args)
12+
print(num)
13+
print(__name__)
14+
15+
#当我们在命令行运行该模块文件时,Python解释器把一个特殊变量__name__置为__main__
16+
if __name__ == '__main__':
17+
test()
18+
# print(__name__)

open.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1.我正在测试
2+
2.我正在测试
3+
3.我正在测试
4+
4.我正在测试
5+
5.this is a test content to write to txt file
6+
6.this is a test content to write to txt file
7+
7.this is a test content to write to txt file

0 commit comments

Comments
 (0)