Skip to content

Commit

Permalink
feat: add remoteid and use for prompt deletion (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff authored Dec 13, 2023
2 parents 4b91f01 + 01ad405 commit 5f38dfe
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
DeletePrompts,
PushPrompts,
)
from ..prompts.base_prompt import BasePrompt
from ..prompts.base_prompt import BasePrompt, DestinationPrompt

K = TypeVar("K")
T = TypeVar("T")
Expand All @@ -21,7 +21,7 @@ class BaseDiffDeterminer(Protocol):
def sync(
self,
source_prompts: Sequence[BasePrompt],
destination_prompts: Sequence[BasePrompt],
destination_prompts: Sequence[DestinationPrompt],
) -> Sequence[PromptDestinationCommand]:
...

Expand Down Expand Up @@ -60,12 +60,13 @@ def __init__(self, tmp_read_dir: Path, tmp_write_dir: Path):
def sync(
self,
source_prompts: Sequence[BasePrompt],
destination_prompts: Sequence[BasePrompt],
destination_prompts: Sequence[DestinationPrompt],
) -> Sequence[PromptDestinationCommand]:
syncer = GeneralSyncer(
source={prompt.uid: prompt for prompt in source_prompts},
destination={
prompt.uid: prompt for prompt in destination_prompts
dest_prompt.prompt.uid: dest_prompt
for dest_prompt in destination_prompts
},
)

Expand All @@ -77,12 +78,3 @@ def sync(
tmp_read_dir=self.tmp_read_dir,
),
]


class FakeDiffDeterminer(BaseDiffDeterminer):
def sync(
self,
source_prompts: Sequence[BasePrompt],
destination_prompts: Sequence[BasePrompt],
) -> Sequence[PromptDestinationCommand]:
...
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
DeletePrompts,
PushPrompts,
)
from ..prompts.base_prompt import DestinationPrompt
from ..prompts.qa_prompt import QAPrompt
from .base_diff_determiner import GeneralSyncer, PromptDiffDeterminer

Expand All @@ -27,16 +28,27 @@ def test_prompt_diff_determiner(tmp_path: Path):
QAPrompt(question="b", answer="b"),
]
destination_prompts = [
QAPrompt(question="b", answer="b"),
QAPrompt(question="c", answer="c"),
DestinationPrompt(
QAPrompt(question="b", answer="b"), destination_id="2"
),
DestinationPrompt(
QAPrompt(question="c", answer="c"), destination_id="3"
),
]

