Skip to content

Commit

Permalink
Allow for the literal '' in str2val
Browse files Browse the repository at this point in the history
One of the regexes in `str2val` did cover for strings covered in single
or double quotes, but it required at least 1 character between the
quotes. This would cause an empty-string to raise a `ValuError`.
  • Loading branch information
sphuber committed Sep 27, 2023
1 parent 5f1f458 commit c489a2a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/qe_tools/parsers/_input_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,13 @@ def _str2val(valstr):
)
# Strip any white space characters before analyzing.
valstr = valstr.strip()

# Define a tuple of regular expressions to match and their corresponding
# conversion functions.
re_fn_tuple = ((re.compile(r'[.](true|t)[.]',
re.I), lambda s: True), (re.compile(r'[.](false|f)[.]', re.I), lambda s: False),
(float_re, lambda s: float(s.replace('d', 'e').replace('D', 'E'))), (re.compile(r'[-+]?\d+$'), int),
(re.compile(r"""['"].+['"]"""), lambda s: str(s.strip("\'\""))))
(re.compile(r"""['"].*['"]"""), lambda s: str(s.strip("\'\""))))
# Convert valstr to a value.
val = None
for regex, conversion_fn in re_fn_tuple:
Expand Down
1 change: 1 addition & 0 deletions tests/data/example_comment_in_namelist.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pseudo_dir = 'pseudo/'
calculation = 'scf',
prefix = 'Si_exc1',
title = ''
/
&system
ibrav = 0
Expand Down

0 comments on commit c489a2a

Please sign in to comment.