Compare commits
5 Commits
f9d5b12a21
...
47aaa2d28e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
47aaa2d28e | ||
|
|
fcc8ad68ae | ||
|
|
cd3679cbd7 | ||
|
|
a58f043eaa | ||
|
|
8cff84f4e9 |
20
.vscode/settings.json
vendored
Normal file
20
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"python.testing.pytestArgs": [
|
||||||
|
"tests"
|
||||||
|
],
|
||||||
|
"python.testing.unittestEnabled": false,
|
||||||
|
"python.testing.pytestEnabled": true,
|
||||||
|
"[python]": {
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"editor.codeActionsOnSave": {
|
||||||
|
"source.fixAll": "explicit",
|
||||||
|
"source.organizeImports": "explicit"
|
||||||
|
},
|
||||||
|
"editor.defaultFormatter": "charliermarsh.ruff"
|
||||||
|
},
|
||||||
|
"notebook.formatOnSave.enabled": true,
|
||||||
|
"notebook.codeActionsOnSave": {
|
||||||
|
"notebook.source.fixAll": "explicit",
|
||||||
|
"notebook.source.organizeImports": "explicit"
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -19,3 +19,24 @@ dev = [
|
|||||||
"rich>=14.3.3",
|
"rich>=14.3.3",
|
||||||
"ruff>=0.15.2",
|
"ruff>=0.15.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[tool.ruff]
|
||||||
|
line-length = 120
|
||||||
|
target-version = "py312"
|
||||||
|
|
||||||
|
[tool.ruff.lint]
|
||||||
|
select = [
|
||||||
|
"E", # pycodestyle errors
|
||||||
|
"W", # pycodestyle warnings
|
||||||
|
"F", # pyflakes
|
||||||
|
"I", # isort
|
||||||
|
"UP", # pyupgrade
|
||||||
|
"B", # flake8-bugbear
|
||||||
|
"C4", # flake8-comprehensions
|
||||||
|
"SIM", # flake8-simplify
|
||||||
|
]
|
||||||
|
extend-fixable = ["ALL"]
|
||||||
|
ignore = ["UP046", "UP047"]
|
||||||
|
|
||||||
|
[tool.ruff.lint.isort]
|
||||||
|
known-first-party = ["hooked_containers"]
|
||||||
|
|||||||
@@ -1,10 +1,85 @@
|
|||||||
|
from collections.abc import Callable, Iterable, MutableSequence, Sequence
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from enum import Enum, auto
|
||||||
from typing import Generic, TypeVar
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar("T")
|
||||||
|
|
||||||
class HookedList(Generic[T], list[T]):
|
|
||||||
def __init__(self, *args, **kwargs):
|
class ListChange(Enum):
|
||||||
super().__init__(*args, **kwargs)
|
ADD_ITEM = auto()
|
||||||
|
REMOVE_ITEM = auto()
|
||||||
|
SET_ITEM = auto()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class ChangeEvent(Generic[T]):
|
||||||
|
index: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class AddItemEvent(ChangeEvent[T]):
|
||||||
|
item: T
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class SetItemEvent(ChangeEvent[T]):
|
||||||
|
item: T
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class RemoveItemEvent(ChangeEvent[T]):
|
||||||
|
item: T
|
||||||
|
|
||||||
|
|
||||||
|
class HookedList(Generic[T], MutableSequence[T]):
|
||||||
|
_data: MutableSequence[T]
|
||||||
|
_path: MutableSequence[int]
|
||||||
|
hook: Callable[[ChangeEvent[T]], None] | None
|
||||||
|
|
||||||
|
def __init__(self, iterable: Iterable[T], path: Sequence[int] | None = None, *, hook=None):
|
||||||
|
match iterable:
|
||||||
|
case MutableSequence() as seq:
|
||||||
|
self._data = seq
|
||||||
|
case Iterable() as it:
|
||||||
|
self._data = list(it)
|
||||||
|
self._path = list(path) if path is not None else []
|
||||||
|
self.hook = hook
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'{self.__class__.__name__}({super().__repr__()})'
|
return f"{self.__class__.__name__}({self._data!r})"
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self._data)
|
||||||
|
|
||||||
|
def __contains__(self, value):
|
||||||
|
return value in self._data
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self._data)
|
||||||
|
|
||||||
|
def __getitem__(self, s):
|
||||||
|
# print("Getting item:", s)
|
||||||
|
match self._data[s]:
|
||||||
|
case HookedList() as hs:
|
||||||
|
hs.hook = self.hook
|
||||||
|
return hs
|
||||||
|
case MutableSequence() as seq:
|
||||||
|
return HookedList(seq, hook=self.hook)
|
||||||
|
case _ as item:
|
||||||
|
return item
|
||||||
|
|
||||||
|
def __setitem__(self, s, value):
|
||||||
|
self._data[s] = value
|
||||||
|
if self.hook:
|
||||||
|
self.hook(SetItemEvent(index=s, item=value))
|
||||||
|
|
||||||
|
def __delitem__(self, s):
|
||||||
|
del self._data[s]
|
||||||
|
if self.hook:
|
||||||
|
self.hook(RemoveItemEvent(index=s, item=self._data[s]))
|
||||||
|
|
||||||
|
def insert(self, index, value):
|
||||||
|
self._data.insert(index, value)
|
||||||
|
if self.hook:
|
||||||
|
self.hook(AddItemEvent(index=index, item=value))
|
||||||
|
|||||||
Reference in New Issue
Block a user