Skip to content

Commit d41a55f

Browse files
committed
working
1 parent f22e6ff commit d41a55f

File tree

12 files changed

+83
-206
lines changed

12 files changed

+83
-206
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ __pycache__
88
tex2pdf*
99
.mypy_cache
1010
.log
11+
.coverage

abuse/solution.py

Lines changed: 0 additions & 77 deletions
This file was deleted.

abuse/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_bad_number():
3333
n = random.choice(range(-10, 0))
3434
rv, out = getstatusoutput('{} -n {}'.format(prg, n))
3535
assert rv != 0
36-
assert re.search('--number "{}" cannot be less than 1'.format(n), out)
36+
assert re.search('--number "{}" must be > 1'.format(n), out)
3737

3838

3939
# --------------------------------------------------

bacronym/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
.PHONY: test
1+
.PHONY: test clean cover
2+
3+
clean:
4+
rm -rf __pycache__ coverage
5+
6+
cover:
7+
pytest --cov=bacronym --cov-report html:coverage test_bacronym.py
28

39
test:
410
pytest -xv test.py bacronym.py

bacronym/solution.py

Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
"""Explain acronyms"""
33

44
import argparse
5-
import io
6-
import sys
7-
import os
85
import random
96
import re
107
from collections import defaultdict
@@ -16,46 +13,46 @@ def get_args():
1613
"""get arguments"""
1714

