1- import json
21import os
3- import yaml
42
53import pals
64
@@ -13,17 +11,10 @@ def test_yaml():
1311 # Create line with both elements
1412 line = pals .BeamLine (name = "line" , line = [element1 , element2 ])
1513 # Serialize the BeamLine object to YAML
16- yaml_data = yaml .dump (line .model_dump (), default_flow_style = False )
17- print (f"\n { yaml_data } " )
18- # Write the YAML data to a test file
19- test_file = "line.yaml"
20- with open (test_file , "w" ) as file :
21- file .write (yaml_data )
14+ test_file = "line.pals.yaml"
15+ line .to_file (test_file )
2216 # Read the YAML data from the test file
23- with open (test_file , "r" ) as file :
24- yaml_data = yaml .safe_load (file )
25- # Parse the YAML data back into a BeamLine object
26- loaded_line = pals .BeamLine (** yaml_data )
17+ loaded_line = pals .BeamLine .from_file (test_file )
2718 # Remove the test file
2819 os .remove (test_file )
2920 # Validate loaded BeamLine object
@@ -38,17 +29,10 @@ def test_json():
3829 # Create line with both elements
3930 line = pals .BeamLine (name = "line" , line = [element1 , element2 ])
4031 # Serialize the BeamLine object to JSON
41- json_data = json .dumps (line .model_dump (), sort_keys = True , indent = 2 )
42- print (f"\n { json_data } " )
43- # Write the JSON data to a test file
44- test_file = "line.json"
45- with open (test_file , "w" ) as file :
46- file .write (json_data )
32+ test_file = "line.pals.json"
33+ line .to_file (test_file )
4734 # Read the JSON data from the test file
48- with open (test_file , "r" ) as file :
49- json_data = json .loads (file .read ())
50- # Parse the JSON data back into a BeamLine object
51- loaded_line = pals .BeamLine (** json_data )
35+ loaded_line = pals .BeamLine .from_file (test_file )
5236 # Remove the test file
5337 os .remove (test_file )
5438 # Validate loaded BeamLine object
@@ -224,21 +208,16 @@ def test_comprehensive_lattice():
224208 ],
225209 )
226210
227- # Test serialization to YAML
228- yaml_data = yaml .dump (lattice .model_dump (), default_flow_style = False )
229- print (f"\n Comprehensive lattice YAML:\n { yaml_data } " )
230-
231211 # Write to temporary file
232- yaml_file = "comprehensive_lattice.yaml"
233- with open (yaml_file , "w" ) as file :
234- file .write (yaml_data )
212+ yaml_file = "comprehensive_lattice.pals.yaml"
213+ lattice .to_file (yaml_file )
235214
236215 # Read back from file
237216 with open (yaml_file , "r" ) as file :
238- loaded_yaml_data = yaml . safe_load ( file )
217+ print ( f" \n Comprehensive lattice YAML: \n { file . read () } " )
239218
240219 # Deserialize back to Python object using Pydantic model logic
241- loaded_lattice = pals .BeamLine ( ** loaded_yaml_data )
220+ loaded_lattice = pals .BeamLine . from_file ( yaml_file )
242221
243222 # Verify the loaded lattice has the correct structure and parameter groups
244223 assert len (loaded_lattice .line ) == 31 # Should have 31 elements
@@ -284,21 +263,16 @@ def test_comprehensive_lattice():
284263 assert unionele_loaded .elements [1 ].kind == "Drift"
285264 assert unionele_loaded .elements [1 ].length == 0.1
286265
287- # Test serialization to JSON
288- json_data = json .dumps (lattice .model_dump (), sort_keys = True , indent = 2 )
289- print (f"\n Comprehensive lattice JSON:\n { json_data } " )
290-
291266 # Write to temporary file
292- json_file = "comprehensive_lattice.json"
293- with open (json_file , "w" ) as file :
294- file .write (json_data )
267+ json_file = "comprehensive_lattice.pals.json"
268+ lattice .to_file (json_file )
295269
296270 # Read back from file
297271 with open (json_file , "r" ) as file :
298- loaded_json_data = json . loads ( file .read ())
272+ print ( f" \n Comprehensive lattice JSON: \n { file .read ()} " )
299273
300274 # Deserialize back to Python object using Pydantic model logic
301- loaded_lattice_json = pals .BeamLine ( ** loaded_json_data )
275+ loaded_lattice_json = pals .BeamLine . from_file ( json_file )
302276
303277 # Verify the loaded lattice has the correct structure and parameter groups
304278 assert len (loaded_lattice_json .line ) == 31 # Should have 31 elements
0 commit comments