Skip to content

Commit 7da571a

Browse files
authored
Merge pull request #161 from stevenhua0320/deprecate-structure-function
chore: deprecate read/writeStr, placeInLattice method
2 parents 0762314 + e66ddbe commit 7da571a

File tree

11 files changed

+150
-40
lines changed

11 files changed

+150
-40
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
**Added:**
2+
3+
* Added ``place_in_lattice`` method to ``Structure``
4+
* Added ``read_structure`` method to ``Structure``
5+
* Added ``write_structure`` method to ``Structure``
6+
7+
**Changed:**
8+
9+
* Changed private method ``__emptySharedStructure`` to ``__empty_shared_structure``
10+
11+
**Deprecated:**
12+
13+
* Deprecated ``placeInLattice`` method of ``Structure`` for removal in version 4.0.0
14+
* Deprecated ``readStr`` method of ``Structure`` for removal in version 4.0.0
15+
* Deprecated ``writeStr`` method of ``Structure`` for removal in version 4.0.0
16+
17+
**Removed:**
18+
19+
* <news item>
20+
21+
**Fixed:**
22+
23+
* <news item>
24+
25+
**Security:**
26+
27+
* <news item>

src/diffpy/structure/apps/transtru.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ def main():
109109
strufile = args[1]
110110
stru = Structure()
111111
if args[1] == "-":
112-
stru.readStr(sys.stdin.read(), infmt)
112+
stru.read_structure(sys.stdin.read(), infmt)
113113
else:
114114
stru.read(strufile, infmt)
115-
sys.stdout.write(stru.writeStr(outfmt))
115+
sys.stdout.write(stru.write_structure(outfmt))
116116
except IndexError:
117117
print("strufile not specified", file=sys.stderr)
118118
sys.exit(2)

src/diffpy/structure/parsers/p_discus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def parseLines(self, lines):
125125
latpars = list(self.stru.lattice.abcABG())
126126
superlatpars = [latpars[i] * self.stru.pdffit["ncell"][i] for i in range(3)] + latpars[3:]
127127
superlattice = Lattice(*superlatpars)
128-
self.stru.placeInLattice(superlattice)
128+
self.stru.place_in_lattice(superlattice)
129129
self.stru.pdffit["ncell"] = [1, 1, 1, exp_natoms]
130130
except (ValueError, IndexError):
131131
exc_type, exc_value, exc_traceback = sys.exc_info()

src/diffpy/structure/parsers/p_pdffit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def parseLines(self, lines):
169169
if stru.pdffit["ncell"][:3] != [1, 1, 1]:
170170
superlatpars = [latpars[i] * stru.pdffit["ncell"][i] for i in range(3)] + latpars[3:]
171171
superlattice = Lattice(*superlatpars)
172-
stru.placeInLattice(superlattice)
172+
stru.place_in_lattice(superlattice)
173173
stru.pdffit["ncell"] = [1, 1, 1, p_natoms]
174174
except (ValueError, IndexError):
175175
emsg = "%d: file is not in PDFfit format" % p_nl

src/diffpy/structure/pdffitstructure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def readStr(self, s, format="auto"):
9797
StructureParser
9898
Instance of `StructureParser` used to load the data.
9999
"""
100-
p = Structure.readStr(self, s, format)
100+
p = Structure.read_structure(self, s, format)
101101
sg = getattr(p, "spacegroup", None)
102102
if sg:
103103
self.pdffit["spcgr"] = sg.short_name

src/diffpy/structure/structure.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@
4040
"get_last_atom",
4141
removal_version,
4242
)
43+
placeInLattice_deprecation_msg = build_deprecation_message(
44+
base,
45+
"placeInLattice",
46+
"place_in_lattice",
47+
removal_version,
48+
)
49+
readStr_deprecation_msg = build_deprecation_message(
50+
base,
51+
"readStr",
52+
"read_structure",
53+
removal_version,
54+
)
55+
writeStr_deprecation_msg = build_deprecation_message(
56+
base,
57+
"writeStr",
58+
"write_structure",
59+
removal_version,
60+
)
4361

4462

4563
class Structure(list):
@@ -282,7 +300,16 @@ def angle(self, aid0, aid1, aid2):
282300
u12 = a2.xyz - a1.xyz
283301
return self.lattice.angle(u10, u12)
284302

303+
@deprecated(placeInLattice_deprecation_msg)
285304
def placeInLattice(self, new_lattice):
305+
"""This function has been deprecated and will be removed in
306+
version 4.0.0.
307+
308+
Please use diffpy.structure.Structure.place_in_lattice instead.
309+
"""
310+
return self.place_in_lattice(new_lattice)
311+
312+
def place_in_lattice(self, new_lattice):
286313
"""Place structure into `new_lattice` coordinate system.
287314
288315
Sets `lattice` to `new_lattice` and recalculate fractional coordinates
@@ -345,7 +372,16 @@ def read(self, filename, format="auto"):
345372
self.title = tailbase
346373
return p
347374

