Skip to content

Commit a1b9827

Browse files
committed
saved 4-27-20
1 parent bb88c48 commit a1b9827

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

01_hello/hello.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!py -3
2+
"""
3+
Author: Linnea Honeker
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+
return parser.parse_args()
17+
18+
19+
def main():
20+
"""Make a jazz noise here"""
21+
22+
args = get_args()
23+
print('Hello, ' + args.name + '!')
24+
25+
26+
if __name__ == '__main__':
27+
main()

01_hello/hello2.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#py -3
2+
"""
3+
Author : None
4+
Date : 2020-04-13
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('r'),
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!py -3
2+
"""
3+
Author : Linnea Honeker
4+
Date : 2020-04-13
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='Crows Nest -- choose the correct article',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('positional',
20+
metavar='word',
21+
help='A word')
22+
23+
return parser.parse_args()
24+
25+
26+
# --------------------------------------------------
27+
def main():
28+
"""Make a jazz noise here"""
29+
30+
args = get_args()
31+
word = args.positional
32+
33+
article = 'an' if word[0].lower() in 'aeiou' else 'a'
34+
35+
print('Ahoy, Captain, {} {} off the larboard bow!'.format(article, word))
36+
37+
38+
# --------------------------------------------------
39+
if __name__ == '__main__':
40+
main()

03_picnic/picnic.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!py -3
2+
"""
3+
Author : Linnea Honeker
4+
Date : 2020-04-13
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='Picnic game',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('positional',
20+
metavar='list',
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+
default=False)
29+
30+
return parser.parse_args()
31+
32+
33+
# --------------------------------------------------
34+
def main():
35+
"""Make a jazz noise here"""
36+
37+
args = get_args()
38+
sort_arg = args.sorted
39+
items_arg = args.positional
40+
41+
if sort_arg == True:
42+
items_arg.sort()
43+
44+
45+
if len(items_arg) == 1:
46+
str_items = items_arg
47+
print ('You are bringing {}'.format(str_items))
48+
else:
49+
sec_item = items_arg.pop()
50+
joined_items = ', '.join(items_arg)
51+
print ('You are bringing {} and {}'.format(joined_items, sec_item))
52+
#else:
53+
# last_item = items_arg.pop()
54+
# print ('You are bringing {} and {}'.format(items_arg, last_item))
55+
56+
57+
# --------------------------------------------------
58+
if __name__ == '__main__':
59+
main()

0 commit comments

Comments
 (0)