From 793f5b08666f1d32fc37b459a4aebb62c196f919 Mon Sep 17 00:00:00 2001 From: stevenhua0320 Date: Fri, 20 Feb 2026 00:14:29 -0500 Subject: [PATCH] build: deprecate getLastAtom and add a test --- news/deprecate-getLastAtom.rst | 23 +++++++++++++++++++++++ src/diffpy/structure/parsers/p_cif.py | 2 +- src/diffpy/structure/parsers/p_discus.py | 2 +- src/diffpy/structure/parsers/p_pdb.py | 2 +- src/diffpy/structure/parsers/p_pdffit.py | 2 +- src/diffpy/structure/structure.py | 15 +++++++++++++++ tests/test_structure.py | 17 +++++++++++++++++ 7 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 news/deprecate-getLastAtom.rst diff --git a/news/deprecate-getLastAtom.rst b/news/deprecate-getLastAtom.rst new file mode 100644 index 0000000..9f2824a --- /dev/null +++ b/news/deprecate-getLastAtom.rst @@ -0,0 +1,23 @@ +**Added:** + +* Added `diffpy.structure.Structure.get_last_atom` in replace of `getLastAtom` + +**Changed:** + +* + +**Deprecated:** + +* Deprecated `diffpy.structure.Structure.getLastAtom` for removal in version 4.0.0 + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/structure/parsers/p_cif.py b/src/diffpy/structure/parsers/p_cif.py index b542e69..e8029cd 100644 --- a/src/diffpy/structure/parsers/p_cif.py +++ b/src/diffpy/structure/parsers/p_cif.py @@ -498,7 +498,7 @@ def _parse_atom_site_label(self, block): continue self.labelindex[curlabel] = len(self.stru) self.stru.add_new_atom() - a = self.stru.getLastAtom() + a = self.stru.get_last_atom() for fset, val in zip(prop_setters, values): fset(a, val) if does_adp_type: diff --git a/src/diffpy/structure/parsers/p_discus.py b/src/diffpy/structure/parsers/p_discus.py index 665c096..35b868e 100644 --- a/src/diffpy/structure/parsers/p_discus.py +++ b/src/diffpy/structure/parsers/p_discus.py @@ -265,7 +265,7 @@ def _parse_atom(self, words): xyz = [float(w) for w in words[1:4]] Biso = float(words[4]) self.stru.add_new_atom(element, xyz) - a = self.stru.getLastAtom() + a = self.stru.get_last_atom() a.Bisoequiv = Biso return diff --git a/src/diffpy/structure/parsers/p_pdb.py b/src/diffpy/structure/parsers/p_pdb.py index 9f2ecc4..8694dba 100644 --- a/src/diffpy/structure/parsers/p_pdb.py +++ b/src/diffpy/structure/parsers/p_pdb.py @@ -198,7 +198,7 @@ def parseLines(self, lines): element = line[12:14].strip() element = element[0].upper() + element[1:].lower() stru.add_new_atom(element, occupancy=occupancy, label=name) - last_atom = stru.getLastAtom() + last_atom = stru.get_last_atom() last_atom.xyz_cartn = rc last_atom.Uisoequiv = uiso elif record == "SIGATM": diff --git a/src/diffpy/structure/parsers/p_pdffit.py b/src/diffpy/structure/parsers/p_pdffit.py index 84b55fc..93646ac 100644 --- a/src/diffpy/structure/parsers/p_pdffit.py +++ b/src/diffpy/structure/parsers/p_pdffit.py @@ -133,7 +133,7 @@ def parseLines(self, lines): xyz = [float(w) for w in wl1[1:4]] occ = float(wl1[4]) stru.add_new_atom(element, xyz=xyz, occupancy=occ) - a = stru.getLastAtom() + a = stru.get_last_atom() p_nl += 1 wl2 = next(ilines).split() a.sigxyz = [float(w) for w in wl2[0:3]] diff --git a/src/diffpy/structure/structure.py b/src/diffpy/structure/structure.py index 9e84bc8..784a11a 100644 --- a/src/diffpy/structure/structure.py +++ b/src/diffpy/structure/structure.py @@ -34,6 +34,12 @@ "add_new_atom", removal_version, ) +getLastAtom_deprecation_msg = build_deprecation_message( + base, + "getLastAtom", + "get_last_atom", + removal_version, +) class Structure(list): @@ -192,7 +198,16 @@ def add_new_atom(self, *args, **kwargs): self.append(atom, copy=False) return + @deprecated(getLastAtom_deprecation_msg) def getLastAtom(self): + """This function has been deprecated and will be removed in + version 4.0.0. + + Please use diffpy.structure.Structure.get_last_atom instead. + """ + return self.get_last_atom() + + def get_last_atom(self): """Return Reference to the last `Atom` in this structure.""" last_atom = self[-1] return last_atom diff --git a/tests/test_structure.py b/tests/test_structure.py index 648f684..6528365 100644 --- a/tests/test_structure.py +++ b/tests/test_structure.py @@ -119,6 +119,23 @@ def test___copy__(self): # def test_getLastAtom(self): # """check Structure.getLastAtom()""" # return + def test_getLastAtom(self): + """Check Structure.getLastAtom()""" + s_lat = Lattice() + expected = Atom("C", [0, 0, 0]) + structure = Structure(atoms=[Atom("C", [0, 0, 0])], lattice=s_lat) + actual = structure.getLastAtom() + assert actual.element == expected.element + assert numpy.allclose(expected.xyz, actual.xyz) + + def test_get_last_atom(self): + """Check Structure.get_last_atom()""" + s_lat = Lattice() + expected = Atom("C", [0, 0, 0]) + structure = Structure(atoms=[Atom("C", [0, 0, 0])], lattice=s_lat) + actual = structure.get_last_atom() + assert actual.element == expected.element + assert numpy.allclose(expected.xyz, actual.xyz) def test_addNewAtom(self): """Duplicate test for the deprecated addNewAtom method.