Skip to content

Commit c06dd0d

Browse files
committed
use __destructor rather than close_files()
remove redundant setup.cfg
1 parent 3ceac50 commit c06dd0d

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

dl1_data_handler/reader.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
]
1717

1818
from abc import abstractmethod
19+
import atexit
1920
from collections import OrderedDict
2021
from enum import Enum
2122
import numpy as np
@@ -229,6 +230,8 @@ def __init__(
229230

230231
super().__init__(config=config, parent=parent, **kwargs)
231232

233+
# Register the destructor to close all open files properly
234+
atexit.register(self.__destructor)
232235
# Initialize the Table data quality query
233236
self.quality_query = TableQualityQuery(parent=self)
234237

@@ -936,10 +939,13 @@ def generate_stereo_batch(self, batch_indices) -> Table:
936939
batch.sort(["obs_id", "event_id", "tel_type_id", "tel_id"])
937940
return batch
938941

939-
def close_files(self):
940-
"""Close all open HDF5 files."""
941-
for file in self.files.values():
942-
file.close()
942+
def __destructor(self):
943+
"""Destructor to ensure all opened HDF5 files are properly closed."""
944+
if hasattr(self, "files"): # Ensure self.files exists before attempting to close
945+
for file_name in list(self.files.keys()):
946+
if self.files[file_name].isopen: # Check if file is still open
947+
self.files[file_name].close()
948+
943949

944950
@abstractmethod
945951
def _append_features(self, batch) -> Table:

dl1_data_handler/tests/test_reader.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ def test_dl1_image_reading(dl1_tmp_path, dl1_gamma_file):
2121
mono_batch = dl1_reader.generate_mono_batch([0])
2222
assert mono_batch["tel_id"] == 4
2323
assert mono_batch["features"].shape == (1, 110, 110, 2)
24-
# Close the files
25-
dl1_reader.close_files()
2624

2725
def test_r1_waveform_reading(r1_tmp_path, r1_gamma_file):
2826
"""check reading from pixel-wise waveform data files"""
@@ -42,6 +40,4 @@ def test_r1_waveform_reading(r1_tmp_path, r1_gamma_file):
4240
# Test the generation of a mono batch
4341
mono_batch = r1_reader.generate_mono_batch([0])
4442
assert mono_batch["tel_id"] == 4
45-
assert mono_batch["features"].shape == (1, 110, 110, 20)
46-
# Close the files
47-
r1_reader.close_files()
43+
assert mono_batch["features"].shape == (1, 110, 110, 20)

setup.cfg

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)