Use HDF5File with the "with" statement
Created by: 183amir
In python you usually can do something like:
with open(path) as f:
doc = f.read()
But you cannot do this with the HDF5File
class.
What is the proper way to add this feature to bob?
Activity
-
Newest first Oldest first
-
Show all activity Show comments only Show history only
- Author Owner
Created by: tiagofrepereira2012
In the C++ level you can define in the PyMethodDef two methods called
__enter__
and__exit__
(the python name) and you implement the behaviour that you want. - Author Owner
Created by: 183amir
It can also be done in
Python
:diff --git a/bob/io/base/__init__.py b/bob/io/base/__init__.py index 74a868d..a21a357 100644 --- a/bob/io/base/__init__.py +++ b/bob/io/base/__init__.py @@ -5,13 +5,34 @@ import bob.core import bob.extension bob.extension.load_bob_library('bob.io.base', __file__) -from ._library import File, HDF5File, extensions +from ._library import File as File_C, HDF5File as HDF5File_C, extensions from . import version from .version import module as __version__ from .version import api as __api_version__ import os + +class File(File_C): + __doc__ = File_C.__doc__ + + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() + + +class HDF5File(HDF5File_C): + __doc__ = HDF5File_C.__doc__ + + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() + + def _is_string(s): """Returns ``True`` if the given object is a string
- Author Owner
Created by: tiagofrepereira2012
Just for completeness, the effort in the C++ level (with no documentation :-P )
diff --git a/bob/io/base/hdf5.cpp b/bob/io/base/hdf5.cpp index d909e54..743c589 100644 --- a/bob/io/base/hdf5.cpp +++ b/bob/io/base/hdf5.cpp @@ -217,6 +217,51 @@ BOB_CATCH_MEMBER(exception_message(self, s_close.name()).c_str(), 0); } + + + +static auto s_enter = bob::extension::FunctionDoc( + "__enter__", + "ENTER", + "ENTER", + true +) +.add_prototype("") +; +static PyObject* PyBobIoHDF5File_enter(PyBobIoHDF5FileObject* self, PyObject *args, PyObject* kwds) { +BOB_TRY + /* Parses input arguments in a single shot */ + return Py_BuildValue("N",self); + + +BOB_CATCH_MEMBER(exception_message(self, s_enter.name()).c_str(), 0); +} + + + +static auto s_exit = bob::extension::FunctionDoc( + "__exit__", + "EXIT", + "EXIT", + true +) +.add_prototype("") +; +static PyObject* PyBobIoHDF5File_exit(PyBobIoHDF5FileObject* self, PyObject *args, PyObject* kwds) { +BOB_TRY + /* Parses input arguments in a single shot */ + + self->f->close(); + + + Py_RETURN_NONE; +BOB_CATCH_MEMBER(exception_message(self, s_exit.name()).c_str(), 0); +} + + + + + static auto s_cd = bob::extension::FunctionDoc( "cd", "Changes the current prefix path", @@ -1886,6 +1931,22 @@ static PyMethodDef PyBobIoHDF5File_methods[] = { METH_VARARGS|METH_KEYWORDS, s_close.doc() }, + + { + s_enter.name(), + (PyCFunction)PyBobIoHDF5File_enter, + METH_NOARGS, + s_enter.doc() + }, + + { + s_exit.name(), + (PyCFunction)PyBobIoHDF5File_exit, + METH_VARARGS, + s_exit.doc() + }, + + { s_flush.name(), (PyCFunction)PyBobIoHDF5File_flush,
- Author Owner
Created by: 183amir
@tiagofrepereira2012 when you have the time, could please push your C patch?
- Author Owner
Created by: tiagofrepereira2012
Sure
- Amir MOHAMMADI Reassigned to @amohammadi
Reassigned to @amohammadi
- Owner
Fixed in 77c107fd
- Amir MOHAMMADI Status changed to closed
Status changed to closed
Please register or sign in to reply