No spaces before comments results in a string, rather than the intended type
Closed this issue · 2 comments
I'm using f90nml for writing a bunch of input files for a program. It works great, except for when the original input file has namelists like the following:
&collisions_namelist ! can ignore
collision_model = 'default'
use_layout = T!F
/
Here, f90nml correctly recognises " ! can ignore"
as a comment, but it fails to do so with T!F
, and instead it gets converted to 'T!F'
when recreating the file (and so therefore is presumably turned into a string in Python). If I add a space before the !
, then it correctly assumes T
is a boolean identifier, and converts it to .true.
and just discards the comment.
Formally, this could be one of those unparseable situations. You and I know that use_layout
is logical, but the parser doesn't know if it's a logical or a string.
However, the parser can be coaxed to do what you want here. If you create a parser with strict_logical = False
, then it will interpret any value starting with T
or .T
as a logical .true.
. For example:
>>> import f90nml
>>> p = f90nml.Parser()
>>> p.strict_logical = False
>>> nml = p.read('sample.nml')
>>> nml
Namelist([('collisions_namelist', Namelist([('collision_model', 'default'), ('use_layout', True)]))])
>>> print(nml)
&collisions_namelist
collision_model = 'default'
use_layout = .true.
/
A more robust solution would let you tag use_layout
as a logical
in some way, but I think this will fix your immediate problem.
Closing this due to lack of feedback.
From my perspective, this is not an issue but an unparseable edge case, where f90nml happens to make a different choice as the OP. A flag exists to change this behavior, so I will consider this to be closed.
Please feel free to reopen if you feel differently or have a suggestion to change the behavior.