forked from ax3l/pydantic-mad
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLattice.py
More file actions
40 lines (29 loc) · 1.34 KB
/
Lattice.py
File metadata and controls
40 lines (29 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from pydantic import model_validator
from typing import List, Literal, Union
from .BeamLine import BeamLine
from .PlaceholderName import PlaceholderName
from .mixin import BaseElement
from ..functions import load_file_to_dict, store_dict_to_file
class Lattice(BaseElement):
"""A lattice combines beamlines"""
kind: Literal["Lattice"] = "Lattice"
branches: List[Union[BeamLine, PlaceholderName]]
@model_validator(mode="before")
@classmethod
def unpack_json_structure(cls, data):
"""Deserialize the JSON/YAML/...-like dict for Lattice elements"""
from pals.kinds.mixin.all_element_mixin import unpack_element_list_structure
return unpack_element_list_structure(data, "branches", "branches")
def model_dump(self, *args, **kwargs):
"""Custom model dump for Lattice to handle element list formatting"""
from pals.kinds.mixin.all_element_mixin import dump_element_list
return dump_element_list(self, "branches", *args, **kwargs)
@staticmethod
def from_file(filename: str) -> "Lattice":
"""Load a Lattice from a text file"""
pals_dict = load_file_to_dict(filename)
return Lattice(**pals_dict)
def to_file(self, filename: str):
"""Save a Lattice to a text file"""
pals_dict = self.model_dump()
store_dict_to_file(filename, pals_dict)