|
15 | 15 |
|
16 | 16 | from collections.abc import ItemsView, Iterable, KeysView, Mapping, ValuesView |
17 | 17 | from pathlib import Path |
18 | | -from typing import Any, Optional, SupportsIndex, TypeVar, Union |
| 18 | +from typing import TYPE_CHECKING, Any, Callable, Optional, SupportsIndex, TypeVar, Union |
19 | 19 |
|
20 | 20 | from torch import Tensor |
21 | 21 | from typing_extensions import Self |
22 | 22 |
|
23 | 23 | import lightning.pytorch as pl |
24 | 24 | from lightning.pytorch.callbacks import Checkpoint |
25 | 25 |
|
| 26 | +if TYPE_CHECKING: |
| 27 | + from _typeshed import SupportsRichComparison |
| 28 | + |
26 | 29 |
|
27 | 30 | def _version(loggers: list[Any], separator: str = "_") -> Union[int, str]: |
28 | 31 | if len(loggers) == 1: |
@@ -122,28 +125,96 @@ def __init__(self, loggers: Union[Iterable[_T], Mapping[str, _T]] = None): |
122 | 125 | self._dict: dict = {} |
123 | 126 |
|
124 | 127 | def __eq__(self, other: Any) -> bool: |
125 | | - self_list = list(self) |
| 128 | + list_eq = list.__eq__(self, other) |
126 | 129 | if isinstance(other, _ListMap): |
127 | | - return self_list == list(other) and self._dict == other._dict |
128 | | - if isinstance(other, list): |
129 | | - return self_list == other |
130 | | - return False |
| 130 | + dict_eq = self._dict == other._dict |
| 131 | + return list_eq and dict_eq |
| 132 | + return list_eq |
| 133 | + |
| 134 | + def copy(self): |
| 135 | + new_listmap = _ListMap(self) |
| 136 | + new_listmap._dict = self._dict.copy() |
| 137 | + return new_listmap |
| 138 | + |
| 139 | + def extend(self, __iterable: Iterable[_T]) -> None: |
| 140 | + if isinstance(__iterable, _ListMap): |
| 141 | + offset = len(self) |
| 142 | + for key, idx in __iterable._dict.items(): |
| 143 | + self._dict[key] = idx + offset |
| 144 | + super().extend(__iterable) |
| 145 | + |
| 146 | + def pop(self, key: Union[SupportsIndex, str] = -1, default: Optional[Any] = None) -> _T: |
| 147 | + if isinstance(key, int): |
| 148 | + ret = list.pop(self, key) |
| 149 | + for str_key, idx in list(self._dict.items()): |
| 150 | + if idx == key: |
| 151 | + self._dict.pop(str_key) |
| 152 | + elif idx > key: |
| 153 | + self._dict[str_key] = idx - 1 |
| 154 | + return ret |
| 155 | + if isinstance(key, str): |
| 156 | + if key not in self._dict: |
| 157 | + return default |
| 158 | + return self.pop(self._dict[key]) |
| 159 | + raise TypeError("Key must be int or str") |
| 160 | + |
| 161 | + def insert(self, index: SupportsIndex, __object: _T) -> None: |
| 162 | + for key, idx in self._dict.items(): |
| 163 | + if idx >= index: |
| 164 | + self._dict[key] = idx + 1 |
| 165 | + list.insert(self, index, __object) |
| 166 | + |
| 167 | + def remove(self, __object: _T) -> None: |
| 168 | + idx = self.index(__object) |
| 169 | + name = None |
| 170 | + for key, val in self._dict.items(): |
| 171 | + if val == idx: |
| 172 | + name = key |
| 173 | + elif val > idx: |
| 174 | + self._dict[key] = val - 1 |
| 175 | + if name: |
| 176 | + self._dict.pop(name, None) |
| 177 | + list.remove(self, __object) |
| 178 | + |
| 179 | + def sort( |
| 180 | + self, |
| 181 | + *, |
| 182 | + key: Optional[Callable[[_T], "SupportsRichComparison"]] = None, |
| 183 | + reverse: bool = False, |
| 184 | + ) -> None: |
| 185 | + # Create a mapping from item to its name(s) |
| 186 | + item_to_names = {} |
| 187 | + for name, idx in self._dict.items(): |
| 188 | + item = self[idx] |
| 189 | + item_to_names.setdefault(item, []).append(name) |
| 190 | + # Sort the list |
| 191 | + list.sort(self, key=key, reverse=reverse) |
| 192 | + # Update _dict with new indices |
| 193 | + new_dict = {} |
| 194 | + for idx, item in enumerate(self): |
| 195 | + if item in item_to_names: |
| 196 | + for name in item_to_names[item]: |
| 197 | + new_dict[name] = idx |
| 198 | + self._dict = new_dict |
131 | 199 |
|
132 | 200 | # --- List-like interface --- |
133 | 201 | def __getitem__(self, key: Union[int, slice, str]) -> _T: |
134 | | - if isinstance(key, (int, slice)): |
135 | | - return list.__getitem__(self, key) |
136 | 202 | if isinstance(key, str): |
137 | | - return list.__getitem__(self, self._dict[key]) |
138 | | - raise TypeError("Key must be int / slice (for index) or str (for name).") |
| 203 | + return self[self._dict[key]] |
| 204 | + return list.__getitem__(self, key) |
139 | 205 |
|
140 | | - def __add__(self, other: Union[list[_T], Self]) -> list[_T]: |
141 | | - # todo |
142 | | - return list.__add__(self, other) |
| 206 | + def __add__(self, other: Union[list[_T], Self]) -> Self: |
| 207 | + new_listmap = self.copy() |
| 208 | + new_listmap += other |
| 209 | + return new_listmap |
143 | 210 |
|
144 | 211 | def __iadd__(self, other: Union[list[_T], Self]) -> Self: |
145 | | - # todo |
146 | | - return list.__iadd__(self, other) |
| 212 | + if isinstance(other, _ListMap): |
| 213 | + offset = len(self) |
| 214 | + for key, idx in other._dict.items(): |
| 215 | + self._dict[key] = idx + offset |
| 216 | + |
| 217 | + return super().__iadd__(other) |
147 | 218 |
|
148 | 219 | def __setitem__(self, key: Union[SupportsIndex, slice, str], value: _T) -> None: |
149 | 220 | if isinstance(key, (int, slice)): |
@@ -192,20 +263,6 @@ def items(self) -> ItemsView[str, _T]: |
192 | 263 | return d.items() |
193 | 264 |
|
194 | 265 | # --- List and Dict interface --- |
195 | | - def pop(self, key: Union[SupportsIndex, str] = -1, default: Optional[Any] = None) -> _T: |
196 | | - if isinstance(key, int): |
197 | | - ret = list.pop(self, key) |
198 | | - for str_key, idx in list(self._dict.items()): |
199 | | - if idx == key: |
200 | | - self._dict.pop(str_key) |
201 | | - elif idx > key: |
202 | | - self._dict[str_key] = idx - 1 |
203 | | - return ret |
204 | | - if isinstance(key, str): |
205 | | - if key not in self._dict: |
206 | | - return default |
207 | | - return self.pop(self._dict[key]) |
208 | | - raise TypeError("Key must be int or str") |
209 | 266 |
|
210 | 267 | def __repr__(self) -> str: |
211 | 268 | ret = super().__repr__() |
|
0 commit comments