Commit bbeee552 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Add __iter__ method to HDF5File class

parent 5b63c2d3
...@@ -39,6 +39,9 @@ class HDF5File(HDF5File_C): ...@@ -39,6 +39,9 @@ class HDF5File(HDF5File_C):
""" """
return self.has_key(x) return self.has_key(x)
def __iter__(self):
return iter(self.keys())
def _is_string(s): def _is_string(s):
"""Returns ``True`` if the given object is a string """Returns ``True`` if the given object is a string
......
...@@ -480,6 +480,7 @@ def test_copy_constructor(): ...@@ -480,6 +480,7 @@ def test_copy_constructor():
assert "/Test/Data" in shallow assert "/Test/Data" in shallow
assert hdf5.filename == shallow.filename assert hdf5.filename == shallow.filename
assert hdf5.keys() == shallow.keys() assert hdf5.keys() == shallow.keys()
assert [key for key in hdf5] == shallow.keys()
assert hdf5.cwd == shallow.cwd assert hdf5.cwd == shallow.cwd
assert not deep.has_group("/Test") assert not deep.has_group("/Test")
......
...@@ -257,6 +257,27 @@ the variable `my_array` as an arrayset using ...@@ -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 :py:meth:`bob.io.base.HDF5File.read`. In this case, each position readout
would return a 1D uint8 array instead of a 2D array. 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 Array interfaces
---------------- ----------------
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment