abstractmethods
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from collections.abc import Callable, MutableMapping, MutableSequence
|
||||
from typing import Generic, TypeVar
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import MutableMapping, MutableSequence
|
||||
from typing import Generic, Protocol, TypeVar
|
||||
|
||||
from . import events as e
|
||||
|
||||
@@ -8,22 +9,31 @@ T = TypeVar("T")
|
||||
type MutableNesting[T] = T | MutableSequence[T] | MutableMapping[T, MutableNesting[T]]
|
||||
|
||||
|
||||
class HookedContainer(Generic[T]):
|
||||
class HookFunction(Protocol):
|
||||
def __call__(self, event: e.ChangeEvent[T]) -> None: ...
|
||||
|
||||
|
||||
class HookedContainer(Generic[T], ABC):
|
||||
_path: MutableSequence[int]
|
||||
hook: Callable[[e.ChangeEvent[T]], None] | None
|
||||
hook: HookFunction
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.__class__.__name__}({self._data!r})"
|
||||
|
||||
def __len__(self):
|
||||
return len(self._data)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._data)
|
||||
|
||||
def __contains__(self, value):
|
||||
return value in self._data
|
||||
|
||||
# MutableSequence Methods
|
||||
|
||||
def __len__(self):
|
||||
return len(self._data)
|
||||
|
||||
@abstractmethod
|
||||
def __getitem__(self, key): ...
|
||||
|
||||
def __setitem__(self, s, value):
|
||||
self._data[s] = value
|
||||
if self.hook:
|
||||
@@ -33,3 +43,6 @@ class HookedContainer(Generic[T]):
|
||||
del self._data[s]
|
||||
if self.hook:
|
||||
self.hook(e.RemoveItemEvent(index=s, item=self._data[s]))
|
||||
|
||||
@abstractmethod
|
||||
def insert(self, index, value): ...
|
||||
|
||||
@@ -7,6 +7,7 @@ T = TypeVar("T")
|
||||
@dataclass(frozen=True)
|
||||
class ChangeEvent(Generic[T]):
|
||||
index: int
|
||||
item: T
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
from collections.abc import Iterable, MutableSequence, Sequence
|
||||
from collections.abc import MutableSequence
|
||||
from copy import copy
|
||||
from typing import TypeVar
|
||||
|
||||
from . import events as e
|
||||
from .common import HookedContainer
|
||||
from .common import HookedContainer, HookFunction
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class HookedList(HookedContainer[T], MutableSequence[T]):
|
||||
_data: MutableSequence[T]
|
||||
path: MutableSequence[int]
|
||||
|
||||
def __init__(self, hook, existing: Iterable[T], path: Sequence[int] | None = None):
|
||||
def __init__(self, hook: HookFunction, existing: MutableSequence[T], path: MutableSequence[int] | None = None):
|
||||
self.hook = hook
|
||||
match existing:
|
||||
case HookedContainer(_data=seq):
|
||||
self._data = seq
|
||||
case MutableSequence() as seq:
|
||||
self._data = seq
|
||||
case _ as it:
|
||||
|
||||
Reference in New Issue
Block a user