From 61ce5521a8e68e57141dd2546eaf6c0dff485c16 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 26 May 2025 12:35:20 -0400 Subject: [PATCH] Restructure project into kjvstudy package - Move kjv.py and server files into kjvstudy package directory - Add package __init__.py with module imports - Move tests to tests/ directory with path configuration - Add .envrc to prevent Python bytecode generation - Update imports to use relative imports within package - Create global bible instance for reuse across modules --- .envrc | 1 + kjvstudy/__init__.py | 2 ++ kjvstudy/__main__.py | 0 kjv.py => kjvstudy/kjv.py | 47 +++++++++++++++++--------------- t.py => kjvstudy/server.py | 6 ++-- test_kjv.py => tests/test_kjv.py | 9 ++++-- 6 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 .envrc create mode 100644 kjvstudy/__init__.py create mode 100644 kjvstudy/__main__.py rename kjv.py => kjvstudy/kjv.py (78%) rename t.py => kjvstudy/server.py (94%) rename test_kjv.py => tests/test_kjv.py (80%) diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..eca243a --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +export PYTHONDONTWRITEBYTECODE=1 diff --git a/kjvstudy/__init__.py b/kjvstudy/__init__.py new file mode 100644 index 0000000..783edad --- /dev/null +++ b/kjvstudy/__init__.py @@ -0,0 +1,2 @@ +from . import kjv +from . import server diff --git a/kjvstudy/__main__.py b/kjvstudy/__main__.py new file mode 100644 index 0000000..e69de29 diff --git a/kjv.py b/kjvstudy/kjv.py similarity index 78% rename from kjv.py rename to kjvstudy/kjv.py index 6ca05e3..fcaf112 100644 --- a/kjv.py +++ b/kjvstudy/kjv.py @@ -1,8 +1,11 @@ -import json from pydantic import BaseModel +import json + class Bible: + """Represents a Bible.""" + def __init__(self, fname=None): self.fname = fname or "verses-1769.json" @@ -11,9 +14,7 @@ class Bible: self.verses = json.load(f) def __getitem__(self, verse): - """ - Returns the text of the verse. - """ + """Returns the text of the verse.""" # Check if the verse exists in the dictionary. if verse not in self.verses: @@ -22,24 +23,30 @@ class Bible: return self.verses[verse] def iter_verses(self): - """ - Iterates over the verses in the Bible. - """ + """Iterates over the verses in the Bible.""" for verse in self.verses: verse_ref = VerseReference.from_string(verse) + # Remove the leading "# " and brackets from the text. + # This is a workaround for the JSON format. + # The text is stored as a string with leading "# " and brackets. + # Example: "# [In the beginning God created the heaven and the earth.]" + text = self.verses[verse] + text.replace("# ", "") + text.replace("[", "") + text.replace("]", "") + yield Verse( book=verse_ref.book, chapter=verse_ref.chapter, verse=verse_ref.verse, - text=self.verses[verse], + text=text, ) def iter_books(self): - """ - Iterates over the books in the Bible. - """ + """Iterates over the books in the Bible.""" + yielded = set() for verse in self.verses: @@ -50,9 +57,7 @@ class Bible: yield verse_ref.book def iter_chapters(self): - """ - Iterates over the chapters in the Bible. - """ + """Iterates over the chapters in the Bible.""" yielded = set() @@ -64,9 +69,7 @@ class Bible: yield verse_ref.book, verse_ref.chapter def iter_chapters_by_book(self): - """ - Iterates over the chapters in the Bible, grouped by book. - """ + """Iterates over the chapters in the Bible, grouped by book.""" yielded = set() @@ -78,9 +81,7 @@ class Bible: yield verse_ref.book, verse_ref.chapter def iter_verse_references(self): - """ - Iterates over the verse references in the Bible. - """ + """Iterates over the verse references in the Bible.""" for verse in self.verses: yield VerseReference.from_string(verse) @@ -123,14 +124,16 @@ class VerseReference(BaseModel): return cls(book=book, chapter=int(chapter), verse=int(verse)) +# Create an instance of the Bible class. +bible = Bible() + + if __name__ == "__main__": print(VerseReference.from_string("Genesis 1:1")) print(VerseReference.from_string("I Corinthians 1:1")) print(VerseReference.from_string("John 3:16")) - bible = Bible() - print(bible["Genesis 1:1"]) # print() # print(verse) diff --git a/t.py b/kjvstudy/server.py similarity index 94% rename from t.py rename to kjvstudy/server.py index 3e55148..4f2b873 100644 --- a/t.py +++ b/kjvstudy/server.py @@ -2,7 +2,9 @@ from fastapi import FastAPI, HTTPException, Request from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates -import kjv # assuming your kjv module is available and contains the Bible class + +from .kjv import bible + app = FastAPI() @@ -10,8 +12,6 @@ app = FastAPI() app.mount("/static", StaticFiles(directory="static"), name="static") templates = Jinja2Templates(directory="templates") -bible = kjv.Bible() - @app.get("/", response_class=HTMLResponse) def read_root(request: Request): diff --git a/test_kjv.py b/tests/test_kjv.py similarity index 80% rename from test_kjv.py rename to tests/test_kjv.py index c3062b5..8f03f41 100644 --- a/test_kjv.py +++ b/tests/test_kjv.py @@ -1,6 +1,11 @@ -def test_verse_references(): - from kjv import VerseReference +# PATH HACK +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) +from kjvstudy.kjv import VerseReference + +def test_verse_references(): # Test the parsing of a verse reference string assert VerseReference.from_string("Genesis 1:1") == VerseReference( book="Genesis", chapter=1, verse=1