375+
@deprecated(readStr_deprecation_msg)
348376
def readStr(self, s, format="auto"):
377+
"""This function has been deprecated and will be removed in
378+
version 4.0.0.
379+
380+
Please use diffpy.structure.Structure.read_structure instead.
381+
"""
382+
return self.read_structure(s, format)
383+
384+
def read_structure(self, s, format="auto"):
349385
"""Read structure from a string.
350386
351387
Parameters
@@ -399,7 +435,16 @@ def write(self, filename, format):
399435
fp.write(s)
400436
return
401437

438+
@deprecated(writeStr_deprecation_msg)
402439
def writeStr(self, format):
440+
"""This function has been deprecated and will be removed in
441+
version 4.0.0.
442+
443+
Please use diffpy.structure.Structure.write_structure instead.
444+
"""
445+
return self.write_structure(format)
446+
447+
def write_structure(self, format):
403448
"""Return string representation of the structure in specified
404449
format.
405450
@@ -537,7 +582,7 @@ def __getitem__(self, idx):
537582
>>> stru['Na3', 2, 'Cl2']
538583
"""
539584
if isinstance(idx, slice):
540-
rv = self.__emptySharedStructure()
585+
rv = self.__empty_shared_structure()
541586
lst = super(Structure, self).__getitem__(idx)
542587
rv.extend(lst, copy=False)
543588
return rv
@@ -556,7 +601,7 @@ def __getitem__(self, idx):
556601
idx1 = numpy.r_[idx]
557602
indices = numpy.arange(len(self))[idx1]
558603
rhs = [list.__getitem__(self, i) for i in indices]
559-
rv = self.__emptySharedStructure()
604+
rv = self.__empty_shared_structure()
560605
rv.extend(rhs, copy=False)
561606
return rv
562607
# here we need to resolve at least one string label
@@ -917,7 +962,7 @@ def _get_composition(self):
917962

918963
# Private Methods --------------------------------------------------------
919964

920-
def __emptySharedStructure(self):
965+
def __empty_shared_structure(self):
921966
"""Return empty `Structure` with standard attributes same as in
922967
self."""
923968
rv = Structure()

tests/test_p_cif.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,33 @@ def test_write_and_read(self):
224224
self.assertAlmostEqual(0.046164, a3.U[2, 2])
225225
return
226226

227+
def test_write_structure_and_read_structure(self):
228+
"""High-level check of P_cif.tostring()"""
229+
# high-level check
230+
stru_check = Structure()
231+
stru_check.read(self.cdsebulkpdffitfile)
232+
s_s = stru_check.write_structure("cif")
233+
stru = Structure()
234+
stru.read_structure(s_s, "cif")
235+
self.assertAlmostEqual(4.2352, stru.lattice.a, self.places)
236+
self.assertAlmostEqual(4.2352, stru.lattice.b, self.places)
237+
self.assertAlmostEqual(6.90603, stru.lattice.c, self.places)
238+
self.assertEqual(4, len(stru))
239+
a0 = stru[0]
240+
self.assertEqual("Cd", a0.element)
241+
self.assertTrue(numpy.allclose([0.3334, 0.6667, 0.0], a0.xyz))
242+
self.assertTrue(a0.anisotropy)
243+
self.assertAlmostEqual(0.01303, a0.U[0, 0])
244+
self.assertAlmostEqual(0.01303, a0.U[1, 1])
245+
self.assertAlmostEqual(0.01402, a0.U[2, 2])
246+
a3 = stru[3]
247+
self.assertEqual("Se", a3.element)
248+
self.assertTrue(numpy.allclose([0.6666, 0.333300, 0.87667], a3.xyz))
249+
self.assertAlmostEqual(0.015673, a3.U[0, 0])
250+
self.assertAlmostEqual(0.015673, a3.U[1, 1])
251+
self.assertAlmostEqual(0.046164, a3.U[2, 2])
252+
return
253+
227254
def test_eps(self):
228255
"""Test the P_cif.eps coordinates resolution."""
229256
pcif = P_cif()

tests/test_p_discus.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,53 +91,53 @@ def test_ignored_lines(self):
9191
ni_lines.insert(2, r1)
9292
ni_lines.insert(4, r2)
9393
s_s1 = "".join(ni_lines)
94-
p = self.stru.readStr(s_s1, self.format)
94+
p = self.stru.read_structure(s_s1, self.format)
9595
self.assertEqual([r1.rstrip(), r2.rstrip()], p.ignored_lines)
9696
ni_lines.append(r1)
9797
s_s2 = "".join(ni_lines)
98-
self.assertRaises(StructureFormatError, self.stru.readStr, s_s2, self.format)
98+
self.assertRaises(StructureFormatError, self.stru.read_structure, s_s2, self.format)
9999
return
100100

101101
def test_spdiameter_parsing(self):
102102
"""Check parsing of spdiameter record from a file."""
103103
stru = self.stru
104104
stru.read(self.datafile("Ni-discus.stru"), self.format)
105105
self.assertEqual(0, stru.pdffit["spdiameter"])
106-
snoshape = stru.writeStr(format=self.format)
106+
snoshape = stru.write_structure(format=self.format)
107107
self.assertTrue(not re.search("(?m)^shape", snoshape))
108108
# produce a string with non-zero spdiameter
109109
stru.pdffit["spdiameter"] = 13
110-
s13 = stru.writeStr(format=self.format)
110+
s13 = stru.write_structure(format=self.format)
111111
self.assertTrue(re.search("(?m)^shape +sphere, ", s13))
112112
stru13 = Structure()
113-
stru13.readStr(s13)
113+
stru13.read_structure(s13)
114114
self.assertEqual(13, stru13.pdffit["spdiameter"])
115115
with open(self.datafile("Ni.stru")) as fp:
116116
ni_lines = fp.readlines()
117117
ni_lines.insert(3, "shape invalid, 7\n")
118118
sbad = "".join(ni_lines)
119-
self.assertRaises(StructureFormatError, self.stru.readStr, sbad, format=self.format)
119+
self.assertRaises(StructureFormatError, self.stru.read_structure, sbad, format=self.format)
120120
return
121121

122122
def test_stepcut_parsing(self):
123123
"""Check parsing of stepcut record from a file."""
124124
stru = self.stru
125125
stru.read(self.datafile("Ni-discus.stru"), self.format)
126126
self.assertEqual(0, stru.pdffit["stepcut"])
127-
snoshape = stru.writeStr(format=self.format)
127+
snoshape = stru.write_structure(format=self.format)
128128
self.assertTrue(not re.search("(?m)^shape", snoshape))
129129
# produce a string with non-zero stepcut
130130
stru.pdffit["stepcut"] = 13
131-
s13 = stru.writeStr(format=self.format)
131+
s13 = stru.write_structure(format=self.format)
132132
self.assertTrue(re.search("(?m)^shape +stepcut, ", s13))
133133
stru13 = Structure()
134-
stru13.readStr(s13)
134+
stru13.read_structure(s13)
135135
self.assertEqual(13, stru13.pdffit["stepcut"])
136136
with open(self.datafile("Ni.stru")) as fp:
137137
ni_lines = fp.readlines()
138138
ni_lines.insert(3, "shape invalid, 7\n")
139139
sbad = "".join(ni_lines)
140-
self.assertRaises(StructureFormatError, self.stru.readStr, sbad, format=self.format)
140+
self.assertRaises(StructureFormatError, self.stru.read_structure, sbad, format=self.format)
141141
return
142142

143143

tests/test_p_pdffit.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def test_writeStr_pdffit(self):
172172
f_s = fp.read()
173173
f_s = re.sub("[ \t]+", " ", f_s)
174174
f_s = re.sub("[ \t]+\n", "\n", f_s)
175-
s_s = stru.writeStr(self.format)
175+
s_s = stru.write_structure(self.format)
176176
s_s = re.sub("[ \t]+", " ", s_s)
177177
self.assertEqual(f_s, s_s)
178178
return
@@ -181,9 +181,9 @@ def test_huge_occupancy(self):
181181
"""Check structure with huge occupancy can be read."""
182182
self.stru.read(self.datafile("Ni.stru"), self.format)
183183
self.stru[0].occupancy = 16e16
184-
s_s = self.stru.writeStr(self.format)
184+
s_s = self.stru.write_structure(self.format)
185185
stru1 = Structure()
186-
stru1.readStr(s_s, self.format)
186+
stru1.read_structure(s_s, self.format)
187187
self.assertEqual(16e16, stru1[0].occupancy)
188188
return
189189

@@ -196,53 +196,53 @@ def test_ignored_lines(self):
196196
ni_lines.insert(2, r1 + "\n")
197197
ni_lines.insert(4, r2 + "\n")
198198
s_s1 = "".join(ni_lines)
199-
p = self.stru.readStr(s_s1, self.format)
199+
p = self.stru.read_structure(s_s1, self.format)
200200
self.assertEqual([r1, r2], p.ignored_lines)
201201
ni_lines.insert(-3, r1 + "\n")
202202
s_s2 = "".join(ni_lines)
203-
self.assertRaises(StructureFormatError, self.stru.readStr, s_s2, self.format)
203+
self.assertRaises(StructureFormatError, self.stru.read_structure, s_s2, self.format)
204204
return
205205

206206
def test_spdiameter_parsing(self):
207207
"""Check parsing of spdiameter record from a file."""
208208
stru = self.stru
209209
stru.read(self.datafile("Ni.stru"), self.format)
210210
self.assertEqual(0, stru.pdffit["spdiameter"])
211-
snoshape = stru.writeStr(format=self.format)
211+
snoshape = stru.write_structure(format=self.format)
212212
self.assertTrue(not re.search("(?m)^shape", snoshape))
213213
# produce a string with non-zero spdiameter
214214
stru.pdffit["spdiameter"] = 13
215-
s13 = stru.writeStr(format=self.format)
215+
s13 = stru.write_structure(format=self.format)
216216
self.assertTrue(re.search("(?m)^shape +sphere, ", s13))
217217
stru13 = Structure()
218-
stru13.readStr(s13)
218+
stru13.read_structure(s13)
219219
self.assertEqual(13, stru13.pdffit["spdiameter"])
220220
with open(self.datafile("Ni.stru")) as fp:
221221
ni_lines = fp.readlines()
222222
ni_lines.insert(3, "shape invalid, 7\n")
223223
sbad = "".join(ni_lines)
224-
self.assertRaises(StructureFormatError, self.stru.readStr, sbad, format=self.format)
224+
self.assertRaises(StructureFormatError, self.stru.read_structure, sbad, format=self.format)
225225
return
226226

227227
def test_stepcut_parsing(self):
228228
"""Check parsing of stepcut record from a file."""
229229
stru = self.stru
230230
stru.read(self.datafile("Ni.stru"), self.format)
231231
self.assertEqual(0, stru.pdffit["stepcut"])
232-
snoshape = stru.writeStr(format=self.format)
232+
snoshape = stru.write_structure(format=self.format)
233233
self.assertTrue(not re.search("(?m)^shape", snoshape))
234234
# produce a string with non-zero stepcut
235235
stru.pdffit["stepcut"] = 13
236-
s13 = stru.writeStr(format=self.format)
236+
s13 = stru.write_structure(format=self.format)
237237
self.assertTrue(re.search("(?m)^shape +stepcut, ", s13))
238238
stru13 = Structure()
239-
stru13.readStr(s13)
239+
stru13.read_structure(s13)
240240
self.assertEqual(13, stru13.pdffit["stepcut"])
241241
with open(self.datafile("Ni.stru")) as fp:
242242
ni_lines = fp.readlines()
243243
ni_lines.insert(3, "shape invalid, 7\n")
244244
sbad = "".join(ni_lines)
245-
self.assertRaises(StructureFormatError, self.stru.readStr, sbad, format=self.format)
245+
self.assertRaises(StructureFormatError, self.stru.read_structure, sbad, format=self.format)
246246
return
247247

248248

0 commit comments

Comments
 (0)