avendesora / pythonbible

A python library for validating, parsing, normalizing scripture references and retrieving scripture texts (for open source and public domain versions)
https://docs.python.bible
MIT License
56 stars 11 forks source link

Feature idea: Add flag to return formatted references as abbreviated #170

Open christopherpickering opened 2 months ago

christopherpickering commented 2 months ago

Or, add an optional dict input of a "short title" dictionary. It would be handy when trying to get the output references into a specific format.

I gave it a quick try, but had a hashable dict error:

import pythonbible as bible
from pythonbible.books import Book
text ='Jeremiah 10:11-12;'

titles = {
    Book.JEREMIAH: "Jer", # instead of Jeremiah
}

references = bible.get_references(text)
formatted = bible.format_scripture_references(references, short_titles=titles)

print(formatted)

I modified two functions. They way you pass through kwargs is pretty nice! I haven't used that before.

# fomatter.py:
def _get_book_title(book: Book, include_books: bool = True, **kwargs: Any) -> str:
    if not include_books:
        return ""

    version: Version = kwargs.get("version", DEFAULT_VERSION)
    full_title: bool = kwargs.get("full_title", False)
    version_book_titles: BookTitles = get_book_titles(book, version, **kwargs)

    return (
        version_book_titles.long_title
        if full_title
        else version_book_titles.short_title
    )

# formatter.py:
@lru_cache()
def get_book_titles(book: Book, version: Version, **kwargs: Any) -> BookTitles:
    """Return the book titles for the given Book and optional Version.

    :param book: a book of the Bible
    :type book: Book
    :param version: a version of the Bible, defaults to American Standard
    :type version: Version
    :return: the long and short titles of the given book and version
    :rtype: BookTitles
    :raises MissingBookFileError: if the book file for the given book and version does
                                  not exist
    """
    short_titles, long_titles = _get_version_book_titles(version or DEFAULT_VERSION)

    if short_titles in kwargs:
        short_titles = kwargs.short_titles

    if long_titles in kwargs:
        long_titles = kwargs.long_titles

    short_title = short_titles.get(book, book.title)
    long_title = long_titles.get(book, book.title)

    return BookTitles(long_title, short_title)

but get this error:

    version_book_titles: BookTitles = get_book_titles(book, version, **kwargs)
TypeError: unhashable type: 'list'

if you've handled this errror before, I'd appreciate a tip :) Otherwise I'll probably give it a shot again sometime.

Thanks!

avendesora commented 1 month ago

I'll look into this further when I've got a bit more time, but I did want to thank you for suggesting this and let you know that there are some changes/improvements happening around book titles in some work that is still in development. I'll see if I can include something to allow for abbreviated titles as part of that work. If not, I think it is worth having and can create a separate branch for that work after the current work in progress is finished.