From bef8a1cdd8782a6acdfbb63610616ba555e80d4c Mon Sep 17 00:00:00 2001 From: Kai Wagoner-Oshima Date: Mon, 2 Jun 2025 02:36:51 -0400 Subject: [PATCH 1/2] Fix #124: Support pathlib.Path in loadStructure and add test --- src/diffpy/structure/__init__.py | 6 +++--- tests/test_loadstructure.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/diffpy/structure/__init__.py b/src/diffpy/structure/__init__.py index f8106490..52d46e2b 100644 --- a/src/diffpy/structure/__init__.py +++ b/src/diffpy/structure/__init__.py @@ -37,6 +37,7 @@ # Interface definitions ------------------------------------------------------ +import os from diffpy.structure.atom import Atom from diffpy.structure.lattice import Lattice from diffpy.structure.parsers import getParser @@ -55,8 +56,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 @@ -74,7 +74,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 diff --git a/tests/test_loadstructure.py b/tests/test_loadstructure.py index 4f099c7d..08aec8ba 100644 --- a/tests/test_loadstructure.py +++ b/tests/test_loadstructure.py @@ -59,6 +59,16 @@ 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 From 9cf96d1bf5b5f3558fffc440abba38208d0224e7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Jun 2025 06:38:20 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit hooks --- src/diffpy/structure/__init__.py | 3 ++- tests/test_loadstructure.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/diffpy/structure/__init__.py b/src/diffpy/structure/__init__.py index 52d46e2b..cdfcb28f 100644 --- a/src/diffpy/structure/__init__.py +++ b/src/diffpy/structure/__init__.py @@ -37,7 +37,8 @@ # Interface definitions ------------------------------------------------------ -import os +import os + from diffpy.structure.atom import Atom from diffpy.structure.lattice import Lattice from diffpy.structure.parsers import getParser diff --git a/tests/test_loadstructure.py b/tests/test_loadstructure.py index 08aec8ba..a98c6031 100644 --- a/tests/test_loadstructure.py +++ b/tests/test_loadstructure.py @@ -62,6 +62,7 @@ def test_badkwarg(self): 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)