Skip to content

Commit 71d0d6e

Browse files
committed
Add eps keyword argument to P_cif and getParser.
Support setting of the coordinates-resolution eps when constructing the CIF parser object.
1 parent 093ed48 commit 71d0d6e

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

diffpy/Structure/Parsers/P_cif.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,18 @@ def _get_atom_setters(cifloop):
234234
# normal methods
235235
########################################################################
236236

237-
def __init__(self):
237+
def __init__(self, eps=None):
238+
"""Initialize the parser for CIF structure files.
239+
240+
eps -- fractional coordinates cutoff for duplicate positions.
241+
When None use the default for ExpandAsymmetricUnit.
242+
"""
238243
StructureParser.__init__(self)
239244
self.format = "cif"
240245
self.ciffile = None
241246
self.stru = None
242247
self.spacegroup = None
243-
self.eps = None
248+
self.eps = eps
244249
self.eau = None
245250
self.asymmetric_unit = None
246251
self.labelindex = {}
@@ -722,7 +727,12 @@ def restoreParserOutput(self):
722727
return
723728

724729

725-
def getParser():
726-
return P_cif()
730+
def getParser(eps=None):
731+
"""Return new parser object for CIF structure format.
732+
733+
eps -- fractional coordinates cutoff for duplicate positions.
734+
When None use the default for ExpandAsymmetricUnit.
735+
"""
736+
return P_cif(eps=eps)
727737

728738
# End of file

diffpy/Structure/Parsers/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
from diffpy.Structure.Parsers.structureparser import StructureParser
3636
from diffpy.Structure.Parsers.parser_index_mod import parser_index
3737

38-
def getParser(format):
38+
def getParser(format, **kw):
3939
"""Return Parser instance for a given structure format.
40+
41+
kw -- keyword arguments passed to the Parser init function.
42+
4043
Raises StructureFormatError exception when format is not defined.
4144
"""
4245
if format not in parser_index:
@@ -46,7 +49,7 @@ def getParser(format):
4649
pm = None
4750
import_cmd = 'from diffpy.Structure.Parsers import %s as pm' % pmod
4851
exec(import_cmd)
49-
return pm.getParser()
52+
return pm.getParser(**kw)
5053

5154
def inputFormats():
5255
"""Return list of implemented input structure formats"""

diffpy/Structure/tests/TestP_cif.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from diffpy.Structure.tests.testutils import datafile
2222
from diffpy.Structure.Parsers.P_cif import P_cif, leading_float, getSymOp
23+
from diffpy.Structure.Parsers import getParser
2324
from diffpy.Structure import Structure
2425
from diffpy.Structure import StructureFormatError
2526

@@ -232,6 +233,19 @@ def test_eps(self):
232233
self.assertEqual(4, len(grph2))
233234
return
234235

236+
def test_getParser(self):
237+
"""Test passing of eps keyword argument by getParser function.
238+
"""
239+
pcif = getParser('cif', eps=1e-6)
240+
grph = pcif.parseFile(self.graphiteciffile)
241+
self.assertEqual(8, len(grph))
242+
self.assertTrue(all(a.label.startswith('C1') for a in grph[:2]))
243+
self.assertTrue(all(a.label.startswith('C2') for a in grph[2:]))
244+
pcif2 = getParser('cif')
245+
grph2 = pcif2.parseFile(self.graphiteciffile)
246+
self.assertEqual(4, len(grph2))
247+
return
248+
235249
########################################################################
236250
# helpers
237251
########################################################################

0 commit comments

Comments
 (0)