-
Notifications
You must be signed in to change notification settings - Fork 283
allow amrex substantance fields to have an "_" #5115
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
base: main
Are you sure you want to change the base?
Conversation
this allows for NSE protons in our output ("P_nse")
@zingale Do you mind if I fix those problems and add some tests? |
I'm happy with the changes |
nose test failure was from an aborted run after timing out:
probably a fluke (since other tests ran recently no problem), will trigger it again. |
@yt-fido test this please |
Is this one waiting on a review? |
yes. I think that since I started it and @yut23 then modified it, someone else should review. |
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.
Looks fine, and mostly I have questions more than demands, though there's one point I'd really want to see taken into account about making escaping idempotent.
@@ -504,8 +504,8 @@ def setup_fluid_fields(self): | |||
) | |||
|
|||
|
|||
substance_expr_re = re.compile(r"\(([a-zA-Z][a-zA-Z0-9]*)\)") | |||
substance_elements_re = re.compile(r"(?P<element>[a-zA-Z]+)(?P<digits>\d*)") | |||
substance_expr_re = re.compile(r"\(([a-zA-Z][a-zA-Z0-9_]*)\)") |
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.
Maybe you guys prefer the verbose style, in which case don't mind this nit, but I couldn't resist pointing out that this expression is now 1-1 equivalent
substance_expr_re = re.compile(r"\(([a-zA-Z][a-zA-Z0-9_]*)\)") | |
substance_expr_re = re.compile(r"\(([a-zA-Z]\w*)\)") |
for element, count in self._spec | ||
) | ||
|
||
def _to_tex_descriptive(self) -> str: | ||
return str(self) | ||
return self._tex_escape(str(self)) |
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.
any reason not to update the corresponding __str__
implementation instead ?
@@ -539,18 +539,22 @@ def __str__(self) -> str: | |||
f"{element}{count if count > 1 else ''}" for element, count in self._spec | |||
) | |||
|
|||
@staticmethod | |||
def _tex_escape(s: str) -> str: | |||
return s.replace("_", r"\_") |
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 can see this snowballing into a subtle bug where a single underscore would be escaped multiple times instead of once. I'm sure we make this method idempotent using a regexp:
return s.replace("_", r"\_") | |
# replace '_' with '\_' once, but leave already-escaped occurrences alone | |
return re.sub(r"[^\\]_", r"\\_", s) |
@@ -12,6 +12,7 @@ | |||
pytest.param("X(C12H24)", [("C", 12), ("H", 24)], id="molecule_2"), | |||
pytest.param("X(H2O)", [("H", 2), ("O", 1)], id="molecule_3"), | |||
pytest.param("X(ash)", [("ash", 0)], id="descriptive_name"), | |||
pytest.param("X(P_nse)", [("P_nse", 0)], id="descriptive_underscore"), |
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 did a double take here: the underscore isn't descriptive by itself, or is it ?
pytest.param("X(P_nse)", [("P_nse", 0)], id="descriptive_underscore"), | |
pytest.param("X(P_nse)", [("P_nse", 0)], id="with_an_underscore"), |
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 same applies to the other two test cases)
I'm adding this to the 4.5.0 milestone as it's not currently labeled as a bugfix, but if you guys would like it backported to yt 4.4.x, this seems fine to me, so don't hesitate to changie the milestone accordingly and update the labels (enhancement -> bug). |
this allows for NSE protons in our output ("P_nse")
PR Summary
PR Checklist