slaclab / lcls-tools

Python tools for LCLS: post processing data, PV handling, pulling archive settings, etc.
Apache License 2.0
2 stars 21 forks source link

Convert magnet to use pydantic model #104

Closed MattKing06 closed 11 months ago

MattKing06 commented 11 months ago

Summary of Changes

Device and Magnet classes were originally constructed via dictionary loaded from yaml files. The majority of initialisation code was associated with type-checking, and ensuring that the correct information was defined inside the dictionary passed to the constructor.

It was found that Pydantic models can achieve the same effect and do better at type-enforcement/field-enforcement as well as simplifying the code that defines the class architecture for our devices.

Tests

The user-facing side of these changes now allow us to write code for interfacing with devices like below:

from lcls_tools.common.devices.magnet.reader import create_magnet

def take_data():
  print('taking some data...')

gunb_magnets = create_magnet(area='GUNB')
settings = [{'SOL1B' : 0.1, 'SOL2B' : 0.2}, {'SOL1B' : 0.15, 'SOL2B' : 0.25}, {'SOL1B' : 0.2, 'SOL2B' : 0.3}] 
gunb_magnets.scan(scan_settings=settings, function=take_data)

closes #100