Skip to content

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

Description

@christopherpickering

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions