rename
This commit is contained in:
55
src/hooked_containers/container.py
Normal file
55
src/hooked_containers/container.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Container, Iterable, MutableMapping, MutableSequence, Sized
|
||||
from typing import Protocol, TypeVar
|
||||
|
||||
from . import events as e
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
type MutableNesting[T] = T | MutableSequence[T] | MutableMapping[T, MutableNesting[T]]
|
||||
|
||||
|
||||
class HookFunction(Protocol):
|
||||
def __call__(self, event: e.ChangeEvent[T]) -> None: ...
|
||||
|
||||
|
||||
class HookedContainer(ABC, Sized, Iterable[T], Container[T]):
|
||||
_path: MutableSequence[int]
|
||||
hook: HookFunction
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.__class__.__name__}({self._data!r})"
|
||||
|
||||
# Sequence Methods
|
||||
# __contains__, __iter__, __reversed__, index, and count
|
||||
|
||||
@abstractmethod
|
||||
def __getitem__(self, key): ...
|
||||
|
||||
def __len__(self):
|
||||
return len(self._data)
|
||||
|
||||
# MutableSequence Methods
|
||||
# append, reverse, extend, pop, remove, and __iadd__
|
||||
|
||||
def __setitem__(self, s, value):
|
||||
self._data[s] = value
|
||||
if self.hook:
|
||||
self.hook(e.SetItemEvent(index=s, item=value, path=self._path))
|
||||
|
||||
def __delitem__(self, s):
|
||||
item = self._data.pop(s)
|
||||
if self.hook:
|
||||
self.hook(e.RemoveItemEvent(index=s, item=item, path=self._path))
|
||||
|
||||
@abstractmethod
|
||||
def insert(self, index, value): ...
|
||||
|
||||
# Custom Methods
|
||||
|
||||
def get_nested(self, keys):
|
||||
"""Recursively call __getitem__ with each key in the iterable."""
|
||||
result = self
|
||||
for key in keys:
|
||||
result = result[key]
|
||||
return result
|
||||
Reference in New Issue
Block a user