Skip to content Skip to sidebar Skip to footer

Pyyaml - How To Deal With Composition

I've been trying to use YAML as I love the readability of it. However, I'm a bit stumped at the best way to put in components/deal with composition. Let's say I have this as a clas

Solution 1:

Assuming that you have proper constructors and representers to create the objects, you can do:

-!AggresiveAI&basicai-!Fighter&fightercomponentstrength:10dexterity:5death:dramaticdeath-!Objectname:orchp:100fighter:*fightercomponentai:*basicai

The only thing problematic is your function call to dramaticdeath as YAML stores objects and not function calls. So make that a lookup from the string to the function in the __init__ method of class Fighter

The toplevel doesn't have to be a list, you can e.g. make the toplevel a mapping. Just make sure your anchors are defined before using them in aliases.

Post a Comment for "Pyyaml - How To Deal With Composition"