Skip to content

Commit 0e14e09

Browse files
committed
docs
1 parent a393ae3 commit 0e14e09

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

src/lightning/pytorch/loggers/utilities.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,46 @@ def _log_hyperparams(trainer: "pl.Trainer") -> None:
111111

112112

113113
class _ListMap(list[_T]):
114-
"""A hybrid container for loggers allowing both index and name access."""
114+
"""A hybrid container allowing both index and name access.
115115
116-
def __init__(self, loggers: Union[Iterable[_T], Mapping[str, _T]] = None):
117-
if isinstance(loggers, Mapping):
116+
This class extends the built-in list to provide dictionary-like access to its elements
117+
using string keys. It maintains an internal mapping of string keys to list indices,
118+
allowing users to retrieve, set, and delete elements by their associated names.
119+
120+
Args:
121+
__iterable (Union[Iterable[_T], Mapping[str, _T]], optional): An iterable of objects or a mapping
122+
of string keys to __iterable to initialize the container.
123+
124+
Raises:
125+
TypeError: If a Mapping is provided and any of its keys are not of type str.
126+
127+
Example:
128+
>>> listmap = _ListMap({'obj1': obj1, 'obj2': obj2})
129+
>>> listmap['obj1'] # Access by name
130+
obj1
131+
>>> listmap[0] # Access by index
132+
obj1
133+
>>> listmap['obj2'] = obj3 # Set by name
134+
>>> listmap[1] # Now returns obj3
135+
obj3
136+
>>> listmap.append(obj4) # Append by index
137+
>>> listmap[2]
138+
obj4
139+
140+
"""
141+
142+
def __init__(self, __iterable: Union[Iterable[_T], Mapping[str, _T]] = None):
143+
if isinstance(__iterable, Mapping):
118144
# super inits list with values
119-
if any(not isinstance(x, str) for x in loggers):
145+
if any(not isinstance(x, str) for x in __iterable):
120146
raise TypeError("When providing a Mapping, all keys must be of type str.")
121-
super().__init__(loggers.values())
122-
self._dict = dict(zip(loggers.keys(), range(len(loggers))))
147+
super().__init__(__iterable.values())
148+
self._dict = dict(zip(__iterable.keys(), range(len(__iterable))))
123149
else:
124150
default_dict = {}
125-
if isinstance(loggers, _ListMap):
126-
default_dict = loggers._dict.copy()
127-
super().__init__(() if loggers is None else loggers)
151+
if isinstance(__iterable, _ListMap):
152+
default_dict = __iterable._dict.copy()
153+
super().__init__(() if __iterable is None else __iterable)
128154
self._dict: dict = default_dict
129155

130156
def __eq__(self, other: Any) -> bool:
@@ -274,9 +300,6 @@ def __repr__(self) -> str:
274300
ret = super().__repr__()
275301
return f"_ListMap({ret}, keys={list(self._dict.keys())})"
276302

277-
def __reversed__(self) -> Iterable[_T]:
278-
return reversed(list(self))
279-
280303
def reverse(self) -> None:
281304
for key, idx in self._dict.items():
282305
self._dict[key] = len(self) - 1 - idx

0 commit comments

Comments
 (0)