Skip to content

Commit d0bb5b0

Browse files
committed
Problem 05 solution
1 parent b7669f5 commit d0bb5b0

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

05_howler/howler.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-06-18
5+
Purpose: Howler (upper-cases input)
6+
"""
7+
8+
import argparse
9+
import sys
10+
import os
11+
12+
13+
# --------------------------------------------------
14+
def get_args():
15+
"""Get command-line arguments"""
16+
17+
parser = argparse.ArgumentParser(
18+
description="Howler (upper-cases input)",
19+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
20+
)
21+
22+
parser.add_argument("text", metavar="text", help="Input string or file")
23+
parser.add_argument(
24+
"-o", "--outfile", help="Output filename", metavar="str", type=str, default=""
25+
)
26+
27+
args = parser.parse_args()
28+
if os.path.isfile(args.text):
29+
with open(args.text, "rt", encoding="utf-8") as fh:
30+
args.text = fh.read().rstrip()
31+
32+
return args
33+
34+
35+
# --------------------------------------------------
36+
def main():
37+
"""Make a jazz noise here"""
38+
39+
args = get_args()
40+
text = args.text
41+
outfile = args.outfile
42+
with open(outfile, "wt", encoding="utf-8") if outfile else sys.stdout as fh:
43+
fh.write(text.upper() + "\n")
44+
45+
46+
# --------------------------------------------------
47+
if __name__ == "__main__":
48+
main()

05_howler/test.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@
77
import string
88
from subprocess import getstatusoutput, getoutput
99

10-
prg = './howler.py'
10+
PRG = "./howler.py"
1111

1212

1313
# --------------------------------------------------
1414
def random_string():
1515
"""generate a random string"""
1616

1717
k = random.randint(5, 10)
18-
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))
18+
return "".join(random.choices(string.ascii_letters + string.digits, k=k))
1919

2020

2121
# --------------------------------------------------
2222
def out_flag():
2323
"""Either -o or --outfile"""
2424

25-
return '-o' if random.randint(0, 1) else '--outfile'
25+
return "-o" if random.randint(0, 1) else "--outfile"
2626

2727

2828
# --------------------------------------------------
2929
def test_exists():
3030
"""exists"""
3131

32-
assert os.path.isfile(prg)
32+
assert os.path.isfile(PRG)
3333

3434

3535
# --------------------------------------------------
3636
def test_usage():
3737
"""usage"""
3838

39-
for flag in ['-h', '--help']:
40-
rv, out = getstatusoutput(f'{prg} {flag}')
39+
for flag in ["-h", "--help"]:
40+
rv, out = getstatusoutput(f"python3 {PRG} {flag}")
4141
assert rv == 0
4242
assert re.match("usage", out, re.IGNORECASE)
4343

@@ -46,8 +46,8 @@ def test_usage():
4646
def test_text_stdout():
4747
"""Test STDIN/STDOUT"""
4848

49-
out = getoutput(f'{prg} "foo bar baz"')
50-
assert out.strip() == 'FOO BAR BAZ'
49+
out = getoutput(f'python3 {PRG} "foo bar baz"')
50+
assert out.strip() == "FOO BAR BAZ"
5151

5252

5353
# --------------------------------------------------
@@ -59,11 +59,11 @@ def test_text_outfile():
5959
os.remove(out_file)
6060

6161
try:
62-
out = getoutput(f'{prg} {out_flag()} {out_file} "foo bar baz"')
63-
assert out.strip() == ''
62+
out = getoutput(f'python3 {PRG} {out_flag()} {out_file} "foo bar baz"')
63+
assert out.strip() == ""
6464
assert os.path.isfile(out_file)
6565
text = open(out_file).read().rstrip()
66-
assert text == 'FOO BAR BAZ'
66+
assert text == "FOO BAR BAZ"
6767
finally:
6868
if os.path.isfile(out_file):
6969
os.remove(out_file)
@@ -73,19 +73,18 @@ def test_text_outfile():
7373
def test_file():
7474
"""Test file in/out"""
7575

76-
for expected_file in os.listdir('test-outs'):
76+
for expected_file in os.listdir("test-outs"):
7777
try:
7878
out_file = random_string()
7979
if os.path.isfile(out_file):
8080
os.remove(out_file)
8181

8282
basename = os.path.basename(expected_file)
83-
in_file = os.path.join('../inputs', basename)
84-
out = getoutput(f'{prg} {out_flag()} {out_file} {in_file}')
85-
assert out.strip() == ''
83+
in_file = os.path.join("../inputs", basename)
84+
out = getoutput(f"python3 {PRG} {out_flag()} {out_file} {in_file}")
85+
assert out.strip() == ""
8686
produced = open(out_file).read().rstrip()
87-
expected = open(os.path.join('test-outs',
88-
expected_file)).read().strip()
87+
expected = open(os.path.join("test-outs", expected_file)).read().strip()
8988
assert expected == produced
9089
finally:
9190
if os.path.isfile(out_file):

0 commit comments

Comments
 (0)