Skip to content

Commit

Permalink
Updated get_books (#122)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TaperChipmunk32 authored Sep 27, 2024
1 parent 6d695f9 commit 7a2123f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
49 changes: 32 additions & 17 deletions machine/scripture/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,46 @@

def get_books(books: Union[str, List[str]]) -> Set[int]:
if isinstance(books, str):
books = books.split(",")
books = re.split(",|;", books)

book_set: Set[int] = set()
for book_id in books:
book_id = book_id.strip().strip("*").upper()
subtraction = False
if book_id.startswith("-"):
subtraction = True
book_id = book_id[1:]
if book_id == "NT":
book_set.update(range(40, 67))
book_set = _update_selection(book_set, set(range(40, 67)), subtraction)
elif book_id == "OT":
book_set.update(range(1, 40))
elif book_id.startswith("-"):
# remove the book from the set
book_id = book_id[1:]
book_num = book_id_to_number(book_id)
if book_num == 0:
raise RuntimeError(f"{book_id} is an invalid book ID.")
elif book_num not in book_set:
raise RuntimeError(
f"{book_id}:{book_num} cannot be removed as it is not in the existing book set of {book_set}"
)
else:
book_set.remove(book_num)
book_set = _update_selection(book_set, set(range(1, 40)), subtraction)
elif "-" in book_id:
ends = book_id.split("-")
if len(ends) != 2 or book_id_to_number(ends[0]) == 0 or book_id_to_number(ends[1]) == 0:
raise ValueError(f"{book_id} is an invalid book range.")
if book_id_to_number(ends[0]) >= book_id_to_number(ends[1]):
raise ValueError(f"{book_id} is an invalid book range. {ends[1]} precedes {ends[0]}.")
book_set = _update_selection(
book_set, set(range(book_id_to_number(ends[0]), book_id_to_number(ends[1]) + 1)), subtraction
)
else:
book_num = book_id_to_number(book_id)
if book_num == 0:
raise RuntimeError(f"{book_id} is an invalid book ID.")
book_set.add(book_num)
raise ValueError(f"{book_id} is an invalid book ID.")
book_set = _update_selection(book_set, {book_num}, subtraction)
return book_set


def _update_selection(book_set: Set[int], book_nums: Set[int], subtraction: bool) -> Set[int]:
if subtraction:
if book_nums.issubset(book_set):
book_set.difference_update(book_nums)
else:
book_ids = {book_number_to_id(book_num) for book_num in book_nums}
raise ValueError(f"{book_ids} cannot be removed as it is not in the existing book selection.")
else:
book_set.update(book_nums)

return book_set


Expand Down
24 changes: 20 additions & 4 deletions tests/scripture/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,39 @@ def test_get_books() -> None:
whole_bible.remove(2) # EXO
whole_bible.remove(41) # MRK
assert get_books("NT,OT,-MRK,-EXO") == whole_bible
assert get_books("MAT-JHN") == {i for i in range(40, 44)}
assert get_books("MAT-REV") == {i for i in range(40, 67)}
assert get_books("MAT-JHN;ACT") == {i for i in range(40, 45)}
assert get_books("MAT-JHN;ACT;-JHN-ACT,REV") == {40, 41, 42, 66}

with raises(RuntimeError):
with raises(ValueError):
# invalid name
get_books("HELLO_WORLD")

with raises(RuntimeError):
with raises(ValueError):
# subtracting book from nothing
get_books("-MRK")

with raises(RuntimeError):
with raises(ValueError):
# invalid subtracting name
get_books("NT,OT,-HELLO_WORLD")

with raises(RuntimeError):
with raises(ValueError):
# subtracting book from wrong set
get_books("OT,-MRK,NT")

with raises(ValueError):
# invalid range book
get_books("MAT-ABC")

with raises(ValueError):
# subtract invalid range book
get_books("NT;-ABC-LUK")

with raises(ValueError):
# invalid range order
get_books("MAT-GEN")


def test_get_chapters() -> None:
assert get_chapters([]) == {}
Expand Down

0 comments on commit 7a2123f

Please sign in to comment.