Skip to content

Fix #124: Support pathlib.Path in loadStructure and add test #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/diffpy/structure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

# Interface definitions ------------------------------------------------------

import os

from diffpy.structure.atom import Atom
from diffpy.structure.lattice import Lattice
from diffpy.structure.parsers import getParser
Expand All @@ -55,8 +57,7 @@ def loadStructure(filename, fmt="auto", **kw):

Parameters
----------

filename : str
filename : str or pathlib.Path
Path to the file to be loaded.
fmt : str, Optional
Format of the structure file such as 'cif' or 'xyz'. Must be
Expand All @@ -74,7 +75,7 @@ def loadStructure(filename, fmt="auto", **kw):
Return a more specific PDFFitStructure type for 'pdffit'
and 'discus' formats.
"""

filename = os.fspath(filename) # This handles str, Path, and os.PathLike
p = getParser(fmt, **kw)
rv = p.parseFile(filename)
return rv
Expand Down
11 changes: 11 additions & 0 deletions tests/test_loadstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ def test_badkwarg(self):
self.assertRaises(TypeError, loadStructure, f, eps=1e-10)
return

def test_cif_pathlib(self):
"""check loading CIF file using pathlib.Path input"""
from pathlib import Path

f = self.datafile("PbTe.cif")
f_path = Path(f) # Convert to Path object
stru = loadStructure(f_path)
self.assertTrue(isinstance(stru, Structure))
self.assertFalse(isinstance(stru, PDFFitStructure))
return


# End of class TestLoadStructure

Expand Down