Skip to content

Commit 6d18ab6

Browse files
committed
Add parser keyword arguments to loadStructure.
And also to the P_auto parser.
1 parent 71d0d6e commit 6d18ab6

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

diffpy/Structure/Parsers/P_auto.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ class P_auto(StructureParser):
3030
structure format.
3131
"""
3232

33-
def __init__(self):
33+
def __init__(self, **kw):
3434
StructureParser.__init__(self)
3535
self.format = "auto"
36+
self.pkw = kw
3637
return
3738

3839
# parseLines helpers
@@ -103,7 +104,7 @@ def _wrapParseMethod(self, method, *args, **kwargs):
103104
# try all parsers in sequence
104105
parsers_emsgs = []
105106
for fmt in ofmts:
106-
p = getParser(fmt)
107+
p = getParser(fmt, **self.pkw)
107108
try:
108109
pmethod = getattr(p, method)
109110
stru = pmethod(*args, **kwargs)
@@ -126,7 +127,7 @@ def _wrapParseMethod(self, method, *args, **kwargs):
126127

127128
# Routines
128129

129-
def getParser():
130-
return P_auto()
130+
def getParser(**kw):
131+
return P_auto(**kw)
131132

132133
# End of file

diffpy/Structure/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,20 @@
4242

4343
# top level routines
4444

45-
def loadStructure(filename, fmt='auto'):
45+
def loadStructure(filename, fmt='auto', **kw):
4646
"""Load a new structure from a specified file.
4747
4848
filename -- file to be loaded
4949
fmt -- format of the structure file. Must be one of the formats
5050
defined in the Parsers subpackage. When 'auto', all
5151
Parsers are tried in sequence.
52+
kw -- keyword arguments passed to the getParser factory function.
5253
5354
Return a new Structure object.
5455
Return PDFFitStructure object for 'pdffit' or 'discus' formats.
5556
"""
5657
from diffpy.Structure.Parsers import getParser
57-
p = getParser(fmt)
58+
p = getParser(fmt, **kw)
5859
rv = p.parseFile(filename)
5960
return rv
6061

diffpy/Structure/tests/TestLoadStructure.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ def test_badfile(self):
4646
self.assertRaises(StructureFormatError, loadStructure, f)
4747
return
4848

49+
def test_goodkwarg(self):
50+
"""check loading of CIF file and passing of parser keyword argument.
51+
"""
52+
f = datafile('graphite.cif')
53+
stru = loadStructure(f, eps=1e-10)
54+
self.assertEqual(8, len(stru))
55+
return
56+
57+
def test_badkwarg(self):
58+
"""check loading of xyz file format with invalid keyword argument
59+
"""
60+
f = datafile('bucky.xyz')
61+
self.assertRaises(TypeError, loadStructure, f, eps=1e-10)
62+
return
4963

5064
# End of class TestLoadStructure
5165

0 commit comments

Comments
 (0)