diff = syncer.sync(
source_prompts=source_prompts,
destination_prompts=destination_prompts,
)
assert diff == [
DeletePrompts([QAPrompt(question="c", answer="c")]),
DeletePrompts(
[
DestinationPrompt(
QAPrompt(question="c", answer="c"),
destination_id="3",
)
]
),
PushPrompts(
[QAPrompt(question="a", answer="a")],
tmp_write_dir=tmp_path,
Expand Down
17 changes: 0 additions & 17 deletions personal_mnemonic_medium/v2/domain/main.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
AnkiConnectGateway,
NoteInfo,
)
from ...prompts.base_prompt import BasePrompt
from ...prompts.cloze_prompt import RemoteClozePrompt
from ...prompts.qa_prompt import RemoteQAPrompt
from ...prompts.base_prompt import DestinationPrompt
from ...prompts.cloze_prompt import ClozeWithoutDoc
from ...prompts.qa_prompt import QAWithoutDoc
from ..base_prompt_destination import PromptDestination
from .prompt_converter.anki_prompt_converter import (
AnkiPromptConverter,
Expand All @@ -32,38 +32,48 @@ def __init__(
self.gateway = gateway
self.prompt_converter = prompt_converter

def _note_info_to_prompt(self, note_info: NoteInfo) -> BasePrompt:
def _note_info_to_prompt(
self, note_info: NoteInfo
) -> DestinationPrompt:
if (
"Question" in note_info.fields
and "Answer" in note_info.fields
):
return RemoteQAPrompt(
question=note_info.fields["Question"].value,
answer=note_info.fields["Answer"].value,
add_tags=note_info.tags,
remote_id=str(note_info.noteId),
return DestinationPrompt(
QAWithoutDoc(
question=note_info.fields["Question"].value,
answer=note_info.fields["Answer"].value,
add_tags=note_info.tags,
),
destination_id=str(note_info.noteId),
)

if "Text" in note_info.fields:
return RemoteClozePrompt(
text=note_info.fields["Text"].value,
add_tags=note_info.tags,
remote_id=str(note_info.noteId),
return DestinationPrompt(
ClozeWithoutDoc(
text=note_info.fields["Text"].value,
add_tags=note_info.tags,
),
destination_id=str(note_info.noteId),
)

raise ValueError(
f"NoteInfo {note_info} has neither Question nor Text field"
)

def get_all_prompts(self) -> Sequence[BasePrompt]:
def get_all_prompts(self) -> Sequence[DestinationPrompt]:
return (
Seq(self.gateway.get_all_note_infos())
.map(self._note_info_to_prompt)
.to_list()
)

def _delete_prompts(self, prompts: Sequence[BasePrompt]) -> None:
prompt_ids = {int(prompt.uid) for prompt in prompts}
def _delete_prompts(
self, prompts: Sequence[DestinationPrompt]
) -> None:
prompt_ids = {
int(remote_prompt.prompt.uid) for remote_prompt in prompts
}
self.gateway.delete_notes(list(prompt_ids))

def _grouped_cards_to_deck(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)

from ....prompts.base_prompt import BasePrompt
from ....prompts.cloze_prompt import RemoteClozePrompt
from ....prompts.cloze_prompt import ClozeWithoutDoc
from ....prompts.qa_prompt import QAPrompt


Expand Down Expand Up @@ -45,7 +45,7 @@ def _prompt_to_card(self, prompt: BasePrompt) -> AnkiCard:
tags=prompt.tags,
css=self.card_css,
)
case RemoteClozePrompt():
case ClozeWithoutDoc():
card = AnkiCloze(
text=prompt.text,
deck=deck,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest

from ....prompts.base_prompt import BasePrompt
from ....prompts.cloze_prompt import RemoteClozePrompt
from ....prompts.qa_prompt import RemoteQAPrompt
from ....prompts.cloze_prompt import ClozeWithoutDoc
from ....prompts.qa_prompt import QAWithoutDoc
from .anki_prompt_converter import AnkiPromptConverter
from .prompts.anki_cloze import AnkiCloze
from .prompts.anki_qa import AnkiQA
Expand All @@ -25,18 +25,15 @@
("input_prompt", "expected_card"),
[
(
RemoteQAPrompt(
QAWithoutDoc(
question="FakeQuestion",
answer="FakeAnswer",
add_tags=["FakeTag"],
remote_id="1",
),
fake_anki_qa,
),
(
RemoteClozePrompt(
text="FakeText", add_tags=["FakeTag"], remote_id="1"
),
ClozeWithoutDoc(text="FakeText", add_tags=["FakeTag"]),
fake_anki_cloze,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
UpdateModel,
)
from ....data_access.test_ankiconnect import MockNoteInfo
from ...prompts.cloze_prompt import RemoteClozePrompt
from ...prompts.qa_prompt import RemoteQAPrompt
from ...prompts.cloze_prompt import ClozeWithoutDoc
from ...prompts.qa_prompt import QAWithoutDoc
from ..destination_commands import PushPrompts
from .ankiconnect_destination import AnkiConnectDestination
from .prompt_converter.anki_prompt_converter import (
Expand Down Expand Up @@ -61,16 +61,13 @@ def test_ankiconnect_push_prompts(tmpdir: Path):
[
PushPrompts(
prompts=[
RemoteQAPrompt(
QAWithoutDoc(
question="FakeQuestion",
answer="FakeAnswer",
add_tags=["FakeTag"],
remote_id="1",
),
RemoteClozePrompt(
text="FakeText",
add_tags=["FakeTag"],
remote_id="1",
ClozeWithoutDoc(
text="FakeText", add_tags=["FakeTag"]
),
],
tmp_write_dir=tmpdir,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from collections.abc import Sequence
from typing import Protocol

from ..prompts.base_prompt import BasePrompt
from ..prompts.base_prompt import DestinationPrompt
from .destination_commands import PromptDestinationCommand


class PromptDestination(Protocol):
def get_all_prompts(self) -> Sequence[BasePrompt]:
def get_all_prompts(self) -> Sequence[DestinationPrompt]:
...

def update(
Expand All @@ -16,7 +16,7 @@ def update(


class FakePromptDestination(PromptDestination):
def get_all_prompts(self) -> Sequence[BasePrompt]:
def get_all_prompts(self) -> Sequence[DestinationPrompt]:
...

def update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from dataclasses import dataclass
from pathlib import Path

from ..prompts.base_prompt import BasePrompt
from ..prompts.base_prompt import BasePrompt, DestinationPrompt


@dataclass(frozen=True)
class DeletePrompts:
prompts: Sequence[BasePrompt]
prompts: Sequence[DestinationPrompt]


@dataclass(frozen=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
from collections.abc import Sequence

from ...prompts.cloze_prompt import ClozePrompt, ClozePromptFromDoc
from ...prompts.cloze_prompt import ClozeFromDoc, ClozePrompt
from ..document_ingesters.document import Document
from .base_prompt_extractor import BasePromptExtractor

Expand Down Expand Up @@ -68,7 +68,7 @@ def _replace_cloze_id_with_unique(
def extract_prompts(
self, document: Document
) -> Sequence[ClozePrompt]:
prompts: list[ClozePromptFromDoc] = []
prompts: list[ClozeFromDoc] = []

blocks = self._break_string_by_two_or_more_newlines(
document.content
Expand All @@ -90,7 +90,7 @@ def extract_prompts(
)

prompts.append(
ClozePromptFromDoc(
ClozeFromDoc(
text=prompt_content, source_doc=document
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from collections.abc import Sequence

from ...prompts.qa_prompt import QAPrompt, QAPromptFromDoc
from ...prompts.qa_prompt import QAFromDoc, QAPrompt
from ..document_ingesters.document import Document
from .base_prompt_extractor import BasePromptExtractor

Expand Down Expand Up @@ -79,7 +79,7 @@ def extract_prompts(
continue

prompts.append(
QAPromptFromDoc(
QAFromDoc(
question=question,
answer=answer,
parent_doc=document,
Expand Down
9 changes: 9 additions & 0 deletions personal_mnemonic_medium/v2/domain/prompts/base_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ def uid(self) -> int:
@property
def tags(self) -> Sequence[str]:
...


from dataclasses import dataclass


@dataclass(frozen=True)
class DestinationPrompt:
prompt: BasePrompt
destination_id: str
5 changes: 2 additions & 3 deletions personal_mnemonic_medium/v2/domain/prompts/cloze_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def tags(self) -> Sequence[str]:


@dataclass(frozen=True)
class RemoteClozePrompt(ClozePrompt):
remote_id: str
class ClozeWithoutDoc(ClozePrompt):
add_tags: Sequence[str]

@property
Expand All @@ -32,7 +31,7 @@ def tags(self) -> Sequence[str]:


@dataclass(frozen=True)
class ClozePromptFromDoc(ClozePrompt):
class ClozeFromDoc(ClozePrompt):
text: str
source_doc: Document

Expand Down
5 changes: 2 additions & 3 deletions personal_mnemonic_medium/v2/domain/prompts/qa_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def tags(self) -> Sequence[str]:


@dataclass(frozen=True)
class RemoteQAPrompt(QAPrompt):
remote_id: str
class QAWithoutDoc(QAPrompt):
add_tags: Sequence[str]

@property
Expand All @@ -35,7 +34,7 @@ def tags(self) -> Sequence[str]:


@dataclass(frozen=True)
class QAPromptFromDoc(QAPrompt):
class QAFromDoc(QAPrompt):
parent_doc: Document
line_nr: int

Expand Down

0 comments on commit 5f38dfe

Please sign in to comment.