Skip to content

Commit 68ab6ce

Browse files
committed
chap 3 going on a picnic
1 parent 8b49d04 commit 68ab6ce

5 files changed

Lines changed: 195 additions & 2 deletions

File tree

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ tex2pdf*
1111
.coverage
1212
.idea
1313
.vscode
14-
02_crowsnest/crowsnest.py
15-
03_picnic/picnic.py
1614
04_jump_the_five/jump.py
1715
05_howler/howler.py
1816
06_wc/wc.py

01_hello/hello.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/Users/yida.wang/miniconda3/envs/py39/bin/python3
2+
"""
3+
Author: Yida Wang <yidaalex.wang@gmail.com>
4+
Purpose: Say hello
5+
"""
6+
7+
import argparse
8+
9+
10+
def get_args():
11+
"""Get the command-line arguments"""
12+
13+
parser = argparse.ArgumentParser(description='Say hello')
14+
parser.add_argument('-n', '--name', metavar='name',
15+
default='World', help='Name to greet')
16+
args = parser.parse_args()
17+
return args
18+
19+
20+
def main():
21+
"""Make a jazz noise here"""
22+
23+
args = get_args()
24+
print('Hello, ' + args.name + '!')
25+
26+
27+
if __name__ == '__main__':
28+
main()

01_hello/hello2.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Yida Wang <yidaalex.wang@gmail.com>
4+
Date : 2021-06-27
5+
Purpose: Rock the Casbah
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description='Rock the Casbah',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('positional',
20+
metavar='str',
21+
help='A positional argument')
22+
23+
parser.add_argument('-a',
24+
'--arg',
25+
help='A named string argument',
26+
metavar='str',
27+
type=str,
28+
default='')
29+
30+
parser.add_argument('-i',
31+
'--int',
32+
help='A named integer argument',
33+
metavar='int',
34+
type=int,
35+
default=0)
36+
37+
parser.add_argument('-f',
38+
'--file',
39+
help='A readable file',
40+
metavar='FILE',
41+
type=argparse.FileType('rt'),
42+
default=None)
43+
44+
parser.add_argument('-o',
45+
'--on',
46+
help='A boolean flag',
47+
action='store_true')
48+
49+
return parser.parse_args()
50+
51+
52+
# --------------------------------------------------
53+
def main():
54+
"""Make a jazz noise here"""
55+
56+
args = get_args()
57+
str_arg = args.arg
58+
int_arg = args.int
59+
file_arg = args.file
60+
flag_arg = args.on
61+
pos_arg = args.positional
62+
63+
print(f'str_arg = "{str_arg}"')
64+
print(f'int_arg = "{int_arg}"')
65+
print('file_arg = "{}"'.format(file_arg.name if file_arg else ''))
66+
print(f'flag_arg = "{flag_arg}"')
67+
print(f'positional = "{pos_arg}"')
68+
69+
70+
# --------------------------------------------------
71+
if __name__ == '__main__':
72+
main()

02_crowsnest/crowsnest.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Yida Wang <yidaalex.wang@gmail.com>
4+
Date : 2021-07-01
5+
Purpose: Crow's Nest
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description='Crow\'s Nest -- choose the correct article',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('word', metavar='word', help='A word')
20+
21+
return parser.parse_args()
22+
23+
24+
# --------------------------------------------------
25+
def main():
26+
"""Make a jazz noise here"""
27+
28+
args = get_args()
29+
word = args.word
30+
article = 'an' if word[0].lower() in 'aeiou' else 'a'
31+
32+
print(f'Ahoy, Captain, {article} {word} off the larboard bow!')
33+
34+
35+
# --------------------------------------------------
36+
if __name__ == '__main__':
37+
main()

03_picnic/picnic.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Yida Wang <yidaalex.wang@gmail.com>
4+
Date : 2021-07-02
5+
Purpose: Picnic game
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description='Picnic game',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('items',
20+
metavar='str',
21+
nargs='+',
22+
help='Item(s) to bring')
23+
24+
parser.add_argument('-s',
25+
'--sorted',
26+
help='Sort the items',
27+
action='store_true')
28+
29+
return parser.parse_args()
30+
31+
32+
# --------------------------------------------------
33+
def main():
34+
"""Make a jazz noise here"""
35+
36+
args = get_args()
37+
items = args.items
38+
to_sort = args.sorted
39+
40+
num = len(items)
41+
42+
if to_sort:
43+
items.sort()
44+
45+
bringing = ''
46+
if num == 1:
47+
bringing = items[0]
48+
elif num == 2:
49+
bringing = items[0] + ' and ' + items[1]
50+
else:
51+
bringing = ', '.join(items[:-1]) + ', and ' + items[-1]
52+
53+
print(f'You are bringing {bringing}.')
54+
55+
56+
# --------------------------------------------------
57+
if __name__ == '__main__':
58+
main()

0 commit comments

Comments
 (0)