1815
parser = argparse.ArgumentParser(
19-
description='Explain acronyms',
16+
description="Explain acronyms",
2017
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
2118

22-
parser.add_argument('acronym', help='Acronym', type=str, metavar='STR')
19+
parser.add_argument("acronym", help="Acronym", type=str, metavar="STR")
2320

24-
parser.add_argument('-n',
25-
'--num',
26-
help='Maximum number of definitions',
21+
parser.add_argument("-n",
22+
"--num",
23+
help="Maximum number of definitions",
2724
type=int,
28-
metavar='NUM',
25+
metavar="NUM",
2926
default=5)
3027

31-
parser.add_argument('-w',
32-
'--wordlist',
33-
help='Dictionary/word file',
34-
type=argparse.FileType('r'),
35-
metavar='STR',
36-
default='/usr/share/dict/words')
28+
parser.add_argument("-w",
29+
"--wordlist",
30+
help="Dictionary/word file",
31+
type=argparse.FileType("r"),
32+
metavar="STR",
33+
default="/usr/share/dict/words")
3734

38-
parser.add_argument('-x',
39-
'--exclude',
40-
help='List of words to exclude',
35+
parser.add_argument("-x",
36+
"--exclude",
37+
help="List of words to exclude",
4138
type=str,
42-
metavar='STR',
43-
nargs='+',
44-
default='a an the'.split())
39+
metavar="STR",
40+
nargs="+",
41+
default="a an the".split())
4542

46-
parser.add_argument('-s',
47-
'--seed',
48-
help='Random seed',
43+
parser.add_argument("-s",
44+
"--seed",
45+
help="Random seed",
4946
type=int,
50-
metavar='INT',
47+
metavar="INT",
5148
default=None)
5249

5350
args = parser.parse_args()
5451

5552
if args.num < 1:
5653
parser.error('--num "{}" must be > 0'.format(args.num))
5754

58-
if not re.search(r'^[A-Z]{2,}$', args.acronym.upper()):
55+
if not re.search(r"^[A-Z]{2,}$", args.acronym.upper()):
5956
msg = 'Acronym "{}" must be >1 in length, only use letters'.format(
6057
args.acronym)
6158
parser.error(msg)
@@ -64,13 +61,14 @@ def get_args():
6461

6562

6663
# --------------------------------------------------
67-
def group_words(file, stop_words=set()):
64+
def group_words(file, stop_words=()):
6865
"""Groups words in file by first letter"""
6966

7067
good = partial(re.search, r'^[a-z]{2,}$')
7168
seen = set()
7269
words_by_letter = defaultdict(list)
7370
clean = lambda word: re.sub('[^a-z]', '', word)
71+
7472
for word in filter(good, map(clean, file.read().lower().split())):
7573
if word not in seen and word not in stop_words:
7674
seen.add(word)
@@ -79,20 +77,6 @@ def group_words(file, stop_words=set()):
7977
return words_by_letter
8078

8179

82-
# --------------------------------------------------
83-
def test_group_words():
84-
"""Test group_words()"""
85-
86-
words = io.StringIO('apple, "BANANA," The Coconut! Berry - APPLE; A cabbage.')
87-
stop = 'a an the'.split()
88-
words_by_letter = group_words(words, stop)
89-
90-
assert words_by_letter['a'] == ['apple']
91-
assert words_by_letter['b'] == ['banana', 'berry']
92-
assert words_by_letter['c'] == ['coconut', 'cabbage']
93-
assert 't' not in words_by_letter
94-
95-
9680
# --------------------------------------------------
9781
def make_definitions(acronym, words_by_letter, limit=1):
9882
"""Find definitions an acronym given groupings of words by letters"""
@@ -102,33 +86,12 @@ def make_definitions(acronym, words_by_letter, limit=1):
10286
definition = []
10387
for letter in acronym.lower():
10488
opts = words_by_letter.get(letter.lower(), [])
105-
definition.append(random.choice(opts).title() if opts else '?')
106-
definitions.append(' '.join(definition))
89+
definition.append(random.choice(opts).title() if opts else "?")
90+
definitions.append(" ".join(definition))
10791

10892
return definitions
10993

11094

111-
# --------------------------------------------------
112-
def test_make_definitions():
113-
"""Test make_definitions()"""
114-
115-
words = {
116-
'a': ['apple'],
117-
'b': ['banana', 'berry'],
118-
'c': ['coconut', 'cabbage']
119-
}
120-
121-
random.seed(1)
122-
assert make_definitions('ABC', words) == ['Apple Banana Cabbage']
123-
random.seed(2)
124-
assert make_definitions('ABC', words) == ['Apple Banana Coconut']
125-
random.seed(3)
126-
assert make_definitions('AAA', words) == ['Apple Apple Apple']
127-
random.seed(4)
128-
assert make_definitions('YYZ', words) == ['? ? ?']
129-
random.seed(None)
130-
131-
13295
# --------------------------------------------------
13396
def main():
13497
"""Make a jazz noise here"""
@@ -142,13 +105,13 @@ def main():
142105
definitions = make_definitions(acronym, words_by_letter, args.num)
143106

144107
if definitions:
145-
print(acronym.upper() + ' =')
108+
print(acronym.upper() + " =")
146109
for definition in definitions:
147-
print(' - ' + definition)
110+
print(" - " + definition)
148111
else:
149-
print('Sorry I could not find any good definitions')
112+
print("Sorry I could not find any good definitions")
150113

151114

152115
# --------------------------------------------------
153-
if __name__ == '__main__':
116+
if __name__ == "__main__":
154117
main()

bacronym/test_bacronym.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import io, random
2+
from bacronym import group_words, make_definitions
3+
4+
5+
# --------------------------------------------------
6+
def test_group_words():
7+
"""Test group_words()"""
8+
9+
text = 'apple, "BANANA," The Coconut! Berry - APPLE; A cabbage.'
10+
words = io.StringIO(text)
11+
stop = 'a an the'.split()
12+
words_by_letter = group_words(words, stop)
13+
14+
assert words_by_letter['a'] == ['apple']
15+
assert words_by_letter['b'] == ['banana', 'berry']
16+
assert words_by_letter['c'] == ['coconut', 'cabbage']
17+
assert 't' not in words_by_letter
18+
19+
20+
# --------------------------------------------------
21+
def test_make_definitions():
22+
"""Test make_definitions()"""
23+
24+
words = {
25+
'a': ['apple'],
26+
'b': ['banana', 'berry'],
27+
'c': ['coconut', 'cabbage']
28+
}
29+
30+
random.seed(1)
31+
assert make_definitions('ABC', words) == ['Apple Banana Cabbage']
32+
random.seed(2)
33+
assert make_definitions('ABC', words) == ['Apple Banana Coconut']
34+
random.seed(3)
35+
assert make_definitions('AAA', words) == ['Apple Apple Apple']
36+
random.seed(4)
37+
assert make_definitions('YYZ', words) == ['? ? ?']
38+
random.seed(None)

bottles_of_beer/solution.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

gashlycrumb/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def test_bad_file():
7676
expected = "No such file or directory: '{}'".format(bad_file)
7777
assert re.search(expected, out)
7878

79+
7980
# --------------------------------------------------
8081
def random_string():
8182
"""generate a random string"""

inputs/spiders.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Dont worry, spiders,
1+
Don't worry, spiders,
22
I keep house
33
casually.

piggy/solution.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def main():
6363
# --------------------------------------------------
6464
def pig(word):
6565
"""Create Pig Latin version of a word"""
66+
6667
if re.match(r"^[\w']+$", word):
6768
vowels = 'aeiouAEIOU'
6869
consonants = re.sub('[' + vowels + ']', '', string.ascii_letters)

0 commit comments

Comments
 (0)