Skip to content
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

Fix RPN validation #281

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This is a major release with significant upgrades under the hood of Cheetah. Des
- Port Bmad-X tracking methods to Cheetah for `Quadrupole`, `Drift`, and `Dipole` (see #153, #240) (@jp-ga, @jank324)
- Add `TransverseDeflectingCavity` element (following the Bmad-X implementation) (see #240, #278) (@jp-ga, @cr-xu, @jank324)
- `Dipole` and `RBend` now take a focusing moment `k1` (see #235, #247) (@hespe)
- Implement a converter for lattice files imported from Elegant (see #222, #251, #273) (@hespe)
- Implement a converter for lattice files imported from Elegant (see #222, #251, #273, #281) (@hespe, @jank324)

### 🐛 Bug fixes

Expand Down
4 changes: 2 additions & 2 deletions cheetah/converters/utils/rpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def is_valid_expression(expression: str) -> bool:
"""Checks if expression is a reverse Polish notation."""
stripped = expression[1:-1].strip()
stripped = expression.strip()
return stripped[-1] in "+-/*" and len(stripped.split(" ")) == 3


Expand All @@ -13,5 +13,5 @@ def eval_expression(expression: str, context: dict) -> Any:

NOTE: Does not support nested expressions.
"""
splits = expression[1:-1].strip().split(" ")
splits = expression.strip().split(" ")
return eval(" ".join([splits[0], splits[2], splits[1]]), context)
2 changes: 1 addition & 1 deletion tests/resources/cavity.lte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
C1E: EMATRIX, L = 0, ORDER = 1, C2=-0.0027, C4=-0.150, R11=1, R21=0.04, R22=1, R23=0.003, R33=1, R41=0.003, R43=-0.04, R44=1, R55=1, R66=1
C1: RFCA, L = 0.7, PHASE = 90.000000, VOLT = 16175000.000000, FREQ = 1200000000.000000
C1: RFCA, L = 0.7, PHASE = 90.000000, VOLT = 16175000.000000, FREQ = 1200000000.000000, CHANGE_P0 = 1, END1_FOCUS = 1, END2_FOCUS = 1, BODY_FOCUS_MODEL = "SRS"

cavity: line=(C1E,C1)
33 changes: 33 additions & 0 deletions tests/test_rpn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from cheetah.converters.utils import rpn


def test_valid_rpn_expression():
"""
Test that a valid RPN expression without nesting is correctly recognised as a valid
RPN expression.
"""
expression = "2 3 +"

assert rpn.is_valid_expression(expression)


def test_valid_rpn_expression_with_single_quotes():
"""
Test that a valid RPN expression that has single quotes around it and should
therefore not be recognised as valid because these should have been stripped off
before calling the function.
"""
expression = "'2 3 +'"

assert not rpn.is_valid_expression(expression)


def test_falsely_validated_normal_expression():
"""
Tests that the expression `"ldsp2h +dldsp17h +lblxsph/2-lbxsph/2"`, which was
falsely recognised as a valid RPN expression in a previous version of Cheetah, is
correctly recognised as invalid.
"""
expression = "ldsp2h +dldsp17h +lblxsph/2-lbxsph/2"

assert not rpn.is_valid_expression(expression)