Skip to content

Commit 4efb6c2

Browse files
authored
Merge pull request diffpy#82 from sbillinge/dump_with_info
dump with info
2 parents 1660233 + 0e00735 commit 4efb6c2

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

news/dump_with_info.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* diffraction_object.dump now adds creation time and diffpy.utils version number to the output file
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

requirements/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ pytest
33
codecov
44
coverage
55
pytest-env
6+
pytest-mock
7+
freezegun

src/diffpy/utils/scattering_objects/diffraction_objects.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import datetime
12
from copy import deepcopy
23

34
import numpy as np
45

6+
from diffpy.utils.tools import get_package_info
7+
58
QQUANTITIES = ["q"]
69
ANGLEQUANTITIES = ["angle", "tth", "twotheta", "2theta"]
710
DQUANTITIES = ["d", "dspace"]
@@ -463,6 +466,8 @@ def dump(self, filepath, xtype=None):
463466
data_to_save = np.column_stack((self.on_tth[0], self.on_tth[1]))
464467
else:
465468
print(f"WARNING: cannot handle the xtype '{xtype}'")
469+
self.metadata.update(get_package_info("diffpy.utils", metadata=self.metadata))
470+
self.metadata["creation_time"] = datetime.datetime.now()
466471

467472
with open(filepath, "w") as f:
468473
f.write(

src/diffpy/utils/tests/test_diffraction_objects.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
import pytest
5+
from freezegun import freeze_time
56

67
from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object
78

@@ -230,22 +231,25 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected):
230231
assert (diffraction_object1 == diffraction_object2) == expected
231232

232233

233-
def test_dump(tmp_path):
234+
def test_dump(tmp_path, mocker):
234235
x, y = np.linspace(0, 10, 11), np.linspace(0, 10, 11)
235236
directory = Path(tmp_path)
236237
file = directory / "testfile"
237238
test = Diffraction_object()
238239
test.wavelength = 1.54
239240
test.name = "test"
240241
test.scat_quantity = "x-ray"
241-
test.insert_scattering_quantity(x, y, "q", metadata={"thing1": 1, "thing2": "thing2"})
242-
test.dump(file, "q")
242+
test.insert_scattering_quantity(
243+
x, y, "q", metadata={"thing1": 1, "thing2": "thing2", "package_info": {"package2": "3.4.5"}}
244+
)
245+
with mocker.patch("importlib.metadata.version", return_value="3.3.0"), freeze_time("2012-01-14"):
246+
test.dump(file, "q")
243247
with open(file, "r") as f:
244248
actual = f.read()
245249
expected = (
246250
"[Diffraction_object]\nname = test\nwavelength = 1.54\nscat_quantity = x-ray\nthing1 = 1\n"
247-
"thing2 = thing2\npackage_info = [('package1', '1.2.3'), ('diffpy.utils', '3.3.0')]\n"
248-
"creation_time = 2024-05-30 12:30:01\n\n#### start data\n0.000000000000000000e+00 0.000000000000000000e+00\n"
251+
"thing2 = thing2\npackage_info = {'package2': '3.4.5', 'diffpy.utils': '3.3.0'}\n"
252+
"creation_time = 2012-01-14 00:00:00\n\n#### start data\n0.000000000000000000e+00 0.000000000000000000e+00\n"
249253
"1.000000000000000000e+00 1.000000000000000000e+00\n2.000000000000000000e+00 2.000000000000000000e+00\n"
250254
"3.000000000000000000e+00 3.000000000000000000e+00\n4.000000000000000000e+00 4.000000000000000000e+00\n"
251255
"5.000000000000000000e+00 5.000000000000000000e+00\n"

0 commit comments

Comments
 (0)