Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applied iterator pattern to Response #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions backend/controller/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from pydantic import BaseModel

from covid_nlp.language.detect_language import LanguageDetector
from collections.abc import Iterable, Iterator


from backend.config import (
DB_HOST,
Expand Down Expand Up @@ -116,14 +118,32 @@ class Answer(BaseModel):
# TODO move these two into "meta" also for the regular extractive QA


class ResponseToIndividualQuestion(BaseModel):
class ResponseToIndividualQuestion(Iterator):
_position: int = None
_reverse: bool = False
question: str
answers: List[Optional[Answer]]
model_id: int


class Response(BaseModel):
def __init__(self, results, reverse = False) -> None:
self._results = results
self._reverse = reverse
self._position = -1 if reverse else 0
def __next__(self):
try:
value = self._results[self._position]
self._position += -1 if self._reverse else 1
except IndexError:
raise StopIteration()

return value
class Response(Iterable):
def __init__(self, results: List[ResponseToIndividualQuestion] = []) -> None:
self._results = results
results: List[ResponseToIndividualQuestion]

def add_item(self, item):
self._results.append(item)


#############################################
Expand Down