Skip to content
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

FIX: tdb: Update float parsing regex #144

Merged
merged 3 commits into from Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion pycalphad/io/tdb.py
Expand Up @@ -180,7 +180,8 @@ def _tdb_grammar(): #pylint: disable=R0914
int_number = Word(nums).setParseAction(lambda t: [int(t[0])])
pos_neg_int_number = Word('+-'+nums).setParseAction(lambda t: [int(t[0])]) # '+3' or '-2' are examples
# matching float w/ regex is ugly but is recommended by pyparsing
float_number = Regex(r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?') \
regex_after_decimal = r'([0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)'
float_number = Regex(r'[-+]?([0-9]+\.(?!([0-9]|[eE])))|{0}'.format(regex_after_decimal)) \
.setParseAction(lambda t: [float(t[0])])
# symbol name, e.g., phase name, function name
symbol_name = Word(alphanums+'_:', min=1)
Expand Down
15 changes: 15 additions & 0 deletions pycalphad/tests/test_database.py
Expand Up @@ -406,3 +406,18 @@ def test_tdb_missing_terminator_element():
FUNCTION EMBCCTI 298.15 -39.72; 6000 N !"""
Database(tdb_str)


def test_database_parsing_of_floats_with_no_values_after_decimal():
"""Floats with no values after the decimal should be properly parsed (gh-143)"""
tdb_string = """$ The element has no values after the decimal in '5004.'
ELEMENT CU FCC_A1 63.546 5004. 33.15 !"""
dbf = Database.from_string(tdb_string, fmt='tdb')
assert "CU" in dbf.elements


def test_database_parsing_of_floats_with_multiple_leading_zeros():
"""Floats with multiple leading zeros should be properly parsed (gh-143)"""
tdb_string = """$ The element has multiple leading zeros in '00.546'
ELEMENT CU FCC_A1 00.546 5004.0 33.15 !"""
dbf = Database.from_string(tdb_string, fmt='tdb')
assert "CU" in dbf.elements