Skip to content

Commit

Permalink
Make Dataset a Context Manager. (#68)
Browse files Browse the repository at this point in the history
Dataset can now be used in a with statement.
  • Loading branch information
fzimmermann89 authored Oct 6, 2023
1 parent 404aaf3 commit 41deff1
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ismrmrd/hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ def __del__(self):
self.close()
except:
pass


def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

@property
def _dataset(self):
if self._dataset_name not in self._file:
Expand All @@ -167,7 +173,7 @@ def _dataset(self):

def list(self):
return self._dataset.keys()

def close(self):
self._file.close()

Expand Down

0 comments on commit 41deff1

Please sign in to comment.