class MetaContainer(object): """ Class to control storing of metainformation Parameters ----- path: string Absolute path to output .h5 file summary_data: dictionary-like object Additional information that should be saved. For example dalek_version, tardis_version, time, ranks, iterations """ def __init__(self, path, summary_data=None): self._path, self._file = os.path.split(path) self.open = False if summary_data is not None: self._write_summary(summary_data) def _write_summary(self, data): ''' Write summary_data to .h5 file. ''' # TODO: proper dictionary to pd.Series parsing with self as store: if 'overview' not in store.keys(): store.put('overview', pd.Series(data.values(), index=data.keys())) def __enter__(self): ''' Wrapper function to allow with MetaContainer as store: <code> ''' if not self.open: self.open = True self.store = SafeHDFStore(os.path.join(self._path, self._file)) return self.store def __exit__(self, type, value, traceback): ''' close store after 'with' ''' if self.open: self.store.__exit__(type, value, traceback) self.open = False return