Skip to content

Commit 706eee9

Browse files
committed
Merge branch 'master' of github.com:michaelliao/learn-python3
2 parents 57a27e5 + 9988a95 commit 706eee9

File tree

5 files changed

+92
-27
lines changed

5 files changed

+92
-27
lines changed

samples/advance/do_generator.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,9 @@ def fib(max):
2323
g = fib(5)
2424
while 1:
2525
try:
26-
x = g.send(None)
26+
x = next(g)
2727
print('g:', x)
2828
except StopIteration as e:
2929
print('Generator return value:', e.value)
3030
break
3131

32-
# call generator using iter:
33-
i = iter(fib(5))
34-
while 1:
35-
try:
36-
r = next(i)
37-
print('i:', r)
38-
except StopIteration as e:
39-
print('Generator return value:', e.value)
40-
break

samples/advance/do_iter.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
from collections import Iterable
5-
print('iterable? [1, 2, 3]:', isinstance([1, 2, 3], Iterable))
6-
print('iterable? \'abc\':', isinstance('abc', Iterable))
7-
print('iterable? 123:', isinstance(123, Iterable))
4+
from collections import Iterable, Iterator
5+
6+
def g():
7+
yield 1
8+
yield 2
9+
yield 3
10+
11+
print('Iterable? [1, 2, 3]:', isinstance([1, 2, 3], Iterable))
12+
print('Iterable? \'abc\':', isinstance('abc', Iterable))
13+
print('Iterable? 123:', isinstance(123, Iterable))
14+
print('Iterable? g():', isinstance(g(), Iterable))
15+
16+
print('Iterator? [1, 2, 3]:', isinstance([1, 2, 3], Iterator))
17+
print('Iterator? iter([1, 2, 3]):', isinstance(iter([1, 2, 3]), Iterator))
18+
print('Iterator? \'abc\':', isinstance('abc', Iterator))
19+
print('Iterator? 123:', isinstance(123, Iterator))
20+
print('Iterator? g():', isinstance(g(), Iterator))
821

922
# iter list:
10-
print('iter [1, 2, 3, 4, 5]')
23+
print('for x in [1, 2, 3, 4, 5]:')
1124
for x in [1, 2, 3, 4, 5]:
1225
print(x)
1326

27+
print('for x in iter([1, 2, 3, 4, 5]):')
28+
for x in iter([1, 2, 3, 4, 5]):
29+
print(x)
30+
31+
print('next():')
32+
it = iter([1, 2, 3, 4, 5])
33+
print(next(it))
34+
print(next(it))
35+
print(next(it))
36+
print(next(it))
37+
print(next(it))
38+
1439
d = {'a': 1, 'b': 2, 'c': 3}
1540

1641
# iter each key:

samples/functional/do_reduce.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,39 @@ def str2int(s):
2323
print(str2int('0'))
2424
print(str2int('12300'))
2525
print(str2int('0012345'))
26+
27+
CHAR_TO_FLOAT = {
28+
'0': 0,
29+
'1': 1,
30+
'2': 2,
31+
'3': 3,
32+
'4': 4,
33+
'5': 5,
34+
'6': 6,
35+
'7': 7,
36+
'8': 8,
37+
'9': 9,
38+
'.': -1
39+
}
40+
41+
def str2float(s):
42+
nums = map(lambda ch: CHAR_TO_FLOAT[ch], s)
43+
point = -1
44+
def to_float(f, n):
45+
nonlocal point
46+
if n == -1:
47+
point = 0
48+
return f
49+
if point == -1:
50+
return f * 10 + n
51+
else:
52+
point = point + 1
53+
return f + n / pow(10, point)
54+
return reduce(to_float, nums, 0.0)
55+
56+
print(str2float('0'))
57+
print(str2float('123.456'))
58+
print(str2float('123.45600'))
59+
print(str2float('0.1234'))
60+
print(str2float('.1234'))
61+
print(str2float('120.0034'))

samples/functional/do_sorted.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,8 @@
88
print(sorted(L))
99
print(sorted(L, key=str.lower))
1010

11-
students = [
12-
('Adam', 90),
13-
('Tim', 60),
14-
('Lisa', 80),
15-
('Bart', 60)
16-
]
11+
students = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
1712

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))
13+
print(sorted(students, key=itemgetter(0)))
14+
print(sorted(students, key=lambda t: t[1]))
15+
print(sorted(students, key=itemgetter(1), reverse=True))

samples/module/hello.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
' a test module '
5+
6+
__author__ = 'Michael Liao'
7+
8+
import sys
9+
10+
def test():
11+
args = sys.argv
12+
if len(args)==1:
13+
print('Hello, world!')
14+
elif len(args)==2:
15+
print('Hello, %s!' % args[1])
16+
else:
17+
print('Too many arguments!')
18+
19+
if __name__=='__main__':
20+
test()
21+

0 commit comments

Comments
 (0)