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

allow np int in get item #148

Merged
merged 1 commit into from
Nov 4, 2024
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
11 changes: 11 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,14 @@ def test_index_error(tmp_path):
io[1]
with pytest.raises(IndexError):
io[-2]


def test_np_int_getitem(tmp_path):
io = znh5md.IO(tmp_path / "test.h5")
io.append(ase.build.molecule("H2O"))
assert io[np.int64(0)] is not None
assert io[np.int64(-1)] is not None
with pytest.raises(IndexError):
io[np.int64(1)]
with pytest.raises(IndexError):
io[np.int64(-2)]
3 changes: 2 additions & 1 deletion znh5md/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ def __len__(self) -> int:
return len(f["particles"][self.particle_group]["species"]["value"])

def __getitem__(
self, index: Union[int, slice]
self, index: Union[int, np.int_, slice]
) -> Union[ase.Atoms, List[ase.Atoms]]:
index = int(index) if isinstance(index, np.int_) else index
single_item = isinstance(index, int)
index = [index] if single_item else index

Expand Down
Loading