Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/deepcopy.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* copy() method for DiffractionObject instance

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
11 changes: 11 additions & 0 deletions src/diffpy/utils/diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,14 @@ def dump(self, filepath, xtype=None):
f.write(f"{key} = {value}\n")
f.write("\n#### start data\n")
np.savetxt(f, data_to_save, delimiter=" ")

def copy(self):
"""
Create a deep copy of the DiffractionObject instance.

Returns
-------
DiffractionObject
A new instance of DiffractionObject, which is a deep copy of the current instance.
"""
return deepcopy(self)
12 changes: 12 additions & 0 deletions tests/test_diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,15 @@ def test_all_array_setter():
"Please use 'insert_scattering_quantity' to modify `all_arrays`.",
):
actual_do.all_arrays = np.empty((4, 4))


def test_copy_object():
do = DiffractionObject(
name="test",
wavelength=4.0 * np.pi,
xarray=np.array([0.0, 90.0, 180.0]),
yarray=np.array([1.0, 2.0, 3.0]),
xtype="tth",
)
copy_of_DO = do.copy()
assert do == copy_of_DO
Copy link
Contributor Author

@bobleesj bobleesj Dec 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utilzing the object's __eq__ nice implementation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To complete this test also assert id(actual) != Id(initial)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the copy is not pointing to the same object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes done below.

Loading