Skip to content

Commit

Permalink
💪 Adding sentence fetch queries
Browse files Browse the repository at this point in the history
This is a temporary commit for the purpose of reaching out for assistance in better understanding async sqlalchemy and race conditions when generating ORM models.

Probably a million other things are wrong and/or stupid.  That's why I'm asking for help.
  • Loading branch information
beverage committed Oct 20, 2024
1 parent d539570 commit a0ead11
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/dbtest/dbtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
from .database.utils import object_as_dict

from .sentences.create import create_random_problem_with_delay, create_random_problem
from .sentences.create import create_random_sentence, create_sentence, problem_formatter
from .sentences.create import create_random_sentence, create_sentence
from .sentences.database import get_sentence
from .sentences.utils import problem_formatter

from .verbs.get import download_verb, get_verb, get_random_verb

from .utils.console import Style
Expand Down Expand Up @@ -80,6 +83,13 @@ async def batch(quantity: int, workers: int):
async def sentence():
pass

@sentence.command('get')
@click.option('-q', '--quantity', required=False, default=1)
@sentence_options
async def get(quantity: int, **kwargs):
result = await get_sentence(quantity, **kwargs)
print(problem_formatter([result.first()]))

@sentence.command('new')
@click.option('-q', '--quantity', required=False, default=1)
@sentence_options
Expand Down
27 changes: 20 additions & 7 deletions src/dbtest/sentences/database.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@

from ..database.engine import AsyncSession, get_async_session
from .models import Pronoun, Sentence, Tense, DirectObject, IndirectPronoun, Negation
from ..database.engine import AsyncSession, get_async_session, Base
from .models import Pronoun, Tense, DirectObject, IndirectPronoun, Negation, Sentence

async def get_sentence(verb_infinitive: str,
from sqlalchemy import inspect
from sqlalchemy import select

import logging

async def get_sentence(quantity: int,
verb_infinitive: str,
pronoun: Pronoun = Pronoun.first_person, # Pronoun and tense will remain both random
tense: Tense = Tense.present, # and correct for now. These values will be
direct_object: DirectObject = DirectObject.none, # ignored.
indirect_pronoun: IndirectPronoun = IndirectPronoun.none,
negation: Negation = Negation.none,
is_correct: bool = True, # This cannot be guaranteed until the AI has responded.
openai_client: AsyncSession = get_async_session()):
is_correct: bool = True): # This cannot be guaranteed until the AI has responded.

async with get_async_session() as session:
# The sentence is returned as a DTO.
pass

stmt = select(Sentence).where(Sentence.infinitive == verb_infinitive).limit(quantity)

logging.info(str(stmt))

sentence: Sentence = (
await session.execute(stmt)
)

return sentence

async def save_sentence(sentence: Sentence):
async with get_async_session() as session:
Expand Down

0 comments on commit a0ead11

Please sign in to comment.