From 6c5d79b9d2161732cba5262afb70484b07927634 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Dec 2024 16:44:41 -0500 Subject: [PATCH 1/5] Add copy method for instance --- src/diffpy/utils/diffraction_objects.py | 11 +++++++++++ tests/test_diffraction_objects.py | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 6ce965d0..888f83c9 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -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) diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index aeba1522..b65521cd 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -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 From 005a19168e3662a8c0cd4681b20834b72c58710a Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Dec 2024 16:44:46 -0500 Subject: [PATCH 2/5] Add news --- news/deepcopy.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 news/deepcopy.rst diff --git a/news/deepcopy.rst b/news/deepcopy.rst new file mode 100644 index 00000000..578d360c --- /dev/null +++ b/news/deepcopy.rst @@ -0,0 +1,23 @@ +**Added:** + +* copy() method for DiffractionObject instance + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* From 33ce9b3393e687282bf7e6652d4228e3a880257c Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Dec 2024 16:58:45 -0500 Subject: [PATCH 3/5] Add one more test for different memory pointed by copied obj --- tests/test_diffraction_objects.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index b65521cd..dcb99ac6 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -422,3 +422,5 @@ def test_copy_object(): ) copy_of_DO = do.copy() assert do == copy_of_DO + assert id(do) != id(copy_of_DO) + From edfc415d107e468e3f4dcb1c70aff4c1b663595a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Dec 2024 21:58:52 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit hooks --- tests/test_diffraction_objects.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index dcb99ac6..81f6ce8c 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -423,4 +423,3 @@ def test_copy_object(): copy_of_DO = do.copy() assert do == copy_of_DO assert id(do) != id(copy_of_DO) - From 876c15ee41c164e91949fbc0f75530cd7bce3d88 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Dec 2024 17:00:38 -0500 Subject: [PATCH 5/5] enhance variable name --- tests/test_diffraction_objects.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index dcb99ac6..dcdd4f59 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -420,7 +420,6 @@ def test_copy_object(): yarray=np.array([1.0, 2.0, 3.0]), xtype="tth", ) - copy_of_DO = do.copy() - assert do == copy_of_DO - assert id(do) != id(copy_of_DO) - + do_copy = do.copy() + assert do == do_copy + assert id(do) != id(do_copy)