Skip to content

Commit ea57e69

Browse files
committed
Problem 07 solution
1 parent 90300a3 commit ea57e69

File tree

2 files changed

+74
-20
lines changed

2 files changed

+74
-20
lines changed

07_gashlycrumb/gashlycrumb.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-06-21
5+
Purpose: Prints the line from a file starting with a given letter.
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description="Gashlycrumb",
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
18+
)
19+
20+
parser.add_argument("letter", metavar="letter", nargs="+", help="Letter(s)")
21+
22+
parser.add_argument(
23+
"-f",
24+
"--file",
25+
help="Input file",
26+
metavar="FILE",
27+
type=argparse.FileType("rt"),
28+
default="gashlycrumb.txt",
29+
)
30+
return parser.parse_args()
31+
32+
33+
# --------------------------------------------------
34+
def main():
35+
"""Make a jazz noise here"""
36+
37+
args = get_args()
38+
39+
my_dict = {}
40+
for i, line in enumerate(args.file):
41+
my_dict[line[0].lower()] = line.strip()
42+
43+
for l in args.letter:
44+
text = my_dict.get(l.lower())
45+
if text:
46+
print(my_dict[l.lower()])
47+
else:
48+
print(f'I do not know "{l}".')
49+
50+
51+
# --------------------------------------------------
52+
if __name__ == "__main__":
53+
main()

07_gashlycrumb/test.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import string
88
from subprocess import getstatusoutput
99

10-
prg = './gashlycrumb.py'
10+
prg = "./gashlycrumb.py"
1111

1212

1313
# --------------------------------------------------
1414
def file_flag():
1515
"""Either -f or --file"""
1616

17-
return '-f' if random.randint(0, 1) else '--file'
17+
return "-f" if random.randint(0, 1) else "--file"
1818

1919

2020
# --------------------------------------------------
@@ -28,8 +28,8 @@ def test_exists():
2828
def test_usage():
2929
"""usage"""
3030

31-
for flag in ['-h', '--help']:
32-
rv, out = getstatusoutput(f'{prg} {flag}')
31+
for flag in ["-h", "--help"]:
32+
rv, out = getstatusoutput(f"python3 {prg} {flag}")
3333
assert rv == 0
3434
assert re.match("usage", out, re.IGNORECASE)
3535

@@ -40,7 +40,7 @@ def test_bad_file():
4040

4141
bad = random_string()
4242
letter = random.choice(string.ascii_lowercase)
43-
rv, out = getstatusoutput(f'{prg} {letter} -f {bad}')
43+
rv, out = getstatusoutput(f"python3 {prg} {letter} -f {bad}")
4444
assert rv != 0
4545
expected = f"No such file or directory: '{bad}'"
4646
assert re.search(expected, out)
@@ -50,52 +50,53 @@ def test_bad_file():
5050
def test_a():
5151
"""Test for 'a'"""
5252

53-
rv, out = getstatusoutput(f'{prg} a')
53+
rv, out = getstatusoutput(f"python3 {prg} a")
5454
assert rv == 0
55-
expected = 'A is for Amy who fell down the stairs.'
55+
expected = "A is for Amy who fell down the stairs."
5656
assert out.strip() == expected
5757

5858

5959
# --------------------------------------------------
6060
def test_b_c():
6161
"""Test for 'b c'"""
6262

63-
rv, out = getstatusoutput(f'{prg} b c')
63+
rv, out = getstatusoutput(f"python3 {prg} b c")
6464
assert rv == 0
65-
expected = ('B is for Basil assaulted by bears.\n'
66-
'C is for Clara who wasted away.')
65+
expected = "B is for Basil assaulted by bears.\n" "C is for Clara who wasted away."
6766
assert out.strip() == expected
6867

6968

7069
# --------------------------------------------------
7170
def test_y():
7271
"""Test for 'y'"""
7372

74-
rv, out = getstatusoutput(f'{prg} Y')
73+
rv, out = getstatusoutput(f"python3 {prg} Y")
7574
assert rv == 0
76-
expected = 'Y is for Yorick whose head was bashed in.'
75+
expected = "Y is for Yorick whose head was bashed in."
7776
assert out.strip() == expected
7877

7978

8079
# --------------------------------------------------
8180
def test_o_alternate():
82-
""" Test for 'o' from 'alternate.txt' """
81+
"""Test for 'o' from 'alternate.txt'"""
8382

84-
rv, out = getstatusoutput(f'{prg} o P q -f alternate.txt')
83+
rv, out = getstatusoutput(f"python3 {prg} o P q -f alternate.txt")
8584
assert rv == 0
86-
expected = ('O is for Orville, who fell in a canyon.\n'
87-
'P is for Paul, strangled by his banyan.\n'
88-
'Q is for Quintanna, flayed in the night.')
85+
expected = (
86+
"O is for Orville, who fell in a canyon.\n"
87+
"P is for Paul, strangled by his banyan.\n"
88+
"Q is for Quintanna, flayed in the night."
89+
)
8990
assert out.strip() == expected
9091

9192

9293
# --------------------------------------------------
9394
def test_bad_letter():
9495
"""Test for bad input"""
9596

96-
rv, out = getstatusoutput(f'{prg} 5 CH')
97+
rv, out = getstatusoutput(f"python3 {prg} 5 CH")
9798
assert rv == 0
98-
expected = ('I do not know "5".\n' 'I do not know "CH".')
99+
expected = 'I do not know "5".\n' 'I do not know "CH".'
99100
assert out.strip() == expected
100101

101102

@@ -104,4 +105,4 @@ def random_string():
104105
"""generate a random string"""
105106

106107
k = random.randint(5, 10)
107-
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))
108+
return "".join(random.choices(string.ascii_letters + string.digits, k=k))

0 commit comments

Comments
 (0)