Skip to content

Commit 60ddafa

Browse files
authored
Open dagmc model file within a context manager (#3706)
1 parent 9c91bdd commit 60ddafa

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

openmc/dagmc.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -223,21 +223,19 @@ def auto_mat_ids(self, val):
223223

224224
@property
225225
def material_names(self):
226-
dagmc_file_contents = h5py.File(self.filename)
227-
material_tags_hex = dagmc_file_contents['/tstt/tags/NAME'].get(
228-
'values')
229226
material_tags_ascii = []
230-
for tag in material_tags_hex:
231-
candidate_tag = tag.tobytes().decode().replace('\x00', '')
232-
# tags might be for temperature or reflective surfaces
233-
if candidate_tag.startswith('mat:'):
234-
# if name ends with _comp remove it, it is not parsed
235-
if candidate_tag.endswith('_comp'):
236-
candidate_tag = candidate_tag[:-5]
237-
# removes first 4 characters as openmc.Material name should be
238-
# set without the 'mat:' part of the tag
239-
material_tags_ascii.append(candidate_tag[4:])
240-
227+
with h5py.File(self.filename) as dagmc_file_contents:
228+
material_tags_hex = dagmc_file_contents['/tstt/tags/NAME'].get('values')
229+
for tag in material_tags_hex:
230+
candidate_tag = tag.tobytes().decode().replace('\x00', '')
231+
# tags might be for temperature or reflective surfaces
232+
if candidate_tag.startswith('mat:'):
233+
# if name ends with _comp remove it, it is not parsed
234+
if candidate_tag.endswith('_comp'):
235+
candidate_tag = candidate_tag[:-5]
236+
# removes first 4 characters as openmc.Material name should be
237+
# set without the 'mat:' part of the tag
238+
material_tags_ascii.append(candidate_tag[4:])
241239
return sorted(set(material_tags_ascii))
242240

243241
def _n_geom_elements(self, geom_type):

0 commit comments

Comments
 (0)