diff --git a/src/scippnexus/base.py b/src/scippnexus/base.py index ae7142f7..930b338c 100644 --- a/src/scippnexus/base.py +++ b/src/scippnexus/base.py @@ -5,7 +5,7 @@ import inspect import warnings -from collections.abc import Iterator, Mapping +from collections.abc import ItemsView, Iterator, KeysView, Mapping, ValuesView from functools import lru_cache from pathlib import PurePosixPath from types import MappingProxyType @@ -287,6 +287,18 @@ def __len__(self) -> int: def __iter__(self) -> Iterator[str]: return self._children.__iter__() + def keys(self) -> KeysView[str]: + """Return a view of the keys of the group's elements.""" + return self._children.keys() + + def values(self) -> ValuesView[Field | Group]: + """Return a view of the group's elements.""" + return self._children.values() + + def items(self) -> ItemsView[str, Field | Group]: + """Return a view of pairs of the keys and the group's elements.""" + return self._children.items() + def _get_children_by_nx_class( self, select: type | list[type] ) -> dict[str, NXobject | Field]: diff --git a/src/scippnexus/typing.py b/src/scippnexus/typing.py index 75286cab..a8528834 100644 --- a/src/scippnexus/typing.py +++ b/src/scippnexus/typing.py @@ -3,7 +3,7 @@ # @author Simon Heybrock from __future__ import annotations -from collections.abc import Callable, Mapping +from collections.abc import Callable, KeysView, Mapping from typing import TYPE_CHECKING, Any, Protocol @@ -47,7 +47,7 @@ class H5Group(H5Base, Protocol): def __getitem__(self, index: str | Any) -> H5Dataset | H5Group: """Keys in the group""" - def keys(self) -> list[str]: + def keys(self) -> KeysView[str]: """Keys in the group""" def create_dataset(self) -> H5Dataset: