Reputation: 1
I am trying to write python data class(es) to hold data in a yaml config file, using yaml.safe_load() to read-in the data. I'm finding that although I get all the data into my python classes (if I print the data it's all there), I run into problems accessing some of the data from my data class structure, indicating that my class structure is not correct.
Here is an excerpt of the yaml file:
parent_config: ../../../core/base_config.yaml
properties:
firmware: null
selenium: true
device: true
specs_path: apps/tests
config_path: apps/batch_tests
power_supply: # Define power supply to use for test setup
default_voltage: 4.1
queues:
- name: cloud_default
properties:
lib_manager:
device: true
- name: power_consumption
properties:
dut_preparation:
device: false
power_supply: # Define power supply to use for test setup
enabled: true
type: otii
multimeter:
enabled: true
type: otii
- name: power-thermal-test
properties:
dut_preparation:
device: false
power_supply: # Define power supply to use for test setup
enabled: false
multimeter:
enabled: true
type: scope
serial: ‘00001' # set joulescope serial to use
thermal:
enabled: true
ip_addr: ’00.00.01.1'
- name: build_acceptance
properties:
dut_preparation:
device: true
dut_requirements:
queue: 'build_acceptance'
testbeds:
- name: cloud
queue: cloud_default
properties: ~
- name: linux
queue: power_consumption
properties:
device_sn: ‘ABCDEF'
device_id: 110011
ql:
enabled: true
org_id: 112233
- name: 11-mac
queue: thermal-test
properties:
device_sn: ‘XYZSDW'
device_id: 22222666
ql:
enabled: true
org_id: 227755
- name: cloud
queue: build_acceptance
properties: ~
And here is my Python data class structure:
@dataclass
class PowerSupplyClass:
default_voltage: float
@dataclass
class PropertiesClass:
firmware: str
selenium: bool
device: bool
specs_path: str
config_path: str
power_supply: PowerSupplyClass
@dataclass
class Config:
parent_config: str
properties: PropertiesClass
queues: list
testbeds: list
Here's my code for loading from YAML file:
import yaml
from test_config2 import Config # This file contains my class structure above
from pprint import pprint
with open("theyaml.yaml", "r") as f:
config_data = yaml.safe_load(f)
props = Config(**config_data)
pprint(props)
pprint(props.parent_config)
pprint(props.properties)
pprint(props.properties.power_supply.default_voltage) # Here I get an error - AttributeError: 'dict' object has no attribute 'power_supply'
pprint(props.queues)
pprint(props.testbeds)
It seems that I can access elements that are not from nested classes, but when I try to access something that is in a class that's nested underneath another class, I get an error as noted above.
What am I doing wrong?
Also - how will I be able to access any desired element from the "queues" and "testbeds" lists?
I tried various ways of reconfiguring my class structure without success. I need to be able to access any element within the entire structure by simply using the dot operator and the correct sequence of object names, like the "props.properties.power_supply.default_voltage" syntax above.
Upvotes: 0
Views: 48