1313# limitations under the License.
1414"""Utilities for loggers."""
1515
16- from collections .abc import ItemsView , KeysView , Mapping , ValuesView
16+ from collections .abc import ItemsView , Iterable , KeysView , Mapping , ValuesView
1717from pathlib import Path
18- from typing import Any , Optional , TypeVar , Union
18+ from typing import Any , Optional , SupportsIndex , TypeVar , Union
1919
2020from torch import Tensor
2121from typing_extensions import Self
@@ -110,7 +110,7 @@ def _log_hyperparams(trainer: "pl.Trainer") -> None:
110110class _ListMap (list [_T ]):
111111 """A hybrid container for loggers allowing both index and name access."""
112112
113- def __init__ (self , loggers : Union [list [_T ], Mapping [str , _T ]] = None ):
113+ def __init__ (self , loggers : Union [Iterable [_T ], Mapping [str , _T ]] = None ):
114114 if isinstance (loggers , Mapping ):
115115 # super inits list with values
116116 if any (not isinstance (x , str ) for x in loggers ):
@@ -145,7 +145,7 @@ def __iadd__(self, other: Union[list[_T], Self]) -> Self:
145145 # todo
146146 return list .__iadd__ (self , other )
147147
148- def __setitem__ (self , key : Union [int , slice , str ], value : _T ) -> None :
148+ def __setitem__ (self , key : Union [SupportsIndex , slice , str ], value : _T ) -> None :
149149 if isinstance (key , (int , slice )):
150150 # replace element by index
151151 return list .__setitem__ (self , key , value )
@@ -192,7 +192,7 @@ def items(self) -> ItemsView[str, _T]:
192192 return d .items ()
193193
194194 # --- List and Dict interface ---
195- def pop (self , key : Union [int , str ] = - 1 , default : Optional [Any ] = None ) -> _T :
195+ def pop (self , key : Union [SupportsIndex , str ] = - 1 , default : Optional [Any ] = None ) -> _T :
196196 if isinstance (key , int ):
197197 ret = list .pop (self , key )
198198 for str_key , idx in list (self ._dict .items ()):
@@ -210,3 +210,15 @@ def pop(self, key: Union[int, str] = -1, default: Optional[Any] = None) -> _T:
210210 def __repr__ (self ) -> str :
211211 ret = super ().__repr__ ()
212212 return f"_ListMap({ ret } , keys={ list (self ._dict .keys ())} )"
213+
214+ def __reversed__ (self ) -> Iterable [_T ]:
215+ return reversed (list (self ))
216+
217+ def reverse (self ) -> None :
218+ for key , idx in self ._dict .items ():
219+ self ._dict [key ] = len (self ) - 1 - idx
220+ list .reverse (self )
221+
222+ def clear (self ):
223+ self ._dict .clear ()
224+ list .clear (self )
0 commit comments