-
Notifications
You must be signed in to change notification settings - Fork 4
[Draft] Literal Include #61
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
Draft
ax3l
wants to merge
3
commits into
pals-project:main
Choose a base branch
from
ax3l:draft-include
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import pals | ||
|
|
||
|
|
||
| def test_include(tmp_path): | ||
| main_file = tmp_path / "main.pals.yaml" | ||
| root_included_file = tmp_path / "included.pals.yaml" | ||
| facility_included_file = tmp_path / "facility.pals.yaml" | ||
| facility_nested_file = tmp_path / "facility_nested.pals.yaml" | ||
|
|
||
| main_content = f""" | ||
| PALS: | ||
| include: "{root_included_file.name}" | ||
| facility: | ||
| - drift1: | ||
| kind: Drift | ||
| length: 0.25 | ||
|
|
||
| - include: "{facility_included_file.name}" | ||
|
|
||
| - fodo_cell: | ||
| kind: BeamLine | ||
| line: | ||
| - drift1 | ||
| - quad1 | ||
| - drift2 | ||
| - quad2 | ||
| - drift1 | ||
|
|
||
| - fodo_lattice: | ||
| kind: Lattice | ||
| branches: | ||
| - fodo_cell | ||
|
|
||
| - use: fodo_lattice | ||
| """ | ||
|
|
||
| root_included_content = """ | ||
| author: "Some One <name@email.com>" | ||
| version: 1.0 | ||
| """ | ||
|
|
||
| facility_included_content = f""" | ||
| - quad1: | ||
| kind: Quadrupole | ||
| MagneticMultipoleP: | ||
| Bn1: 1.0 | ||
| length: 1.0 | ||
|
|
||
| - drift2: | ||
| kind: Drift | ||
| length: 0.5 | ||
|
|
||
| - include: "{facility_nested_file.name}" | ||
| """ | ||
|
|
||
| facility_nested_content = """ | ||
| - quad2: | ||
| kind: Quadrupole | ||
| MagneticMultipoleP: | ||
| Bn1: -1.0 | ||
| length: 1.0 | ||
| """ | ||
|
|
||
| main_file.write_text(main_content) | ||
| root_included_file.write_text(root_included_content) | ||
| facility_included_file.write_text(facility_included_content) | ||
| facility_nested_file.write_text(facility_nested_content) | ||
|
|
||
| data = pals.Lattice.from_file(main_file) | ||
|
|
||
| assert data["PALS"]["version"] == 1.0 | ||
| assert data["PALS"]["author"] == "Some One <name@email.com>" | ||
| assert "include" not in data["PALS"] | ||
|
|
||
|
|
||
| def test_nested_include(tmp_path): | ||
| root_file = tmp_path / "root.pals.yaml" | ||
| middle_file = tmp_path / "middle.pals.yaml" | ||
| leaf_file = tmp_path / "leaf.pals.yaml" | ||
|
|
||
| root_content = f""" | ||
| root: | ||
| include: "{middle_file.name}" | ||
| """ | ||
|
|
||
| middle_content = f""" | ||
| middle: val | ||
| include: "{leaf_file.name}" | ||
| """ | ||
|
|
||
| leaf_content = """ | ||
| leaf: val | ||
| """ | ||
|
|
||
| root_file.write_text(root_content) | ||
| middle_file.write_text(middle_content) | ||
| leaf_file.write_text(leaf_content) | ||
|
|
||
| data = pals.functions.load_file_to_dict(str(root_file)) | ||
|
|
||
| assert data["root"]["middle"] == "val" | ||
| assert data["root"]["leaf"] == "val" | ||
| assert "include" not in data["root"] | ||
|
|
||
|
|
||
| def test_include_list_into_dict_conversion(tmp_path): | ||
| # This tests the spec example where a list of properties is included into a dict (element) | ||
| main_file = tmp_path / "element.pals.yaml" | ||
| params_file = tmp_path / "params.pals.yaml" | ||
|
|
||
| main_content = f""" | ||
| element: | ||
| kind: Quadrupole | ||
| include: "{params_file.name}" | ||
| """ | ||
|
|
||
| # params file content is a list of single-key dicts | ||
| params_content = """ | ||
| - MagneticMultipoleP: | ||
| - Kn3L: 0.3 | ||
| - ApertureP: | ||
| x_limits: [-0.1, 0.1] | ||
| """ | ||
|
|
||
| main_file.write_text(main_content) | ||
| params_file.write_text(params_content) | ||
|
|
||
| data = pals.functions.load_file_to_dict(str(main_file)) | ||
|
|
||
| elem = data["element"] | ||
| assert elem["kind"] == "Quadrupole" | ||
|
|
||
| # Check if keys are correctly merged from the list | ||
| assert "MagneticMultipoleP" in elem | ||
| assert elem["MagneticMultipoleP"] == [{"Kn3L": 0.3}] | ||
|
|
||
| assert "ApertureP" in elem | ||
| assert elem["ApertureP"] == {"x_limits": [-0.1, 0.1]} | ||
Oops, something went wrong.
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.
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.
What was the intention here? Why does this go through
Lattice.from_fileas opposed to going throughfunctions.load_file_to_dictas below (which would work here too - tested locally)? Could you comment on whether you wanted to expand theLattice.from_filefunction but haven't gotten to implement the feature in the PR yet? I think I'm missing some context, inteded use case, and current PR progress, to start working on completing this PR.