Port PALSParserJ to Python - #1
Merged
Merged
Conversation
PALSParserPy is now a Python wrapper around the PALSParserCpp C library, mirroring PALSParserJ: the YAML tree object model, lattice expansion, expression evaluation, node correspondence, name matching, parameter lookup, and the Bmad / MAD-X / SciBmad translators. The binding is ctypes, so there is nothing to compile here; the C library is located at first call from $PALS_PARSER_CPP_LIB, $PALS_PARSER_CPP_DIR, or a PALSParserCpp checkout beside (or around) this one. The API is Pythonic where Julia's was Julian: 0-based indexing, `key in node`, len/keys/values/items, node.value / .as_int() / .as_float() / .as_bool(), KeyError / IndexError / ValueError / FileNotFoundError, and None where Julia returned missing. Every node operation is both a method and a free function, so the PALSParserJ spelling still reads. The `problems` argument takes "print", "none", or a path, in place of Julia's Symbol-vs-String overload. Verified against the reference implementation: pals_to_bmad and pals_to_madx on bta.pals.yaml, and pals_to_scibmad on convert.pals.yaml, produce byte-identical files and identical diagnostics to PALSParserJ. The 110 tests are the Julia suite ported to pytest. Also ported: the seven examples and the tree-manipulation notebook, the documentation site (one Sphinx build, autodoc replacing the Documenter half), and the test/docs workflows. One fix along the way: an ApertureP `location: CENTER` raised a type error in the Julia source (`x || (y && println(...))` yields nothing); here it warns and sets aperture_at = both_ends, as intended. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Contributor
|
📖 Documentation preview for this PR: https://pals-project.github.io/PALSParserPy/previews/PR1/ Rebuilt on every push; removed automatically when the PR closes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fills in
PALSParserPy— the Python interface to PALSParserCpp — by portingPALSParserJ, the Julia
interface to the same C library.
What is here
palsparserpy/_clib.pyctypesmirror ofPALSParserCpp.h: structs, enums, prototypes, library discoverypalsparserpy/node.pyYAMLTree/YAMLNode, parsing, navigation, editing, emittingpalsparserpy/structs.pyProblem,Lattices,ProblemSeverity/ProblemOrigin,NodeCorrespondencepalsparserpy/parser.pyparse_and_expand_pals,evaluate_pals_expression,node_correspondence,match_names,parameter_valuepalsparserpy/to_bmad.py,to_madx.py,to_scibmad.py_common.pytests/examples/docs/.github/workflows/The binding is
ctypes, so there is nothing to compile in this repository. The Clibrary is located at the first call from
$PALS_PARSER_CPP_LIB,$PALS_PARSER_CPP_DIR, or a PALSParserCpp checkout beside — or around — thisone;
import palsparserpyitself never loads it, so the docs build needs no C++toolchain.
Verification
Beyond the test suite, the translators were checked against the reference
implementation on real lattices:
pals_to_bmadandpals_to_madxonbta.pals.yaml, andpals_to_scibmadonconvert.pals.yaml, producebyte-identical files and identical diagnostics to PALSParserJ. The
match_names,node_correspondenceandevaluate_expressionsexample outputsmatch line for line.
Where it departs from a literal transcription
node[0],add_scalar(index=0)) — Python's conventionand the C API's, where PALSParserJ is 1-based.
key in node,len/keys/values/items,node.value/.as_int()/.as_float()/.as_bool(),KeyError/IndexError/ValueError/FileNotFoundError, andNonewhere Juliareturned
missing. Every node operation is both a method and a free function,so
is_map(node)still reads alongsidenode.is_map().problems="print" | "none" | <path>in place of Julia'sSymbol-vs-Stringoverload; the two names are reserved and documented.
Sphinx, so the Documenter half PALSParserJ needs has nothing to stitch on.
AperturePlocation: CENTERraised a type error inthe Julia source (
x || (y && println(...))yieldsnothing); here it warnsand sets
aperture_at = both_ends, as was clearly intended.Requires Python 3.9+; CI runs 3.9 and 3.12 on macOS and Ubuntu. The interpreter
has to match the architecture the C library was built for.
🤖 Generated with Claude Code