Skip to content

Commit 2997b72

Browse files
committed
Problem 09 solution
1 parent 94853b2 commit 2997b72

File tree

2 files changed

+100
-17
lines changed

2 files changed

+100
-17
lines changed

09_abuse/abuse.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-06-26
5+
Purpose: A Shakesperean insult generator
6+
"""
7+
8+
import argparse
9+
import random
10+
11+
12+
# --------------------------------------------------
13+
def positive_int(string):
14+
"""check for positive integer"""
15+
try:
16+
value = int(string)
17+
except ValueError as e:
18+
raise argparse.ArgumentTypeError(f"invalid int value: '{string}'") from e
19+
if value < 1:
20+
raise argparse.ArgumentTypeError(f'"{value}" must be > 0')
21+
return value
22+
23+
24+
# --------------------------------------------------
25+
def get_args():
26+
"""Get command-line arguments"""
27+
28+
parser = argparse.ArgumentParser(
29+
description="A Shakesperean insult generator",
30+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
31+
)
32+
33+
parser.add_argument(
34+
"-a",
35+
"--adjectives",
36+
help="Number of adjectives",
37+
metavar="int",
38+
type=positive_int,
39+
default=2,
40+
)
41+
42+
parser.add_argument(
43+
"-n",
44+
"--number",
45+
help="Number of insults",
46+
metavar="int",
47+
type=positive_int,
48+
default=3,
49+
)
50+
51+
parser.add_argument("-s", "--seed", help="Random seed", metavar="int", type=int)
52+
53+
return parser.parse_args()
54+
55+
56+
# --------------------------------------------------
57+
def main():
58+
"""Make a jazz noise here"""
59+
60+
adjectives = """
61+
bankrupt base caterwauling corrupt cullionly detestable dishonest false
62+
filthsome filthy foolish foul gross heedless indistinguishable infected
63+
insatiate irksome lascivious lecherous loathsome lubbery old peevish
64+
rascaly rotten ruinous scurilous scurvy slanderous sodden-witted
65+
thin-faced toad-spotted unmannered vile wall-eyed""".split()
66+
67+
nouns = """
68+
Judas Satan ape ass barbermonger beggar block boy braggart butt
69+
carbuncle coward coxcomb cur dandy degenerate fiend fishmonger fool
70+
gull harpy jack jolthead knave liar lunatic maw milksop minion
71+
ratcatcher recreant rogue scold slave swine traitor varlet villain worm""".split()
72+
73+
args = get_args()
74+
random.seed(args.seed)
75+
76+
for _ in range(args.number):
77+
descriptors = ", ".join(random.sample(adjectives, args.adjectives))
78+
print(f"You {descriptors} {random.choice(nouns)}!")
79+
80+
81+
# --------------------------------------------------
82+
if __name__ == "__main__":
83+
main()

09_abuse/test.py

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

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

1212

1313
# --------------------------------------------------
1414
def test_exists():
1515
"""exists"""
1616

17-
assert os.path.isfile(prg)
17+
assert os.path.isfile(PRG)
1818

1919

2020
# --------------------------------------------------
2121
def test_usage():
2222
"""usage"""
2323

24-
for flag in ['-h', '--help']:
25-
rv, out = getstatusoutput(f'{prg} {flag}')
24+
for flag in ["-h", "--help"]:
25+
rv, out = getstatusoutput(f"python3 {PRG} {flag}")
2626
assert rv == 0
2727
assert re.match("usage", out, re.IGNORECASE)
2828

@@ -32,7 +32,7 @@ def test_bad_adjective_str():
3232
"""bad_adjectives"""
3333

3434
bad = random_string()
35-
rv, out = getstatusoutput(f'{prg} -a {bad}')
35+
rv, out = getstatusoutput(f"python3 {PRG} -a {bad}")
3636
assert rv != 0
3737
assert re.search(f"invalid int value: '{bad}'", out)
3838

@@ -42,18 +42,18 @@ def test_bad_adjective_num():
4242
"""bad_adjectives"""
4343

4444
n = random.choice(range(-10, 0))
45-
rv, out = getstatusoutput(f'{prg} -a {n}')
45+
rv, out = getstatusoutput(f"python3 {PRG} -a {n}")
4646
print(out)
4747
assert rv != 0
48-
assert re.search(f'--adjectives "{n}" must be > 0', out)
48+
assert re.search(f'--adjectives: "{n}" must be > 0', out)
4949

5050

5151
# --------------------------------------------------
5252
def test_bad_number_str():
5353
"""bad_number"""
5454

5555
bad = random_string()
56-
rv, out = getstatusoutput(f'{prg} -n {bad}')
56+
rv, out = getstatusoutput(f"python3 {PRG} -n {bad}")
5757
assert rv != 0
5858
assert re.search(f"invalid int value: '{bad}'", out)
5959

@@ -63,17 +63,17 @@ def test_bad_number_int():
6363
"""bad_number"""
6464

6565
n = random.choice(range(-10, 0))
66-
rv, out = getstatusoutput(f'{prg} -n {n}')
66+
rv, out = getstatusoutput(f"python3 {PRG} -n {n}")
6767
assert rv != 0
68-
assert re.search(f'--number "{n}" must be > 0', out)
68+
assert re.search(f'--number: "{n}" must be > 0', out)
6969

7070

7171
# --------------------------------------------------
7272
def test_bad_seed():
7373
"""bad seed"""
7474

7575
bad = random_string()
76-
rv, out = getstatusoutput(f'{prg} -s {bad}')
76+
rv, out = getstatusoutput(f"python3 {PRG} -s {bad}")
7777
assert rv != 0
7878
assert re.search(f"invalid int value: '{bad}'", out)
7979

@@ -82,15 +82,15 @@ def test_bad_seed():
8282
def test_01():
8383
"""test"""
8484

85-
out = getoutput(f'{prg} -s 1 -n 1')
86-
assert out.strip() == 'You filthsome, cullionly fiend!'
85+
out = getoutput(f"python3 {PRG} -s 1 -n 1")
86+
assert out.strip() == "You filthsome, cullionly fiend!"
8787

8888

8989
# --------------------------------------------------
9090
def test_02():
9191
"""test"""
9292

93-
out = getoutput(f'{prg} --seed 2')
93+
out = getoutput(f"python3 {PRG} --seed 2")
9494
expected = """
9595
You corrupt, detestable beggar!
9696
You peevish, foolish gull!
@@ -103,7 +103,7 @@ def test_02():
103103
def test_03():
104104
"""test"""
105105

106-
out = getoutput(f'{prg} -s 3 -n 5 -a 1')
106+
out = getoutput(f"python3 {PRG} -s 3 -n 5 -a 1")
107107
expected = """
108108
You infected villain!
109109
You vile braggart!
@@ -118,7 +118,7 @@ def test_03():
118118
def test_04():
119119
"""test"""
120120

121-
out = getoutput(f'{prg} --seed 4 --number 2 --adjectives 4')
121+
out = getoutput(f"python3 {PRG} --seed 4 --number 2 --adjectives 4")
122122
expected = """
123123
You infected, lecherous, dishonest, rotten recreant!
124124
You filthy, detestable, cullionly, base lunatic!
@@ -130,4 +130,4 @@ def test_04():
130130
def random_string():
131131
"""generate a random filename"""
132132

133-
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=5))
133+
return "".join(random.choices(string.ascii_lowercase + string.digits, k=5))

0 commit comments

Comments
 (0)