Repository: glotzerlab/signac
Pull request #182 (2019-05-01 17:08:00)

Improve the representation of `H5Group`.
Implements `H5Group.__repr__`.

For example:

```python\
>>> H5Store('data.h5')['foo']
H5Group(store=H5Store(filename='data.h5'), path='/foo')
```

Improves the REPL experience.

codecov[bot]: # [Codecov](https://codecov.io/gh/glotzerlab/signac/pull/182?src=pr&el=h1) Report
> Merging [#182](https://codecov.io/gh/glotzerlab/signac/pull/182?src=pr&el=desc) into [master](https://codecov.io/gh/glotzerlab/signac/commit/b35cc5ca616b851af6872ced5a5d68f9a20b5e33?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `50%`.

[![Impacted file tree graph](https://codecov.io/gh/glotzerlab/signac/pull/182/graphs/tree.svg?width=650&token=FghbjTyzgM&height=150&src=pr)](https://codecov.io/gh/glotzerlab/signac/pull/182?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #182      +/-   ##
==========================================
- Coverage   64.99%   64.99%   -0.01%     
==========================================
  Files          37       37              
  Lines        5528     5530       +2     
==========================================
+ Hits         3593     3594       +1     
- Misses       1935     1936       +1
```


| [Impacted Files](https://codecov.io/gh/glotzerlab/signac/pull/182?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [signac/core/h5store.py](https://codecov.io/gh/glotzerlab/signac/pull/182/diff?src=pr&el=tree#diff-c2lnbmFjL2NvcmUvaDVzdG9yZS5weQ==) | `91.03% <50%> (-0.3%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/glotzerlab/signac/pull/182?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/glotzerlab/signac/pull/182?src=pr&el=footer). Last update [b35cc5c...2e13564](https://codecov.io/gh/glotzerlab/signac/pull/182?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/changelog.txt b/changelog.txt
index 9547534f..c7caa002 100644
--- a/changelog.txt
+++ b/changelog.txt
@@ -22,6 +22,7 @@ next
  - Add command line options ``--sp`` and ``--doc`` for ``signac find`` that allow users to display key-value pairs of the state point and document in combination with the job id (#97, #146).
  - Fix: Searches for whole numbers will match all numerically matching integers regardless of whether they are stored as decimals or whole numbers (#169).
  - Fix: Passing an instance of dict to `H5Store.setdefault()` will return an instance of `H5Group` instead of a dict (#180).
+ - Improve the representation (return value of `repr()`) of instances of `H5Group`.
 
 
 [1.0.0] -- 2019-02-28
diff --git a/signac/core/h5store.py b/signac/core/h5store.py
index 473daa99..51992261 100644
--- a/signac/core/h5store.py
+++ b/signac/core/h5store.py
@@ -21,6 +21,12 @@ else:
     from collections.abc import MutableMapping
 
 
+__all__ = [
+    'H5Store', 'H5Group', 'H5StoreManager',
+    'H5StoreClosedError', 'H5StoreAlreadyOpenError',
+    ]
+
+
 def _group_is_pandas_type(group):
     return 'pandas_type' in group.attrs
 
@@ -180,6 +186,10 @@ class H5Group(MutableMapping):
         self._store = store
         self._path = path
 
+    def __repr__(self):
+        return '{}(store={}, path={})'.format(
+            type(self).__name__, repr(self._store), repr(self._path))
+
     @property
     def _group(self):
         return self._store.file[self._path]


Test patch:
diff --git a/tests/test_h5store.py b/tests/test_h5store.py
index c51b5c71..fd18c79f 100644
--- a/tests/test_h5store.py
+++ b/tests/test_h5store.py
@@ -200,9 +200,9 @@ class H5StoreTest(BaseH5StoreTest):
     def test_repr(self):
         with self.open_h5store() as h5s:
             key = 'test_repr'
+            self.assertEqual(repr(h5s), repr(eval(repr(h5s))))
             h5s[key] = self.get_testdata()
-            repr(h5s)    # open
-        repr(h5s)   # closed
+        self.assertEqual(repr(h5s), repr(eval(repr(h5s))))
 
     def test_str(self):
         with self.open_h5store() as h5s:
@@ -537,6 +537,15 @@ class H5StoreNestedDataTest(H5StoreTest):
     def get_testdata(self, size=None):
         return dict(a=super(H5StoreNestedDataTest, self).get_testdata(size))
 
+    def test_repr(self):
+        from signac.core.h5store import H5Store, H5Group  # noqa:F401
+        with self.open_h5store() as h5s:
+            key = 'test_repr'
+            self.assertEqual(repr(h5s), repr(eval(repr(h5s))))
+            h5s[key] = self.get_testdata()
+            self.assertEqual(repr(h5s[key]), repr(eval(repr(h5s[key]))))
+        self.assertEqual(repr(h5s), repr(eval(repr(h5s))))
+
 
 class H5StoreBytesDataTest(H5StoreTest):