Skip to content

Commit dbbd113

Browse files
committed
work
1 parent c58f6b5 commit dbbd113

File tree

6 files changed

+170
-32
lines changed

6 files changed

+170
-32
lines changed

abuse/test.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import random
66
import re
7+
import string
78
from subprocess import getstatusoutput, getoutput
89

910
prg = './abuse.py'
@@ -27,38 +28,68 @@ def test_usage():
2728

2829

2930
# --------------------------------------------------
30-
def test_bad_adjectives():
31+
def test_bad_adjective_str():
32+
"""bad_adjectives"""
33+
34+
bad = random_string()
35+
rv, out = getstatusoutput(f'{prg} -a {bad}')
36+
assert rv != 0
37+
assert re.search(f"invalid int value: '{bad}'", out)
38+
39+
40+
# --------------------------------------------------
41+
def test_bad_adjective_num():
3142
"""bad_adjectives"""
3243

3344
n = random.choice(range(-10, 0))
34-
rv, out = getstatusoutput('{} -a {}'.format(prg, n))
45+
rv, out = getstatusoutput(f'{prg} -a {n}')
3546
assert rv != 0
36-
assert re.search('--adjectives "{}" must be > 1'.format(n), out)
47+
assert re.search(f'--adjectives "{n}" must be > 0', out)
3748

3849

3950
# --------------------------------------------------
40-
def test_bad_number():
51+
def test_bad_number_str():
52+
"""bad_number"""
53+
54+
bad = random_string()
55+
rv, out = getstatusoutput(f'{prg} -n {bad}')
56+
assert rv != 0
57+
assert re.search(f"invalid int value: '{bad}'", out)
58+
59+
60+
# --------------------------------------------------
61+
def test_bad_number_int():
4162
"""bad_number"""
4263

4364
n = random.choice(range(-10, 0))
44-
rv, out = getstatusoutput('{} -n {}'.format(prg, n))
65+
rv, out = getstatusoutput(f'{prg} -n {n}')
66+
assert rv != 0
67+
assert re.search(f'--number "{n}" must be > 0', out)
68+
69+
70+
# --------------------------------------------------
71+
def test_bad_seed():
72+
"""bad seed"""
73+
74+
bad = random_string()
75+
rv, out = getstatusoutput(f'{prg} -s {bad}')
4576
assert rv != 0
46-
assert re.search('--number "{}" must be > 1'.format(n), out)
77+
assert re.search(f"invalid int value: '{bad}'", out)
4778

4879

4980
# --------------------------------------------------
5081
def test_01():
5182
"""test"""
5283

53-
out = getoutput('{} -s 1 -n 1'.format(prg))
84+
out = getoutput(f'{prg} -s 1 -n 1')
5485
assert out.strip() == 'You filthsome, cullionly fiend!'
5586

5687

5788
# --------------------------------------------------
5889
def test_02():
5990
"""test"""
6091

61-
out = getoutput('{} --seed 2'.format(prg))
92+
out = getoutput(f'{prg} --seed 2')
6293
expected = """
6394
You corrupt, detestable beggar!
6495
You peevish, foolish gull!
@@ -71,7 +102,7 @@ def test_02():
71102
def test_03():
72103
"""test"""
73104

74-
out = getoutput('{} -s 3 -n 5 -a 1'.format(prg))
105+
out = getoutput(f'{prg} -s 3 -n 5 -a 1')
75106
expected = """
76107
You infected villain!
77108
You vile braggart!
@@ -86,9 +117,16 @@ def test_03():
86117
def test_04():
87118
"""test"""
88119

89-
out = getoutput('{} --seed 4 --number 2 --adjectives 4'.format(prg))
120+
out = getoutput(f'{prg} --seed 4 --number 2 --adjectives 4')
90121
expected = """
91122
You infected, lecherous, dishonest, rotten recreant!
92123
You filthy, detestable, cullionly, base lunatic!
93124
""".strip()
94125
assert out.strip() == expected
126+
127+
128+
# --------------------------------------------------
129+
def random_string():
130+
"""generate a random filename"""
131+
132+
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=5))

