Skip to content

Commit d742cf8

Browse files
authored
Merge pull request #185 from stevenhua0320/fix-load-structure
fix: fix load_structure method with loading Path object.
2 parents 913382e + 4f7656c commit d742cf8

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

news/fix-load-structure.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Added method ``load_structure`` in ``__init__.py``
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* Deprecated method ``loadStructure`` in ``__init__.py`` for removal in version 4.0.0
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* Fixed ``load_structure`` with successfully loading `Path` object
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/structure/__init__.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"""
3535

3636

37+
import os
3738
import sys
3839

3940
import diffpy.structure as _structure
@@ -46,8 +47,17 @@
4647

4748
# package version
4849
from diffpy.structure.version import __version__
50+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
4951

5052
# Deprecations -------------------------------------------------------
53+
base = "diffpy.structure"
54+
removal_version = "4.0.0"
55+
loadStructure_deprecation_msg = build_deprecation_message(
56+
base,
57+
"loadStructure",
58+
"load_structure",
59+
removal_version,
60+
)
5161

5262

5363
# @deprecated
@@ -72,7 +82,17 @@ def __getattr__(self, name):
7282
# top level routines
7383

7484

85+
@deprecated(loadStructure_deprecation_msg)
7586
def loadStructure(filename, fmt="auto", **kw):
87+
"""This function has been deprecated and will be removed in version
88+
4.0.0.
89+
90+
Please use diffpy.structure.load_structure instead.
91+
"""
92+
return load_structure(filename, fmt, **kw)
93+
94+
95+
def load_structure(filename, fmt="auto", **kw):
7696
"""Load new structure object from the specified file.
7797
7898
Parameters
@@ -96,7 +116,7 @@ def loadStructure(filename, fmt="auto", **kw):
96116
Return a more specific PDFFitStructure type for 'pdffit'
97117
and 'discus' formats.
98118
"""
99-
119+
filename = os.fspath(filename)
100120
p = get_parser(fmt, **kw)
101121
rv = p.parse_file(filename)
102122
return rv

tests/test_loadstructure.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from diffpy.structure import PDFFitStructure, Structure, loadStructure
9+
from diffpy.structure import PDFFitStructure, Structure, load_structure, loadStructure
1010
from diffpy.structure.structureerrors import StructureFormatError
1111

1212

@@ -19,22 +19,22 @@ def prepare_fixture(self, datafile):
1919
def test_xcfg(self):
2020
"""Check loading of atomeye xcfg format."""
2121
f = self.datafile("BubbleRaftShort.xcfg")
22-
stru = loadStructure(f)
22+
stru = load_structure(f)
2323
self.assertTrue(type(stru) is Structure)
24-
self.assertRaises(StructureFormatError, loadStructure, f, "xyz")
24+
self.assertRaises(StructureFormatError, load_structure, f, "xyz")
2525
return
2626

2727
def test_discus(self):
2828
"""Check loading of discus file format."""
2929
f = self.datafile("Ni-discus.stru")
30-
stru = loadStructure(f)
30+
stru = load_structure(f)
3131
self.assertTrue(type(stru) is PDFFitStructure)
3232
return
3333

3434
def test_cif(self):
3535
"""Check loading of CIF file format."""
3636
f = self.datafile("PbTe.cif")
37-
stru = loadStructure(f)
37+
stru = load_structure(f)
3838
self.assertTrue(isinstance(stru, Structure))
3939
self.assertFalse(isinstance(stru, PDFFitStructure))
4040
return
@@ -45,25 +45,51 @@ def test_badfile(self):
4545
self.assertRaises(StructureFormatError, loadStructure, f)
4646
return
4747

48+
def test_load_bad_file(self):
49+
"""Check loading of CIF file format."""
50+
f = self.datafile("Ni-bad.stru")
51+
self.assertRaises(StructureFormatError, load_structure, f)
52+
return
53+
4854
def test_goodkwarg(self):
4955
"""Check loading of CIF file and passing of parser keyword
5056
argument."""
5157
f = self.datafile("graphite.cif")
52-
stru = loadStructure(f, eps=1e-10)
58+
stru = load_structure(f, eps=1e-10)
5359
self.assertEqual(8, len(stru))
5460
return
5561

5662
def test_badkwarg(self):
5763
"""Check loading of xyz file format with invalid keyword
5864
argument."""
5965
f = self.datafile("bucky.xyz")
60-
self.assertRaises(TypeError, loadStructure, f, eps=1e-10)
66+
self.assertRaises(TypeError, load_structure, f, eps=1e-10)
6167
return
6268

6369

6470
# End of class TestLoadStructure
6571

72+
6673
# ----------------------------------------------------------------------------
74+
@pytest.mark.parametrize(
75+
"filename, expected",
76+
[ # C1: Load the cif file in Path object, expected to load the Structure instance.
77+
("PbTe.cif", (True, False)),
78+
],
79+
)
80+
def test_load_structure_cif_in_path(datafile, filename, expected):
81+
from pathlib import Path
82+
83+
f = datafile(filename)
84+
f_path = Path(f)
85+
stru = load_structure(f_path)
86+
actual = (
87+
isinstance(stru, Structure),
88+
isinstance(stru, PDFFitStructure),
89+
)
90+
91+
assert actual == expected
92+
6793

6894
if __name__ == "__main__":
6995
unittest.main()

0 commit comments

Comments
 (0)