overwriting datasets makes sense in special cases

but prevent user from accidently doing that and store the information
This commit is contained in:
Martin Diehl 2020-05-25 19:12:31 +02:00
parent b6812d58b0
commit b9e16ea951
1 changed files with 19 additions and 2 deletions

View File

@ -89,6 +89,8 @@ class Result:
self.fname = os.path.abspath(fname)
self._allow_overwrite = False
def __repr__(self):
"""Show selected data."""
@ -163,6 +165,16 @@ class Result:
self.selection[what] = diff_sorted
def enable_overwrite(self):
print(util.bcolors().WARNING,util.bcolors().BOLD,
'Warning: Enabled overwrite of existing datasets!',
util.bcolors().ENDC)
self._allow_overwrite = True
def disable_overwrite(self):
self._allow_overwrite = False
def incs_in_range(self,start,end):
selected = []
for i,inc in enumerate([int(i[3:]) for i in self.increments]):
@ -1015,7 +1027,12 @@ class Result:
continue
lock.acquire()
with h5py.File(self.fname, 'a') as f:
try: # ToDo: Replace if exists?
try:
if self._allow_overwrite and result[0]+'/'+result[1]['label'] in f:
dataset = f[result[0]+'/'+result[1]['label']]
datset = result[1]['data']
dataset.attrs['Overwritten'] = True
else:
dataset = f[result[0]].create_dataset(result[1]['label'],data=result[1]['data'])
now = datetime.datetime.now().astimezone()
dataset.attrs['Created'] = now.strftime('%Y-%m-%d %H:%M:%S%z').encode()