|
20 | 20 |
|
21 | 21 | import sys
|
22 | 22 | import abc
|
| 23 | +import io |
23 | 24 | from collections import Counter
|
24 | 25 | from collections.abc import Generator, MappingView, Iterator
|
25 | 26 | from typing import NoReturn
|
@@ -84,6 +85,28 @@ def _iter_dfs(
|
84 | 85 | else:
|
85 | 86 | yield v.name, v
|
86 | 87 |
|
| 88 | + def __repr__(self) -> str: |
| 89 | + """Implement :func:`repr(self) <repr>`.""" |
| 90 | + cls = type(self) |
| 91 | + indent = " " * (3 + len(cls.__name__)) |
| 92 | + iterator = iter(self) |
| 93 | + |
| 94 | + stream = io.StringIO() |
| 95 | + stream.write(f"<{cls.__name__} [") |
| 96 | + |
| 97 | + # Print size-1 view on a single line by special casing the first element |
| 98 | + try: |
| 99 | + item = next(iterator) |
| 100 | + except StopIteration: |
| 101 | + pass |
| 102 | + else: |
| 103 | + stream.write(repr(item)) |
| 104 | + for item in iterator: |
| 105 | + stream.write(f",\n{indent}{item!r}") |
| 106 | + |
| 107 | + stream.write("]>") |
| 108 | + return stream.getvalue() |
| 109 | + |
87 | 110 | def __len__(self) -> int:
|
88 | 111 | """Implement :func:`len(self)<len>`."""
|
89 | 112 | i = 0
|
@@ -150,14 +173,11 @@ class RecursiveKeysView(_RecursiveMappingView, KeysView[str]):
|
150 | 173 | ... dset1 = f.create_dataset('dset1', (10,), dtype=float)
|
151 | 174 | ... dset2 = f['a'].create_dataset('dset2', (10,), dtype=float)
|
152 | 175 | ... dset3 = f['a']['b'].create_dataset('dset3', (10,), dtype=float)
|
153 |
| -
|
154 |
| - >>> with h5py.File(filename, 'r') as f: |
155 |
| - ... for key in RecursiveKeysView(f): |
156 |
| - ... print(repr(key)) |
157 |
| - '/a/b/dset3' |
158 |
| - '/a/dset2' |
159 |
| - '/dset1' |
160 |
| -
|
| 176 | + ... |
| 177 | + ... print(RecursiveKeysView(f)) |
| 178 | + <RecursiveKeysView ['/a/b/dset3', |
| 179 | + '/a/dset2', |
| 180 | + '/dset1']> |
161 | 181 |
|
162 | 182 | .. testcleanup:: python
|
163 | 183 |
|
@@ -223,14 +243,11 @@ class RecursiveValuesView(_RecursiveMappingView, ValuesView[H5PyDataset]):
|
223 | 243 | ... dset1 = f.create_dataset('dset1', (10,), dtype=float)
|
224 | 244 | ... dset2 = f['a'].create_dataset('dset2', (10,), dtype=float)
|
225 | 245 | ... dset3 = f['a']['b'].create_dataset('dset3', (10,), dtype=float)
|
226 |
| -
|
227 |
| - >>> with h5py.File(filename, 'r') as f: |
228 |
| - ... for value in RecursiveValuesView(f): |
229 |
| - ... print(value) |
230 |
| - <HDF5 dataset "dset3": shape (10,), type "<f8"> |
231 |
| - <HDF5 dataset "dset2": shape (10,), type "<f8"> |
232 |
| - <HDF5 dataset "dset1": shape (10,), type "<f8"> |
233 |
| -
|
| 246 | + ... |
| 247 | + ... print(RecursiveValuesView(f)) |
| 248 | + <RecursiveValuesView [<HDF5 dataset "dset3": shape (10,), type "<f8">, |
| 249 | + <HDF5 dataset "dset2": shape (10,), type "<f8">, |
| 250 | + <HDF5 dataset "dset1": shape (10,), type "<f8">]> |
234 | 251 |
|
235 | 252 | .. testcleanup:: python
|
236 | 253 |
|
@@ -303,14 +320,11 @@ class RecursiveItemsView(_RecursiveMappingView, ItemsView[str, H5PyDataset]):
|
303 | 320 | ... dset1 = f.create_dataset('dset1', (10,), dtype=float)
|
304 | 321 | ... dset2 = f['a'].create_dataset('dset2', (10,), dtype=float)
|
305 | 322 | ... dset3 = f['a']['b'].create_dataset('dset3', (10,), dtype=float)
|
306 |
| -
|
307 |
| - >>> with h5py.File(filename, 'r') as f: |
308 |
| - ... for items in RecursiveItemsView(f): |
309 |
| - ... print(items) |
310 |
| - ('/a/b/dset3', <HDF5 dataset "dset3": shape (10,), type "<f8">) |
311 |
| - ('/a/dset2', <HDF5 dataset "dset2": shape (10,), type "<f8">) |
312 |
| - ('/dset1', <HDF5 dataset "dset1": shape (10,), type "<f8">) |
313 |
| -
|
| 323 | + ... |
| 324 | + ... print(RecursiveItemsView(f)) |
| 325 | + <RecursiveItemsView [('/a/b/dset3', <HDF5 dataset "dset3": shape (10,), type "<f8">), |
| 326 | + ('/a/dset2', <HDF5 dataset "dset2": shape (10,), type "<f8">), |
| 327 | + ('/dset1', <HDF5 dataset "dset1": shape (10,), type "<f8">)]> |
314 | 328 |
|
315 | 329 | .. testcleanup:: python
|
316 | 330 |
|
|
0 commit comments