Skip to content
Snippets Groups Projects
Commit bbeee552 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Add __iter__ method to HDF5File class

parent 5b63c2d3
No related branches found
No related tags found
1 merge request!17Add `__iter__` method to the HDF5File class
......@@ -39,6 +39,9 @@ class HDF5File(HDF5File_C):
"""
return self.has_key(x)
def __iter__(self):
return iter(self.keys())
def _is_string(s):
"""Returns ``True`` if the given object is a string
......
......@@ -480,6 +480,7 @@ def test_copy_constructor():
assert "/Test/Data" in shallow
assert hdf5.filename == shallow.filename
assert hdf5.keys() == shallow.keys()
assert [key for key in hdf5] == shallow.keys()
assert hdf5.cwd == shallow.cwd
assert not deep.has_group("/Test")
......
......@@ -257,6 +257,27 @@ the variable `my_array` as an arrayset using
:py:meth:`bob.io.base.HDF5File.read`. In this case, each position readout
would return a 1D uint8 array instead of a 2D array.
Pythonic operations on HDF5 files
---------------------------------
You can use some Pythonic opertations on :py:class:`bob.io.base.HDF5File`.
You can iterate over :py:class:`bob.io.base.HDF5File` objects to get an
iterable of keys instead of calling
:py:meth:`bob.io.base.HDF5File.keys`. You can also use the ``in`` keyword
instead of calling :py:meth:`bob.io.base.HDF5File.has_key`. For example:
.. doctest::
>>> keys = f.keys() # Get a list of keys
>>> keys == [key for key in f] # instead you can also iterate over keys
True
>>> f.has_key('arrayset')
True
>>> 'arrayset' in f # you can use the `in` operator instead of `has_key`
True
Array interfaces
----------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment