Skip to content

Commit bef8a1c

Browse files
committed
Fix #124: Support pathlib.Path in loadStructure and add test
1 parent ae0110d commit bef8a1c

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/diffpy/structure/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
# Interface definitions ------------------------------------------------------
3939

40+
import os
4041
from diffpy.structure.atom import Atom
4142
from diffpy.structure.lattice import Lattice
4243
from diffpy.structure.parsers import getParser
@@ -55,8 +56,7 @@ def loadStructure(filename, fmt="auto", **kw):
5556
5657
Parameters
5758
----------
58-
59-
filename : str
59+
filename : str or pathlib.Path
6060
Path to the file to be loaded.
6161
fmt : str, Optional
6262
Format of the structure file such as 'cif' or 'xyz'. Must be
@@ -74,7 +74,7 @@ def loadStructure(filename, fmt="auto", **kw):
7474
Return a more specific PDFFitStructure type for 'pdffit'
7575
and 'discus' formats.
7676
"""
77-
77+
filename = os.fspath(filename) # This handles str, Path, and os.PathLike
7878
p = getParser(fmt, **kw)
7979
rv = p.parseFile(filename)
8080
return rv

tests/test_loadstructure.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ def test_badkwarg(self):
5959
self.assertRaises(TypeError, loadStructure, f, eps=1e-10)
6060
return
6161

62+
def test_cif_pathlib(self):
63+
"""check loading CIF file using pathlib.Path input"""
64+
from pathlib import Path
65+
f = self.datafile("PbTe.cif")
66+
f_path = Path(f) # Convert to Path object
67+
stru = loadStructure(f_path)
68+
self.assertTrue(isinstance(stru, Structure))
69+
self.assertFalse(isinstance(stru, PDFFitStructure))
70+
return
71+
6272

6373
# End of class TestLoadStructure
6474

0 commit comments

Comments
 (0)