Skip to content

Commit 22d92c0

Browse files
committed
add new samples
1 parent 55adb46 commit 22d92c0

12 files changed

Lines changed: 245 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
*.py[cod]
22

3+
test.db
4+
test.txt
5+
36
# C extensions
47
*.so
58

py3/commonlib/do_base64.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import base64
5+
6+
s = base64.b64encode('在Python中使用BASE 64编码'.encode('utf-8'))
7+
print(s)
8+
d = base64.b64decode(s).decode('utf-8')
9+
print(d)
10+
11+
s = base64.urlsafe_b64encode('在Python中使用BASE 64编码'.encode('utf-8'))
12+
print(s)
13+
d = base64.urlsafe_b64decode(s).decode('utf-8')
14+
print(d)

py3/coroutine/async_hello.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import asyncio
5+
6+
@asyncio.coroutine
7+
def hello():
8+
print("Hello world!")
9+
yield from asyncio.sleep(1)
10+
print("Hello again!")
11+
12+
loop = asyncio.get_event_loop()
13+
loop.run_until_complete(hello())
14+
loop.close()

py3/db/do_sqlite.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import sqlite3
5+
6+
# 连接到SQLite数据库
7+
# 数据库文件是test.db
8+
# 如果文件不存在,会自动在当前目录创建:
9+
conn = sqlite3.connect('test.db')
10+
# 创建一个Cursor:
11+
cursor = conn.cursor()
12+
# 执行一条SQL语句,创建user表:
13+
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
14+
# 继续执行一条SQL语句,插入一条记录:
15+
cursor.execute('insert into user (id, name) values (\'1\', \'Michael\')')
16+
# 通过rowcount获得插入的行数:
17+
print('rowcount =', cursor.rowcount)
18+
# 关闭Cursor:
19+
cursor.close()
20+
# 提交事务:
21+
conn.commit()
22+
# 关闭Connection:
23+
conn.close()
24+
25+
# 查询记录:
26+
conn = sqlite3.connect('test.db')
27+
cursor = conn.cursor()
28+
# 执行查询语句:
29+
cursor.execute('select * from user where id=?', '1')
30+
# 获得查询结果集:
31+
values = cursor.fetchall()
32+
print(values)
33+
cursor.close()
34+
conn.close()

py3/debug/do_try.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
try:
5+
print('try...')
6+
r = 10 / 0
7+
print('result:', r)
8+
except ZeroDivisionError as e:
9+
print('except:', e)
10+
finally:
11+
print('finally...')
12+
print('END')

py3/functional/decorator.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import functools
5+
6+
def log(func):
7+
@functools.wraps(func)
8+
def wrapper(*args, **kw):
9+
print('call %s():' % func.__name__)
10+
return func(*args, **kw)
11+
return wrapper
12+
13+
@log
14+
def now():
15+
print('2015-3-25')
16+
17+
now()
18+
19+
def logger(text):
20+
def decorator(func):
21+
@functools.wraps(func)
22+
def wrapper(*args, **kw):
23+
print('%s %s():' % (text, func.__name__))
24+
return func(*args, **kw)
25+
return wrapper
26+
return decorator
27+
28+
@logger('DEBUG')
29+
def today():
30+
print('2015-3-25')
31+
32+
today()
33+
print(today.__name__)

py3/functional/do_filter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
def is_odd(n):
5+
return n % 2 == 1
6+
7+
L = range(100)
8+
9+
print(list(filter(is_odd, L)))
10+
11+
def not_empty(s):
12+
return s and s.strip()
13+
14+
print(list(filter(not_empty, ['A', '', 'B', None, 'C', ' '])))

py3/functional/do_partial.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import functools
5+
6+
int2 = functools.partial(int, base=2)
7+
8+
print('1000000 =', int2('1000000'))
9+
print('1010101 =', int2('1010101'))

py3/functional/do_reduce.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
from functools import reduce
5+
6+
CHAR_TO_INT = {
7+
'0': 0,
8+
'1': 1,
9+
'2': 2,
10+
'3': 3,
11+
'4': 4,
12+
'5': 5,
13+
'6': 6,
14+
'7': 7,
15+
'8': 8,
16+
'9': 9
17+
}
18+
19+
def str2int(s):
20+
ints = map(lambda ch: CHAR_TO_INT[ch], s)
21+
return reduce(lambda x, y: x * 10 + y, ints)
22+
23+
print(str2int('0'))
24+
print(str2int('12300'))
25+
print(str2int('0012345'))

py3/functional/do_sorted.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
from operator import itemgetter
5+
6+
L = ['bob', 'about', 'Zoo', 'Credit']
7+
8+
print(sorted(L))
9+
print(sorted(L, key=str.lower))
10+
11+
students = [
12+
('Adam', 90),
13+
('Tim', 60),
14+
('Lisa', 80),
15+
('Bart', 60)
16+
]
17+
18+
print(sorted(students, key=itemgetter(1)))
19+
20+
def student_to_key(t):
21+
return '%+02d%s' % (100-t[1], t[0])
22+
23+
print(sorted(students, key=student_to_key))

0 commit comments

Comments
 (0)