-
Notifications
You must be signed in to change notification settings - Fork 51
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
Add Parser.patch and Parser.write methods #84
Comments
As I am sure you can gather from my write up, this is not a bug, but rather an enhancement request. |
I forgot to mention that I have been (slowly) working on a rewrite of the parser: newparser. That parser is still incomplete, with the core stuff and derived types working, vectors with arbitrary start index are the next hurdle. But it would be good to make sure this is handled in both versions. (This is not your problem, I'm mostly writing this for myself!) Having said that, I understand your problem a little bit better (handling content within There is some precedent for this with the |
I do like the idea of adding the possibility of allowing the specification of ghost variables, But more importantly it would be nice if the |
I agree, it would be good to pre-format the file before handling the parsing. The earlier versions of this module could handle a stream and work with each byte as it came in, but this was often very awkward and caused a lot of the complexity in the current Currently, we are not even doing this anymore, since the tokenizer (which ought to still be capable of handling streams) just dumps all of the tokens into There are grander goals of returning to this streamable method with the tokenizer acting as a true iterator, but I don't know if it was ever really a worthwhile goal, and it has made it harder to do some of the tasks that you need here. Sorry for the very long answer; this is helping me to rethink some of the original design assumptions, and that I am open to introduce some of the changes that you are suggesting. BTW there could also be a place for introducing new custom tokens like |
Currently
Parser.read
is used to perform three different tasks: reading, patching and writing. This muti-use architecture makes sub-classing or wrapping of the class difficult. Splitting of this functionality into separate methods would improve extendability.Proposal
Split the functionality in Parser.read into the following methods:
Parser.read
Parser.patch
Also for completeness:
Parser.write
In the process add the following internal methods:
Parser._read_nml_file
Parser._write_nml_file
Complications
Currently reading, patching and writing are all done inside the token parser logic, which is started in
Parser._readstream
. There is no easy way to separate the various functions as they are all part of the fundamental logic off90nml
.Possible Solution
It is possible to fake the separation of
read
,parse
andwrite
by usingStringIO
objects in the call to_readstream
. However, to make this work smoothly requires an intermediate layer in the code.Advantages
f90nml.Parser
is clear and easy to subclass or wrap.Disadvantages
Motivation
I am only providing this description since I am proposing a major(ish) change to
Parser
and thought you might like to see a specific use case.I happen to be working with a scientific code where
input.namelist
files are used both for input to a FORTRAN code and for a series of Python based wrappers. This is enabled by using a special comment specifier, e.g.!ss variable = value
, which may or may not be inside a proper namelist group.(I am not defending this design choice, but it is what I have to work with).
By creating a subclass of
f90nml
I am able to easily read these files by doing the following:Parser.read
, but using StreamIO for input and output.!ss
directives.The text was updated successfully, but these errors were encountered: