PyYAML parse into arbitary object
- by Philip Fourie
I have the following Python 2.6 program and YAML definition (using PyYAML):
import yaml
x = yaml.load(
"""
product:
name : 'Product X'
sku : 123
features :
- size : '10x30cm'
weight : '10kg'
"""
)
print type(x)
print x
Which results in the following output:
<type 'dict'>
{'product': {'sku': 123, 'name': 'Product X', 'features': [{'weight': '10kg', 'size': '10x30cm'}]}}
It is possible to create a strongly typed object from x?
I would like to the following:
print x.features(0).size
I am aware that it is possible to create and instance from an existent class, but that is not what I want for this particular scenario.