Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Generic, TypeVar, Union, Dict, Any, Type, cast
- T = TypeVar("T")
- class Indexed(Generic[T]):
- __slots__ = ("_index", "_value")
- def __init__(self, index: int, value: T) -> None:
- self._index = index
- self._value = value
- @property
- def index(self) -> int:
- return self._index
- @property
- def value(self) -> T:
- return self._value
- def __str__(self) -> str:
- return f"({self.index}, {self.value})"
- def __eq__(self, other) -> bool:
- return isinstance(other, Indexed) and other.index == self.index and other.value == self.value
- def __hash__(self) -> int:
- return hash(self.index) + 31 * hash(self.value)
- def serialize_index(i: Indexed[T]) -> Dict[str, Union[int, T]]:
- return {"index": i.index, "value": i.value}
- def deserialize_index(value_type: Type[T], d: Dict[str, Any]) -> Indexed[T]:
- return Indexed(index=d["index"], value=cast(value_type, d["value"]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement