Skip to content

typing

Typing Logic.

Attributes⚓︎

Classes⚓︎

AtEndOfExpectedError ⚓︎

Bases: Exception

Reached end of the expected keys.

Source code in tui_typer_tutor/core/typing.py
class AtEndOfExpectedError(Exception):
    """Reached end of the expected keys."""

ExpectedKey ⚓︎

Bases: BaseModel

Expected Key.

Source code in tui_typer_tutor/core/typing.py
class ExpectedKey(BaseModel):
    """Expected Key."""

    textual: str
    """Textual Key Name."""

    @property
    def text(self) -> str:
        """Displayed text."""
        return DISPLAY_TO_TEXTUAL.inverse.get(self.textual) or UNKNOWN

Attributes⚓︎

text property ⚓︎
text

Displayed text.

textual instance-attribute ⚓︎
textual

Textual Key Name.

Keys ⚓︎

Bases: BaseModel

Key Model.

Source code in tui_typer_tutor/core/typing.py
class Keys(BaseModel):
    """Key Model."""

    expected: list[ExpectedKey] = Field(default_factory=list)
    """The expected keys for practice."""

    typed_all: list[TypedKey] = Field(default_factory=list)
    """Append-only list of typed keys."""

    typed: list[TypedKey] = Field(default_factory=list)
    """Only tracks non-deleted typed keys."""

    last_was_delete: bool = False
    """Indicate if last operation was a delete."""

    @beartype
    def store(self, *, key: TypedKey, is_delete: bool) -> None:
        """Store a new typed key."""
        self.typed_all.append(key)
        self.last_was_delete = is_delete
        if not is_delete:
            self.typed.append(key)
        elif self.typed:
            self.typed = self.typed[:-1]

Attributes⚓︎

expected class-attribute instance-attribute ⚓︎
expected = Field(default_factory=list)

The expected keys for practice.

last_was_delete class-attribute instance-attribute ⚓︎
last_was_delete = False

Indicate if last operation was a delete.

typed class-attribute instance-attribute ⚓︎
typed = Field(default_factory=list)

Only tracks non-deleted typed keys.

typed_all class-attribute instance-attribute ⚓︎
typed_all = Field(default_factory=list)

Append-only list of typed keys.

Functions⚓︎

store ⚓︎
store(*, key, is_delete)

Store a new typed key.

Source code in tui_typer_tutor/core/typing.py
@beartype
def store(self, *, key: TypedKey, is_delete: bool) -> None:
    """Store a new typed key."""
    self.typed_all.append(key)
    self.last_was_delete = is_delete
    if not is_delete:
        self.typed.append(key)
    elif self.typed:
        self.typed = self.typed[:-1]

TypedKey ⚓︎

Bases: ExpectedKey

Typed Key.

Source code in tui_typer_tutor/core/typing.py
class TypedKey(ExpectedKey):
    """Typed Key."""

    expected: ExpectedKey | None = None
    """Store the expected key when typed and expected become out-of-sync."""

    @property
    def was_correct(self) -> bool:
        """If typed key matches expected."""
        return self.expected is not None and self.text == self.expected.text

Attributes⚓︎

expected class-attribute instance-attribute ⚓︎
expected = None

Store the expected key when typed and expected become out-of-sync.

was_correct property ⚓︎
was_correct

If typed key matches expected.

Functions⚓︎

on_keypress ⚓︎

on_keypress(textual, keys)

Process a key press.

Source code in tui_typer_tutor/core/typing.py
@beartype
def on_keypress(textual: str, keys: Keys) -> None:
    """Process a key press."""
    is_delete = textual == BACKSPACE
    if not is_delete and len(keys.typed) == len(keys.expected):
        raise AtEndOfExpectedError
    expected = None if is_delete else keys.expected[len(keys.typed)]
    key = TypedKey(textual=textual, expected=expected)
    keys.store(key=key, is_delete=is_delete)