Skip to content

Commit 7a2123f

Browse files
Updated get_books (#122)
* Updated get_books get_books now accepts ";" as a delimeter and allows for book ranges, just as get_chapters does. * Updated parse.py formatting * Updated test_parse.py for get_books changes -Now tests for book ranges -Now tests for ValueErrors instead of RuntimeErrors with an incorrect input * Updated parse.py -Renamed update_selection to _update_selection since it is "private" -Simplified _update_selection
1 parent 6d695f9 commit 7a2123f

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

machine/scripture/parse.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,46 @@
1212

1313
def get_books(books: Union[str, List[str]]) -> Set[int]:
1414
if isinstance(books, str):
15-
books = books.split(",")
15+
books = re.split(",|;", books)
16+
1617
book_set: Set[int] = set()
1718
for book_id in books:
1819
book_id = book_id.strip().strip("*").upper()
20+
subtraction = False
21+
if book_id.startswith("-"):
22+
subtraction = True
23+
book_id = book_id[1:]
1924
if book_id == "NT":
20-
book_set.update(range(40, 67))
25+
book_set = _update_selection(book_set, set(range(40, 67)), subtraction)
2126
elif book_id == "OT":
22-
book_set.update(range(1, 40))
23-
elif book_id.startswith("-"):
24-
# remove the book from the set
25-
book_id = book_id[1:]
26-
book_num = book_id_to_number(book_id)
27-
if book_num == 0:
28-
raise RuntimeError(f"{book_id} is an invalid book ID.")
29-
elif book_num not in book_set:
30-
raise RuntimeError(
31-
f"{book_id}:{book_num} cannot be removed as it is not in the existing book set of {book_set}"
32-
)
33-
else:
34-
book_set.remove(book_num)
27+
book_set = _update_selection(book_set, set(range(1, 40)), subtraction)
28+
elif "-" in book_id:
29+
ends = book_id.split("-")
30+
if len(ends) != 2 or book_id_to_number(ends[0]) == 0 or book_id_to_number(ends[1]) == 0:
31+
raise ValueError(f"{book_id} is an invalid book range.")
32+
if book_id_to_number(ends[0]) >= book_id_to_number(ends[1]):
33+
raise ValueError(f"{book_id} is an invalid book range. {ends[1]} precedes {ends[0]}.")
34+
book_set = _update_selection(
35+
book_set, set(range(book_id_to_number(ends[0]), book_id_to_number(ends[1]) + 1)), subtraction
36+
)
3537
else:
3638
book_num = book_id_to_number(book_id)
3739
if book_num == 0:
38-
raise RuntimeError(f"{book_id} is an invalid book ID.")
39-
book_set.add(book_num)
40+
raise ValueError(f"{book_id} is an invalid book ID.")
41+
book_set = _update_selection(book_set, {book_num}, subtraction)
42+
return book_set
43+
44+
45+
def _update_selection(book_set: Set[int], book_nums: Set[int], subtraction: bool) -> Set[int]:
46+
if subtraction:
47+
if book_nums.issubset(book_set):
48+
book_set.difference_update(book_nums)
49+
else:
50+
book_ids = {book_number_to_id(book_num) for book_num in book_nums}
51+
raise ValueError(f"{book_ids} cannot be removed as it is not in the existing book selection.")
52+
else:
53+
book_set.update(book_nums)
54+
4055
return book_set
4156

4257

tests/scripture/test_parse.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,39 @@ def test_get_books() -> None:
1515
whole_bible.remove(2) # EXO
1616
whole_bible.remove(41) # MRK
1717
assert get_books("NT,OT,-MRK,-EXO") == whole_bible
18+
assert get_books("MAT-JHN") == {i for i in range(40, 44)}
19+
assert get_books("MAT-REV") == {i for i in range(40, 67)}
20+
assert get_books("MAT-JHN;ACT") == {i for i in range(40, 45)}
21+
assert get_books("MAT-JHN;ACT;-JHN-ACT,REV") == {40, 41, 42, 66}
1822

19-
with raises(RuntimeError):
23+
with raises(ValueError):
2024
# invalid name
2125
get_books("HELLO_WORLD")
2226

23-
with raises(RuntimeError):
27+
with raises(ValueError):
2428
# subtracting book from nothing
2529
get_books("-MRK")
2630

27-
with raises(RuntimeError):
31+
with raises(ValueError):
2832
# invalid subtracting name
2933
get_books("NT,OT,-HELLO_WORLD")
3034

31-
with raises(RuntimeError):
35+
with raises(ValueError):
3236
# subtracting book from wrong set
3337
get_books("OT,-MRK,NT")
3438

39+
with raises(ValueError):
40+
# invalid range book
41+
get_books("MAT-ABC")
42+
43+
with raises(ValueError):
44+
# subtract invalid range book
45+
get_books("NT;-ABC-LUK")
46+
47+
with raises(ValueError):
48+
# invalid range order
49+
get_books("MAT-GEN")
50+
3551

3652
def test_get_chapters() -> None:
3753
assert get_chapters([]) == {}

0 commit comments

Comments
 (0)