Skip to content

Commit

Permalink
Merge pull request #257 from scipp/nice-keys-view
Browse files Browse the repository at this point in the history
Use builtin KeysView, ValueView, ItemsView
  • Loading branch information
jl-wynen authored Jan 16, 2025
2 parents c80fa82 + c698a04 commit b1ea73a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/scippnexus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
4 changes: 2 additions & 2 deletions src/scippnexus/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit b1ea73a

Please sign in to comment.