Skip to content

Commit 88d29dd

Browse files
committed
Problem 03 Solution
1 parent c7807f5 commit 88d29dd

File tree

2 files changed

+72
-17
lines changed

2 files changed

+72
-17
lines changed

03_picnic/picnic.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-06-17
5+
Purpose: List of items to bring on the 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="Get items to bring on the picnic",
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
18+
)
19+
20+
parser.add_argument(
21+
"items",
22+
nargs="+",
23+
metavar="item",
24+
help="Item(s) to bring",
25+
)
26+
27+
parser.add_argument(
28+
"-s",
29+
"--sorted",
30+
help="Sort the items",
31+
action="store_true",
32+
)
33+
34+
return parser.parse_args()
35+
36+
37+
# --------------------------------------------------
38+
def main():
39+
"""Make a jazz noise here"""
40+
41+
args = get_args()
42+
items = sorted(args.items) if args.sorted else args.items
43+
supplies = ""
44+
match len(items):
45+
case 1:
46+
supplies = items[0]
47+
case 2:
48+
supplies = " and ".join(items)
49+
case _:
50+
supplies = ", ".join(items[:-1]) + ", and " + items[-1]
51+
print(f"You are bringing {supplies}.")
52+
53+
54+
# --------------------------------------------------
55+
if __name__ == "__main__":
56+
main()

03_picnic/test.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,64 @@
44
import os
55
from subprocess import getoutput
66

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

99

1010
# --------------------------------------------------
1111
def test_exists():
1212
"""exists"""
1313

14-
assert os.path.isfile(prg)
14+
assert os.path.isfile(PRG)
1515

1616

1717
# --------------------------------------------------
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"python3 {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"python3 {PRG} chips")
31+
assert out.strip() == "You are bringing chips."
3232

3333

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

38-
out = getoutput(f'{prg} soda "french fries"')
39-
assert out.strip() == 'You are bringing soda and french fries.'
38+
out = getoutput(f'python3 {PRG} soda "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"python3 {PRG} {arg}")
48+
expected = "You are bringing potato chips, coleslaw, cupcakes, and French silk pie."
5049
assert out.strip() == expected
5150

5251

5352
# --------------------------------------------------
5453
def test_two_sorted():
5554
"""two items sorted output"""
5655

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

6059

6160
# --------------------------------------------------
6261
def test_more_than_two_sorted():
6362
"""more than two items sorted output"""
6463

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

0 commit comments

Comments
 (0)