hello/two_args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ def get_args():
1313
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
1414

1515
parser.add_argument('color',
16-
metavar='COLOR',
16+
metavar='str',
1717
type=str,
1818
help='Color')
1919

2020
parser.add_argument('size',
21-
metavar='SIZE',
21+
metavar='int',
2222
type=int,
2323
help='Size')
2424

inputs/now.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Now is the time for all good men to come to the aid of the party
1+
Now is the time for all good men to come to the aid of the party.
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def get_args():
2020
parser.add_argument('-s',
2121
'--seed',
2222
help='Random seed',
23-
metavar='str',
24-
type=str,
23+
metavar='int',
24+
type=int,
2525
default=None)
2626

2727
parser.add_argument('-m',
@@ -33,9 +33,8 @@ def get_args():
3333

3434
args = parser.parse_args()
3535

36-
if not 0 < args.mutations <= 1:
37-
msg = '--mutations "{}" must be b/w 0 and 1'.format(args.mutations)
38-
parser.error(msg)
36+
if not 0 <= args.mutations <= 1:
37+
parser.error(f'--mutations "{args.mutations}" must be b/w 0 and 1')
3938

4039
if os.path.isfile(args.text):
4140
args.text = open(args.text).read().rstrip()
@@ -50,14 +49,13 @@ def main():
5049
args = get_args()
5150
text = args.text
5251
random.seed(args.seed)
53-
len_text = len(text)
54-
num_mutations = int(args.mutations * len_text)
5552
alpha = string.ascii_letters + string.punctuation
53+
len_text = len(text)
54+
num_mutations = round(args.mutations * len_text)
5655

57-
for _ in range(num_mutations):
58-
i = random.choice(range(len_text))
59-
text = text[:i] + random.choice(alpha) + text[i + 1:]
60-
56+
for i in random.sample(range(len_text), num_mutations):
57+
new_char = random.choice(alpha.replace(text[i], ''))
58+
text = text[:i] + new_char + text[i + 1:]
6159
print(text)
6260

6361

telephone/solution2.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
"""Telephone"""
3+
4+
import argparse
5+
import os
6+
import random
7+
import string
8+
9+
10+
# --------------------------------------------------
11+
def get_args():
12+
"""Get command-line arguments"""
13+
14+
parser = argparse.ArgumentParser(
15+
description='Telephone',
16+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
17+
18+
parser.add_argument('text', metavar='str', help='Input text or file')
19+
20+
parser.add_argument('-s',
21+
'--seed',
22+
help='Random seed',
23+
metavar='int',
24+
type=int,
25+
default=None)
26+
27+
parser.add_argument('-m',
28+
'--mutations',
29+
help='Percent mutations',
30+
metavar='float',
31+
type=float,
32+
default=0.1)
33+
34+
args = parser.parse_args()
35+
36+
if not 0 <= args.mutations <= 1:
37+
parser.error(f'--mutations "{args.mutations}" must be b/w 0 and 1')
38+
39+
if os.path.isfile(args.text):
40+
args.text = open(args.text).read().rstrip()
41+
42+
return args
43+
44+
45+
# --------------------------------------------------
46+
def main():
47+
"""Make a jazz noise here"""
48+
49+
args = get_args()
50+
text = list(args.text)
51+
random.seed(args.seed)
52+
alpha = string.ascii_letters + string.punctuation
53+
len_text = len(text)
54+
num_mutations = round(args.mutations * len_text)
55+
56+
for i in random.sample(range(len_text), num_mutations):
57+
text[i] = random.choice(alpha.replace(text[i], ''))
58+
print(''.join(text))
59+
60+
61+
# --------------------------------------------------
62+
if __name__ == '__main__':
63+
main()

telephone/test.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
from subprocess import getstatusoutput, getoutput
55
import os
6+
import random
67
import re
8+
import string
79

810
prg = "./telephone.py"
911
fox = '../inputs/fox.txt'
@@ -26,6 +28,26 @@ def test_usage():
2628
assert re.match('usage', out, re.IGNORECASE)
2729

2830

31+
# --------------------------------------------------
32+
def test_bad_seed_str():
33+
"""bad seed str value"""
34+
35+
bad = random_string()
36+
rv, out = getstatusoutput('{} -s {} {}'.format(prg, bad, fox))
37+
assert rv > 0
38+
assert re.search(f"invalid int value: '{bad}'", out)
39+
40+
41+
# --------------------------------------------------
42+
def test_bad_mutation_str():
43+
"""bad mutation str value"""
44+
45+
bad = random_string()
46+
rv, out = getstatusoutput('{} -m {} {}'.format(prg, bad, fox))
47+
assert rv > 0
48+
assert re.search(f"invalid float value: '{bad}'", out)
49+
50+
2951
# --------------------------------------------------
3052
def test_bad_mutation():
3153
"""bad mutation values"""
@@ -38,44 +60,61 @@ def test_bad_mutation():
3860

3961

4062
# --------------------------------------------------
41-
def test_text01():
63+
def test_for_echo():
64+
"""test"""
65+
66+
txt = open(now).read().rstrip()
67+
rv, out = getstatusoutput('{} -m 0 "{}"'.format(prg, txt))
68+
assert rv == 0
69+
assert out.rstrip() == txt
70+
71+
72+
# --------------------------------------------------
73+
def test_now_cmd_s1():
4274
"""test"""
4375

4476
txt = open(now).read().rstrip()
4577
rv, out = getstatusoutput('{} -s 1 "{}"'.format(prg, txt))
4678
assert rv == 0
4779
expected = """
48-
Now ao the time for all good-men to came to the aid of Whe pa*ty
80+
Now is B*e time X'r all good mem to come to the ,id of the party.
4981
""".strip()
5082
assert out.rstrip() == expected
5183

5284

5385
# --------------------------------------------------
54-
def test_text02():
86+
def test_now_cmd_s2_m4():
5587
"""test"""
5688

5789
txt = open(now).read().rstrip()
5890
rv, out = getstatusoutput('{} -s 2 -m .4 "{}"'.format(prg, txt))
5991
assert rv == 0
6092
expected = """
61-
NUw iVKPqe time fErXal; gPod"'en to come`Do *Dl aFd of!the Yabta
93+
Nod ie .he(JiFe ?orvalldg/osxmenUt? cxxe.t$PtheOaidWEV:the xa/ty.
6294
""".strip()
6395
assert out.rstrip() == expected
6496

6597

6698
# --------------------------------------------------
67-
def test_file01():
99+
def test_fox_file_s1():
68100
"""test"""
69101

70102
rv, out = getstatusoutput('{} --seed 1 {}'.format(prg, fox))
71103
assert rv == 0
72-
assert out.rstrip() == 'Tho quick brown foa jumps oWer*the lazy dog.'
104+
assert out.rstrip() == "The 'uicq brown *ox jumps over the l-zy dog."
73105

74106

75107
# --------------------------------------------------
76-
def test_file02():
108+
def test_fox_file_s2_m6():
77109
"""test"""
78110

79111
rv, out = getstatusoutput('{} --seed 2 --mutations .6 {}'.format(prg, fox))
80112
assert rv == 0
81-
assert out.rstrip() == 'UheK+u*ckXbrPw~ fox Du*#FT{ver f~e}|Uzy (T?l'
113+
assert out.rstrip() == "V;xvq?ic# E]'Qy x/xdjumFs.o/U? th!Ulv'yrVox."
114+
115+
116+
# --------------------------------------------------
117+
def random_string():
118+
"""generate a random filename"""
119+
120+
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=5))

0 commit comments

Comments
 (0)