Skip to content

Commit

Permalink
Merge pull request #30 from nlesc-nano/eq
Browse files Browse the repository at this point in the history
ENH: Reimplement `RecursiveValuesView.__eq__` in terms of `collections.Counter` comparisons.
  • Loading branch information
BvB93 authored Oct 4, 2021
2 parents 6bf3e3f + 095d01f commit 4a27f8b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ All notable changes to this project will be documented in this file.
This project adheres to `Semantic Versioning <http://semver.org/>`_.


2.1.0
*****
* Reimplement ``RecursiveValuesView.__eq__`` in terms of ``collections.Counter`` comparisons.


2.0.0
*****
* Added the ``fullmatch`` argument to ``VersionInfo.from_str``.
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


################
Nano-Utils 2.0.0
Nano-Utils 2.1.0
################
Utility functions used throughout the various nlesc-nano repositories.

Expand Down
2 changes: 1 addition & 1 deletion nanoutils/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""The **Nano-Utils** version."""

__version__ = '2.0.0'
__version__ = '2.1.0'
5 changes: 3 additions & 2 deletions nanoutils/hdf5_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import sys
import abc
from collections import Counter
from collections.abc import Generator, MappingView, Iterator
from typing import NoReturn

Expand Down Expand Up @@ -119,7 +120,7 @@ def __reversed__(self) -> NoReturn:
This feature requires h5py >= 3.5.
"""
raise TypeError("`reversed` requires h5py >= 3.5.0")
raise TypeError("`reversed` requires h5py >= 3.5.0") from H5PY_EX


class RecursiveKeysView(_RecursiveMappingView, KeysView[str]):
Expand Down Expand Up @@ -255,7 +256,7 @@ def __eq__(self, other: object) -> bool:
cls = type(self)
if not isinstance(other, cls):
return NotImplemented
return self.mapping is other.mapping
return Counter(self) == Counter(other)

def __iter__(self) -> Generator[h5py.Dataset, None, None]:
"""Implement :func:`iter(self)<iter>`."""
Expand Down
21 changes: 18 additions & 3 deletions tests/test_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ def test_superclass(self, view: RecursiveKeysView) -> None:

def test_mapping(self, view: RecursiveKeysView) -> None:
assertion.isinstance(view.mapping, h5py.Group)
assertion.eq(view, RecursiveKeysView(view.mapping))

def test_eq(self, view: RecursiveKeysView) -> None:
with h5py.File(view.mapping.filename) as f:
view2 = RecursiveKeysView(f)
assertion.eq(view, view2)
assertion.ne(view, None)


@pytest.mark.skipif(H5PY_EX is not None, reason=str(H5PY_EX))
Expand Down Expand Up @@ -187,7 +192,12 @@ def test_superclass(self, view: RecursiveValuesView) -> None:

def test_mapping(self, view: RecursiveValuesView) -> None:
assertion.isinstance(view.mapping, h5py.Group)
assertion.eq(view, RecursiveValuesView(view.mapping))

def test_eq(self, view: RecursiveValuesView) -> None:
with h5py.File(view.mapping.filename) as f:
view2 = RecursiveValuesView(f)
assertion.eq(view, view2)
assertion.ne(view, None)


@pytest.mark.skipif(H5PY_EX is not None, reason=str(H5PY_EX))
Expand Down Expand Up @@ -333,4 +343,9 @@ def test_superclass(self, view: RecursiveItemsView) -> None:

def test_mapping(self, view: RecursiveItemsView) -> None:
assertion.isinstance(view.mapping, h5py.Group)
assertion.eq(view, RecursiveItemsView(view.mapping))

def test_eq(self, view: RecursiveItemsView) -> None:
with h5py.File(view.mapping.filename) as f:
view2 = RecursiveItemsView(f)
assertion.eq(view, view2)
assertion.ne(view, None)

0 comments on commit 4a27f8b

Please sign in to comment.