Skip to content

Commit 82a2486

Browse files
committed
Start draft of new Lattice class
- Copy implementation from BeamLine to Lattice - Move methods from_file, to_file from BeamLine to Lattice - Use new class in external examples test script
1 parent 22c0f45 commit 82a2486

4 files changed

Lines changed: 42 additions & 14 deletions

File tree

examples/test_external_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22

3-
from pals import BeamLine
3+
from pals import Lattice
44

55

66
def main():
@@ -14,7 +14,7 @@ def main():
1414
args = parser.parse_args()
1515
example_file = args.path
1616
# Parse and validate YAML data from file
17-
BeamLine.from_file(example_file)
17+
Lattice.from_file(example_file)
1818

1919

2020
if __name__ == "__main__":

src/pals/kinds/BeamLine.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from .all_elements import get_all_elements_as_annotation
55
from .mixin import BaseElement
6-
from ..functions import load_file_to_dict, store_dict_to_file
76

87

98
class BeamLine(BaseElement):
@@ -26,14 +25,3 @@ def model_dump(self, *args, **kwargs):
2625
from pals.kinds.mixin.all_element_mixin import dump_element_list
2726

2827
return dump_element_list(self, "line", *args, **kwargs)
29-
30-
@staticmethod
31-
def from_file(filename: str) -> "BeamLine":
32-
"""Load a BeamLine from a text file"""
33-
pals_dict = load_file_to_dict(filename)
34-
return BeamLine(**pals_dict)
35-
36-
def to_file(self, filename: str):
37-
"""Save a BeamLine to a text file"""
38-
pals_dict = self.model_dump()
39-
store_dict_to_file(filename, pals_dict)

src/pals/kinds/Lattice.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from pydantic import model_validator
2+
from typing import List, Literal
3+
4+
from .all_elements import get_all_elements_as_annotation
5+
from .mixin import BaseElement
6+
from ..functions import load_file_to_dict, store_dict_to_file
7+
8+
9+
class Lattice(BaseElement):
10+
"""A line of elements and/or other lines"""
11+
12+
kind: Literal["Lattice"] = "Lattice"
13+
14+
line: List[get_all_elements_as_annotation()]
15+
16+
@model_validator(mode="before")
17+
@classmethod
18+
def unpack_json_structure(cls, data):
19+
"""Deserialize the JSON/YAML/...-like dict for Lattice elements"""
20+
from pals.kinds.mixin.all_element_mixin import unpack_element_list_structure
21+
22+
return unpack_element_list_structure(data, "line", "line")
23+
24+
def model_dump(self, *args, **kwargs):
25+
"""Custom model dump for Lattice to handle element list formatting"""
26+
from pals.kinds.mixin.all_element_mixin import dump_element_list
27+
28+
return dump_element_list(self, "line", *args, **kwargs)
29+
30+
@staticmethod
31+
def from_file(filename: str) -> "Lattice":
32+
"""Load a Lattice from a text file"""
33+
pals_dict = load_file_to_dict(filename)
34+
return Lattice(**pals_dict)
35+
36+
def to_file(self, filename: str):
37+
"""Save a Lattice to a text file"""
38+
pals_dict = self.model_dump()
39+
store_dict_to_file(filename, pals_dict)

src/pals/kinds/all_elements.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
def get_all_element_types(extra_types: tuple = None):
4444
"""Return a tuple of all element types that can be used in BeamLine or UnionEle."""
4545
element_types = (
46+
"Lattice", # Forward reference to handle circular import
4647
"BeamLine", # Forward reference to handle circular import
4748
"UnionEle", # Forward reference to handle circular import
4849
ACKicker,

0 commit comments

Comments
 (0)