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

Skip to content

Commit 61df97d

Browse files
author
Gabriel Soares Martins
committed
Finished Chapter three.
1 parent 9e51610 commit 61df97d

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

03_picnic/picnic.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : gmartins <gmartins@localhost>
4+
Date : 2024-12-18
5+
Purpose: Rock the Casbah
6+
"""
7+
8+
import argparse
9+
10+
TEMPLATE = 'You are bringing {}.'
11+
12+
13+
# --------------------------------------------------
14+
def get_args():
15+
"""Get command-line arguments"""
16+
17+
parser = argparse.ArgumentParser(
18+
description='Picnic game',
19+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
20+
21+
parser.add_argument('positional',
22+
metavar='str',
23+
nargs='+',
24+
help='Item(s) to bring')
25+
26+
parser.add_argument('-s',
27+
'--sorted',
28+
help='Sort the items',
29+
action='store_true')
30+
31+
parser.add_argument('-n',
32+
'--not-oxford',
33+
help='Do not print Oxford comma',
34+
action='store_true')
35+
36+
parser.add_argument('-r',
37+
'--sep',
38+
metavar='separator',
39+
default=',',
40+
help='Separator')
41+
42+
return parser.parse_args()
43+
44+
45+
# --------------------------------------------------
46+
def main():
47+
"""Make a jazz noise here"""
48+
49+
args = get_args()
50+
sorted_arg = args.sorted
51+
52+
pos_arg = args.positional
53+
oxford_arg = args.not_oxford
54+
sep_arg = args.sep + ' '
55+
56+
if sep_arg != ', ':
57+
oxford_arg = True
58+
59+
if sorted_arg:
60+
pos_arg.sort()
61+
62+
if len(pos_arg) < 1:
63+
print('Error')
64+
elif len(pos_arg) == 1:
65+
print(TEMPLATE.format(pos_arg[0]))
66+
else:
67+
if len(pos_arg) == 2:
68+
s = ' and '.join(pos_arg)
69+
else:
70+
pos_arg[-1] = 'and ' + pos_arg[-1]
71+
s = sep_arg.join(pos_arg)
72+
if oxford_arg:
73+
s = s.replace(sep_arg + 'and', ' and')
74+
print(TEMPLATE.format(s))
75+
76+
77+
# --------------------------------------------------
78+
if __name__ == '__main__':
79+
main()

03_picnic/test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,24 @@ def test_more_than_two_sorted():
6666
out = getoutput(f'{prg} {arg} --sorted')
6767
expected = ('You are bringing apples, bananas, cherries, and dates.')
6868
assert out.strip() == expected
69+
70+
71+
# --------------------------------------------------
72+
def test_more_than_two_not_oxford():
73+
"""more than two items that do not contain oxford comma"""
74+
75+
arg = '"potato chips" coleslaw cupcakes "French silk pie" -n'
76+
out = getoutput(f'{prg} {arg}')
77+
expected = ('You are bringing potato chips, coleslaw, '
78+
'cupcakes and French silk pie.')
79+
assert out.strip() == expected
80+
81+
82+
def test_more_than_two_sep():
83+
"""more than two items with custom separator"""
84+
85+
arg = '"potato chips" coleslaw cupcakes "French silk pie" -r ";"'
86+
out = getoutput(f'{prg} {arg}')
87+
expected = ('You are bringing potato chips; coleslaw; '
88+
'cupcakes and French silk pie.')
89+
assert out.strip() == expected

0 commit comments

Comments
 (0)