Skip to content

Commit

Permalink
Use dict lookup instead of list
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderVertegaal committed Aug 15, 2024
1 parent 4e996d4 commit b3fb33f
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions backend/aethel_db/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,19 @@ class AethelListResponse:
Response object for Aethel query view.
"""

results: List[AethelListItem] = field(default_factory=list)
results: dict[tuple[str, str, str], AethelListItem] = field(default_factory=dict)
error: Optional[str] = None

def get_or_create_result(self, lemma: str, word: str, type: str) -> AethelListItem:
"""
Return an existing result with the same lemma, word, and type, or create a new one if it doesn't exist.
"""
for result in self.results:
if result.lemma == lemma and result.type == type and result.word == word:
return result
key = (lemma, word, type)
new_result = AethelListItem(lemma=lemma, word=word, type=type, samples=[])
self.results.append(new_result)
return new_result
return self.results.setdefault(key, new_result)

def json_response(self) -> JsonResponse:
results = [result.serialize() for result in self.results]
results = [result.serialize() for result in self.results.values()]

return JsonResponse(
{
Expand Down

0 comments on commit b3fb33f

Please sign in to comment.