forked from Electro1512/MetroidAPrime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldMapping.py
39 lines (29 loc) · 1.11 KB
/
WorldMapping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from copy import deepcopy
from dataclasses import dataclass
from typing import Any, Dict, Type, TypeVar, Generic, TypedDict
T = TypeVar("T")
class AreaMappingDict(TypedDict):
area: str
type_mapping: Any
@dataclass
class AreaMapping(Generic[T]):
area: str
type_mapping: T
def to_dict(self) -> AreaMappingDict:
return {
"area": self.area,
"type_mapping": dict(self.type_mapping), # type: ignore
}
@classmethod
def from_dict(cls, data: AreaMappingDict) -> "AreaMapping[T]":
return cls(area=data["area"], type_mapping=data["type_mapping"])
class WorldMapping(Dict[str, AreaMapping[T]], Generic[T]):
def to_option_value(self) -> Dict[str, AreaMappingDict]:
return deepcopy({area: mapping.to_dict() for area, mapping in self.items()})
@classmethod
def from_option_value_generic(
cls, data: Dict[str, Any], area_cls: Type[AreaMapping[T]]
) -> "WorldMapping[T]":
return WorldMapping(
{area: area_cls.from_dict(mapping) for area, mapping in data.items()}
)