-
Notifications
You must be signed in to change notification settings - Fork 65
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
Profiling: subparameter parser support #353
Profiling: subparameter parser support #353
Conversation
Codecov Report
@@ Coverage Diff @@
## dev/gfdl #353 +/- ##
============================================
- Coverage 38.13% 38.13% -0.01%
============================================
Files 269 269
Lines 75743 75743
Branches 13926 13926
============================================
- Hits 28884 28882 -2
- Misses 41660 41662 +2
Partials 5199 5199 see 1 file with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
d38ba4f
to
297994d
Compare
This is a relatively simple PR, but it should be reviewed by someone who is proficient in Python. @raphaeldussin or @adcroft, would one of you be willing to take a quick look at it, please, to see if it makes sense to you. This PR has to be handled before we can deal with PR #352. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the function would benefit from a docstring and some comments explaining the logic
@@ -60,18 +60,34 @@ def main(): | |||
print(json.dumps(config)) | |||
|
|||
|
|||
def parse_mom6_param(param_file): | |||
def parse_mom6_param(param_file, header=None): | |||
params = {} | |||
for line in param_file: | |||
param_stmt = line.split('!')[0].strip() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
param_stmt
is a string used as a bool in the logic, how about if line.find("!") == 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking over this, @raphaeldussin. My Python may be a bit old fashioned. Is the implicit bool(string)
operation frowned upon these days? (I tend to use if var
too often, though I had thought this one was safe.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, the PEP8 style guide suggests using if not str:
to check if a string is empty:
https://peps.python.org/pep-0008/#programming-recommendations
For sequences, (strings, lists, tuples), use the fact that empty sequences are false:
# Correct:
if not seq:
if seq:
# Wrong:
if len(seq):
if not len(seq):
|
||
# Skip blank lines | ||
if not param_stmt: | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continue
-> pass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pass
is a null operation, whereas continue
terminates the loop iteration and begins the next one. Using pass
would leave value
undefined (or worse, defined from the previous loop) and have unexpected effects.
|
||
# Subparameters | ||
if param_stmt[-1] == '%': | ||
key = param_stmt.split('%')[-2] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now param_stmt
is back to being used as a string
subheader = key | ||
if header: | ||
subheader = header + '%' + subheader | ||
value = parse_mom6_param(param_file, header=subheader) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
recursive call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function is intentionally recursive, in order to parse the nested p1%p2%p3% ... %p3%p2%p1
blocks. Is there a better solution here?
297994d
to
516c318
Compare
@raphaeldussin Thanks for the suggestions, I've added docstrings and additional comments to explain the recursive behavior of the function. I've also responded to the inline comments. |
The very crude MOM_input parser in the automatic profiler did not support subparameters (e.g. MLE% ... %MLE), which caused an error when trying to read the FMS clock output. This patch adds the support, or at least enough support to avoid errors.
516c318
to
7f8c6bd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the productive conversation surrounding this PR. It has passed pipeline testing at https://gitlab.gfdl.noaa.gov/ogrp/MOM6/-/pipelines/19324.
The very crude MOM_input parser in the automatic profiler did not support subparameters (e.g. MLE% ... %MLE), which caused an error when trying to read the FMS clock output.
This patch adds the support, or at least enough support to avoid errors.