Skip to content

Add copy() method for DiffractionObject #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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)
13 changes: 13 additions & 0 deletions tests/test_diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,16 @@ 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",
)
do_copy = do.copy()
assert do == do_copy
assert id(do) != id(do_copy)
Loading