Skip to content

Commit 84b402f

Browse files
authoredDec 13, 2024
Merge pull request #216 from bobleesj/uuid
Add gettable `id` property to `DiffractionObject`
2 parents 60b7f97 + 6e51eb7 commit 84b402f

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed
 

‎news/uuid.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Gettable `id` property to `DiffractionObject`
4+
5+
**Changed:**
6+
7+
* <news item>
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>

‎src/diffpy/utils/diffraction_objects.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import uuid
23
import warnings
34
from copy import deepcopy
45

@@ -53,6 +54,7 @@ def __init__(
5354
if yarray is None:
5455
yarray = np.empty(0)
5556

57+
self._id = uuid.uuid4()
5658
self.input_data(xarray, yarray, xtype)
5759

5860
def __eq__(self, other):
@@ -205,6 +207,14 @@ def input_xtype(self):
205207
def input_xtype(self, _):
206208
raise AttributeError(_setter_wmsg("input_xtype"))
207209

210+
@property
211+
def id(self):
212+
return self._id
213+
214+
@id.setter
215+
def id(self, _):
216+
raise AttributeError(_setter_wmsg("id"))
217+
208218
def set_angles_from_list(self, angles_list):
209219
self.angles = angles_list
210220
self.n_steps = len(angles_list) - 1.0

‎tests/test_diffraction_objects.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import re
2+
import uuid
23
from pathlib import Path
4+
from uuid import UUID
35

46
import numpy as np
57
import pytest
@@ -335,8 +337,8 @@ def test_dump(tmp_path, mocker):
335337

336338
@pytest.mark.parametrize("inputs, expected", tc_params)
337339
def test_constructor(inputs, expected):
338-
actual_do = DiffractionObject(**inputs)
339-
diff = DeepDiff(actual_do.__dict__, expected, ignore_order=True, significant_digits=13)
340+
actual = DiffractionObject(**inputs).__dict__
341+
diff = DeepDiff(actual, expected, ignore_order=True, significant_digits=13, exclude_paths="root['_id']")
340342
assert diff == {}
341343

342344

@@ -369,6 +371,29 @@ def test_all_array_setter():
369371
actual_do.all_arrays = np.empty((4, 4))
370372

371373

374+
def test_id_getter():
375+
do = DiffractionObject()
376+
assert hasattr(do, "id")
377+
assert isinstance(do.id, UUID)
378+
assert len(str(do.id)) == 36
379+
380+
381+
def test_id_getter_with_mock(mocker):
382+
mocker.patch.object(DiffractionObject, "id", new_callable=lambda: UUID("d67b19c6-3016-439f-81f7-cf20a04bee87"))
383+
do = DiffractionObject()
384+
assert do.id == UUID("d67b19c6-3016-439f-81f7-cf20a04bee87")
385+
386+
387+
def test_id_setter_error():
388+
do = DiffractionObject()
389+
390+
with pytest.raises(
391+
AttributeError,
392+
match="Direct modification of attribute 'id' is not allowed. Please use 'input_data' to modify 'id'.",
393+
):
394+
do.id = uuid.uuid4()
395+
396+
372397
def test_xarray_yarray_length_mismatch():
373398
with pytest.raises(
374399
ValueError,
@@ -384,7 +409,7 @@ def test_input_xtype_getter():
384409
assert do.input_xtype == "tth"
385410

386411

387-
def test_input_xtype_setter():
412+
def test_input_xtype_setter_error():
388413
do = DiffractionObject(xtype="tth")
389414

390415
# Attempt to directly modify the property

0 commit comments

Comments
 (0)