Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use builtin KeysView, ValueView, ItemsView #257

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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