Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated get_books #122

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading