Skip to content

Commit 41deff1

Browse files
Make Dataset a Context Manager. (#68)
Dataset can now be used in a with statement.
1 parent 404aaf3 commit 41deff1

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

ismrmrd/hdf5.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ def __del__(self):
158158
self.close()
159159
except:
160160
pass
161-
161+
162+
def __enter__(self):
163+
return self
164+
165+
def __exit__(self, exc_type, exc_val, exc_tb):
166+
self.close()
167+
162168
@property
163169
def _dataset(self):
164170
if self._dataset_name not in self._file:
@@ -167,7 +173,7 @@ def _dataset(self):
167173

168174
def list(self):
169175
return self._dataset.keys()
170-
176+
171177
def close(self):
172178
self._file.close()
173179

0 commit comments

Comments
 (0)