forked from ax3l/pydantic-mad
-
Notifications
You must be signed in to change notification settings - Fork 3
PALS Root Structure #63
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
Merged
Merged
Changes from all commits
Commits
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,65 @@ | ||
| from pydantic import BaseModel | ||
|
|
||
| from pydantic import model_validator | ||
| from typing import List, Optional | ||
|
|
||
| from .kinds import Lattice | ||
| from .kinds.all_elements import get_all_elements_as_annotation | ||
| from .functions import load_file_to_dict, store_dict_to_file | ||
|
|
||
|
|
||
| Facility = List[get_all_elements_as_annotation()] | ||
|
|
||
|
|
||
| class PALSroot(BaseModel): | ||
| """Represent the roo PALS structure""" | ||
|
|
||
| version: Optional[str] = None | ||
|
|
||
| facility: Facility | ||
|
|
||
| @model_validator(mode="before") | ||
| @classmethod | ||
| def unpack_json_structure(cls, data): | ||
| """Deserialize the JSON/YAML/...-like dict for facility elements""" | ||
| from pals.kinds.mixin.all_element_mixin import unpack_element_list_structure | ||
|
|
||
| return unpack_element_list_structure(data, "facility", "facility") | ||
|
|
||
| def model_dump(self, *args, **kwargs): | ||
| """Custom model dump for facility to handle element list formatting""" | ||
| from pals.kinds.mixin.all_element_mixin import dump_element_list | ||
|
|
||
| data = {} | ||
| data["PALS"] = {} | ||
| data["PALS"]["version"] = self.version | ||
| data["PALS"] = dump_element_list(self, "facility", *args, **kwargs) | ||
| return data | ||
|
|
||
| @staticmethod | ||
| def from_file(filename: str) -> "PALSroot": | ||
| """Load a facility from a text file""" | ||
| pals_dict = load_file_to_dict(filename) | ||
| return PALSroot(**pals_dict) | ||
|
|
||
| def to_file(self, filename: str): | ||
| """Save a facility to a text file""" | ||
| pals_dict = self.model_dump() | ||
| store_dict_to_file(filename, pals_dict) | ||
|
|
||
|
|
||
| def load(filename: str) -> PALSroot: | ||
| """Load a facility from a text file""" | ||
| pals_dict = load_file_to_dict(filename) | ||
| return PALSroot(**pals_dict) | ||
|
|
||
|
|
||
| def store(filename: str, pals_root: PALSroot | Facility | Lattice): | ||
| # wrap single elements in a list, facility in a PALSroot | ||
| if isinstance(pals_root, Lattice): | ||
| pals_root = PALSroot(version=None, facility=[pals_root]) | ||
| elif isinstance(pals_root, list): | ||
| pals_root = PALSroot(version=None, facility=pals_root) | ||
|
|
||
| pals_dict = pals_root.model_dump() | ||
| store_dict_to_file(filename, pals_dict) | ||
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
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.
With the latest updates, I think we have
unpack_json_structuredefined forPALSBeamLineLatticeUnionEleDo you know if all of these classes still need that method? Or can we remove it from some now?
I think a similar question applies to other methods, e.g.,
from_fileandto_file(defined forPALS,BeamLine,Lattice).