Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2e641b

Browse files
authored
Merge pull request #3 from JBSP-code/chapter_03
Chapter 03
2 parents 8635270 + c3007e1 commit b2e641b

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ tex2pdf*
1111
.coverage
1212
.idea
1313
.vscode
14-
03_picnic/picnic.py
1514
04_jump_the_five/jump.py
1615
05_howler/howler.py
1716
06_wc/wc.py

03_picnic/picnic.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Jeffrey Schmid-Paz
4+
Date : 2023-01-05
5+
Purpose: Create a list of items to bring along a picnic
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+
20+
parser.add_argument("item", metavar="str", help="Item(s) to bring", nargs="+")
21+
22+
parser.add_argument("-s", "--sorted", help="Sort the items", action="store_true")
23+
24+
return parser.parse_args()
25+
26+
27+
# --------------------------------------------------
28+
def main():
29+
"""Make a jazz noise here"""
30+
31+
args = get_args()
32+
items = args.item
33+
34+
if args.sorted == True:
35+
items.sort()
36+
37+
items_to_bring = ""
38+
if len(items) >= 3:
39+
items.insert(-1, "and ")
40+
items_to_bring = ", ".join(items[:-1]) + items[-1]
41+
elif len(items) == 2:
42+
items_to_bring = " and ".join(items)
43+
else:
44+
items_to_bring = items[0]
45+
46+
print(f"You are bringing {items_to_bring}.")
47+
48+
49+
# --------------------------------------------------
50+
if __name__ == "__main__":
51+
main()

03_picnic/test.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
from subprocess import getoutput
66

7-
prg = './picnic.py'
7+
prg = "picnic.py"
88

99

1010
# --------------------------------------------------
@@ -18,51 +18,52 @@ def test_exists():
1818
def test_usage():
1919
"""usage"""
2020

21-
for flag in ['', '-h', '--help']:
22-
out = getoutput(f'{prg} {flag}')
23-
assert out.lower().startswith('usage')
21+
for flag in ["", "-h", "--help"]:
22+
out = getoutput(f"{prg} {flag}")
23+
assert out.lower().startswith("usage")
2424

2525

2626
# --------------------------------------------------
2727
def test_one():
2828
"""one item"""
2929

30-
out = getoutput(f'{prg} chips')
31-
assert out.strip() == 'You are bringing chips.'
30+
out = getoutput(f"{prg} chips")
31+
assert out.strip() == "You are bringing chips."
3232

3333

3434
# --------------------------------------------------
3535
def test_two():
3636
"""two items"""
3737

3838
out = getoutput(f'{prg} soda "french fries"')
39-
assert out.strip() == 'You are bringing soda and french fries.'
39+
assert out.strip() == "You are bringing soda and french fries."
4040

4141

4242
# --------------------------------------------------
4343
def test_more_than_two():
4444
"""more than two items"""
4545

4646
arg = '"potato chips" coleslaw cupcakes "French silk pie"'
47-
out = getoutput(f'{prg} {arg}')
48-
expected = ('You are bringing potato chips, coleslaw, '
49-
'cupcakes, and French silk pie.')
47+
out = getoutput(f"{prg} {arg}")
48+
expected = (
49+
"You are bringing potato chips, coleslaw, " "cupcakes, and French silk pie."
50+
)
5051
assert out.strip() == expected
5152

5253

5354
# --------------------------------------------------
5455
def test_two_sorted():
5556
"""two items sorted output"""
5657

57-
out = getoutput(f'{prg} -s soda candy')
58-
assert out.strip() == 'You are bringing candy and soda.'
58+
out = getoutput(f"{prg} -s soda candy")
59+
assert out.strip() == "You are bringing candy and soda."
5960

6061

6162
# --------------------------------------------------
6263
def test_more_than_two_sorted():
6364
"""more than two items sorted output"""
6465

65-
arg = 'bananas apples dates cherries'
66-
out = getoutput(f'{prg} {arg} --sorted')
67-
expected = ('You are bringing apples, bananas, cherries, and dates.')
66+
arg = "bananas apples dates cherries"
67+
out = getoutput(f"{prg} {arg} --sorted")
68+
expected = "You are bringing apples, bananas, cherries, and dates."
6869
assert out.strip() == expected

0 commit comments

Comments
 (0)