Renamed open_file() --> load_file_obj() and close_file() --> unload_file_obj() to focus more on the management operations on the files that actual file handling operations.
This commit is contained in:
@ -44,11 +44,11 @@ class HDF5DataOpsManager():
|
|||||||
|
|
||||||
# Define public methods
|
# Define public methods
|
||||||
|
|
||||||
def open_file(self):
|
def load_file_obj(self):
|
||||||
if self.file_obj is None:
|
if self.file_obj is None:
|
||||||
self.file_obj = h5py.File(self.file_path, self.mode)
|
self.file_obj = h5py.File(self.file_path, self.mode)
|
||||||
|
|
||||||
def close_file(self):
|
def unload_file_obj(self):
|
||||||
if self.file_obj:
|
if self.file_obj:
|
||||||
self.file_obj.flush() # Ensure all data is written to disk
|
self.file_obj.flush() # Ensure all data is written to disk
|
||||||
self.file_obj.close()
|
self.file_obj.close()
|
||||||
@ -76,7 +76,7 @@ class HDF5DataOpsManager():
|
|||||||
returns a copy of the dataset content in the form of dataframe when possible or numpy array
|
returns a copy of the dataset content in the form of dataframe when possible or numpy array
|
||||||
"""
|
"""
|
||||||
if self.file_obj is None:
|
if self.file_obj is None:
|
||||||
self.open_file()
|
self.load_file_obj()
|
||||||
|
|
||||||
dataset_obj = self.file_obj[dataset_name]
|
dataset_obj = self.file_obj[dataset_name]
|
||||||
# Read dataset content from dataset obj
|
# Read dataset content from dataset obj
|
||||||
@ -148,7 +148,7 @@ class HDF5DataOpsManager():
|
|||||||
obj.attrs.update(annotation_dict_copy)
|
obj.attrs.update(annotation_dict_copy)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.close_file()
|
self.unload_file_obj()
|
||||||
print(f"An unexpected error occurred: {e}. The file object has been properly closed.")
|
print(f"An unexpected error occurred: {e}. The file object has been properly closed.")
|
||||||
|
|
||||||
|
|
||||||
@ -204,7 +204,7 @@ class HDF5DataOpsManager():
|
|||||||
obj.attrs.update(update_dict)
|
obj.attrs.update(update_dict)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.close_file()
|
self.unload_file_obj()
|
||||||
print(f"An unexpected error occurred: {e}. The file object has been properly closed.")
|
print(f"An unexpected error occurred: {e}. The file object has been properly closed.")
|
||||||
|
|
||||||
def delete_metadata(self, obj_name, annotation_dict):
|
def delete_metadata(self, obj_name, annotation_dict):
|
||||||
@ -247,7 +247,7 @@ class HDF5DataOpsManager():
|
|||||||
print(msg)
|
print(msg)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.close_file()
|
self.unload_file_obj()
|
||||||
print(f"An unexpected error occurred: {e}. The file object has been properly closed.")
|
print(f"An unexpected error occurred: {e}. The file object has been properly closed.")
|
||||||
|
|
||||||
|
|
||||||
@ -293,13 +293,13 @@ class HDF5DataOpsManager():
|
|||||||
msg = f"Skipping: Attribute '{old_attr}' does not exist."
|
msg = f"Skipping: Attribute '{old_attr}' does not exist."
|
||||||
print(msg) # Optionally, replace with warnings.warn(msg)
|
print(msg) # Optionally, replace with warnings.warn(msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.close_file()
|
self.unload_file_obj()
|
||||||
print(
|
print(
|
||||||
f"An unexpected error occurred: {e}. The file object has been properly closed. "
|
f"An unexpected error occurred: {e}. The file object has been properly closed. "
|
||||||
"Please ensure that 'obj_name' exists in the file, and that the keys in 'renaming_map' are valid attributes of the object."
|
"Please ensure that 'obj_name' exists in the file, and that the keys in 'renaming_map' are valid attributes of the object."
|
||||||
)
|
)
|
||||||
|
|
||||||
self.close_file()
|
self.unload_file_obj()
|
||||||
|
|
||||||
def get_metadata(self, obj_path):
|
def get_metadata(self, obj_path):
|
||||||
""" Get file attributes from object at path = obj_path. For example,
|
""" Get file attributes from object at path = obj_path. For example,
|
||||||
@ -392,7 +392,7 @@ class HDF5DataOpsManager():
|
|||||||
|
|
||||||
# Close the file if it's already open
|
# Close the file if it's already open
|
||||||
if self.file_obj is not None:
|
if self.file_obj is not None:
|
||||||
self.close_file()
|
self.unload_file_obj()
|
||||||
|
|
||||||
# Attempt to open the file in 'r+' mode for appending
|
# Attempt to open the file in 'r+' mode for appending
|
||||||
try:
|
try:
|
||||||
|
Reference in New Issue
Block a user