Skip to content

Commit f185174

Browse files
authored
Merge pull request #225 from NREL/pp/outputs_fix
Fix attrs in Outputs getting overwritten
2 parents 2f1a224 + 96c9f40 commit f185174

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

rex/outputs.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,26 @@ def full_version_record(self):
196196
dict
197197
Dictionary of package versions for dependencies
198198
"""
199-
versions = {'rex': __version__,
199+
versions = {'python': sys.version,
200+
'rex': __version__,
201+
'h5py': h5py.__version__,
202+
'h5pyd': h5pyd.__version__,
200203
'pandas': pd.__version__,
201204
'numpy': np.__version__,
202-
'python': sys.version,
205+
'scipy': scipy.__version__,
203206
'click': click.__version__,
204-
'h5py': h5py.__version__,
205-
'h5pyd': h5pyd.__version__,
206-
'scipy': scipy.__version__
207207
}
208208
return versions
209209

210210
def set_version_attr(self):
211211
"""Set the version attribute to the h5 file."""
212-
self.h5.attrs['version'] = __version__
213-
self.h5.attrs['full_version_record'] = json.dumps(
214-
self.full_version_record)
215-
self.h5.attrs['package'] = 'rex'
212+
new_attrs = {'version': __version__,
213+
'full_version_record': json.dumps(
214+
self.full_version_record),
215+
'package': 'rex'}
216+
for name, value in new_attrs.items():
217+
if name not in self.h5.attrs:
218+
self.h5.attrs[name] = value
216219

217220
@property
218221
def version(self):
@@ -568,7 +571,7 @@ def _check_chunks(self, chunks, data=None):
568571
msg = ('Shape dimensions ({}) are not the same length as chunks '
569572
'({}). Please provide a single chunk value for each '
570573
'dimension!'
571-
.format(shape, chunks))
574+
.format(shape, chunks))
572575
logger.error(msg)
573576
raise HandlerRuntimeError(msg)
574577

@@ -748,6 +751,7 @@ def update_dset(self, dset, dset_array, dset_slice=None):
748751

749752
keys = (dset, ) + dset_slice
750753

754+
# pylint: disable=unnecessary-dunder-call
751755
arr = self.__getitem__(keys)
752756
if not np.array_equal(arr, dset_array):
753757
self._set_ds_array(dset, dset_array, dset_slice)

rex/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""rex Version number"""
22

3-
__version__ = "0.4.2"
3+
__version__ = "0.4.3"

tests/test_outputs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ def test_1D_dataset_shape():
253253
assert res['dset3'].shape == (8760,)
254254

255255

256+
def test_attrs_multiple_opens():
257+
"""Test that attrs are not overwritten on multiple opens"""
258+
259+
attrs_to_test = ["package", "version", "full_version_record"]
260+
with tempfile.TemporaryDirectory() as td:
261+
fp = os.path.join(td, 'outputs.h5')
262+
with Outputs(fp, 'a') as f:
263+
for attr in attrs_to_test:
264+
assert f.h5.attrs[attr] != "test"
265+
f.h5.attrs[attr] = "test"
266+
assert f.h5.attrs[attr] == "test"
267+
268+
with Outputs(fp, 'a') as f:
269+
for attr in attrs_to_test:
270+
assert f.h5.attrs["package"] == "test"
271+
272+
256273
def execute_pytest(capture='all', flags='-rapP'):
257274
"""Execute module as pytest with detailed summary report.
258275

0 commit comments

Comments
 (0)