lets try the true pyyaml syntax for objects
myyaml.py:
import yaml,sys
class StateMachine(object):
pass
class State(object):
pass
class Connection(object):
pass
if __name__ == '__main__':
o = yaml.load("""
!!python/object:myyaml.StateMachine {
states: [
!!python/object:myyaml.State { name: p1 },
!!python/object:myyaml.State { name: p2 },
!!python/object:myyaml.State { name: p3 },],
connections:
[ !!python/object:myyaml.Connection { 'pim' : [p1,p2]}]}
""")
print o.states[0].name
print o.states[1].name
print o.connections[0].pim
sys.exit(0)
Gets:
p1
p2
['p1', 'p2']
And never try the yaml.load() in module's root block, always use if __name__ == '__main__'
or call it within a function that you ensure it will be called once.
please note that yaml statement:
!!python/object:myyaml.State { name: p1 },
At this point yaml tries to import the myyaml.py again, in another context, and the all codes in the module's root will be executed, if you put yaml.load or anything like this in module's root, you may meet an infinite loop, or an unexpected result .