From e473cb9f45e98e890bcfc4e1537c75bbf4c880be Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 24 Apr 2025 14:55:55 -0400 Subject: [PATCH] Add Bible class with verse iteration functionality --- kjv.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/kjv.py b/kjv.py index c03c369..a02a985 100644 --- a/kjv.py +++ b/kjv.py @@ -1,6 +1,39 @@ +import json from pydantic import BaseModel +class Bible: + def __init__(self, fname=None): + self.fname = fname or "verses-1769.json" + + # Load the JSON data from the file. + with open(self.fname, "r") as f: + self.verses = json.load(f) + + def iter_verses(self): + """ + Iterates over the verses in the Bible. + """ + + for verse in self.verses: + verse_ref = VerseReference.from_string(verse) + + yield Verse( + book=verse_ref.book, + chapter=verse_ref.chapter, + verse=verse_ref.verse, + text=self.verses[verse], + ) + + def iter_verse_references(self): + """ + Iterates over the verse references in the Bible. + """ + + for verse in self.verses: + yield VerseReference.from_string(verse) + + class Verse(BaseModel): book: str chapter: int @@ -40,3 +73,10 @@ class VerseReference(BaseModel): print(VerseReference.from_string("Genesis 1:1")) print(VerseReference.from_string("I Corinthians 1:1")) +print(VerseReference.from_string("John 3:16")) + + +bible = Bible() +for verse in bible.iter_verses(): + print(verse) + # break # Just print the first verse for demonstration