Created
August 4, 2016 08:19
-
-
Save lanfon72/7c7b7144551841a1895207157e42159d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import yaml | |
| class Monster(yaml.YAMLObject): | |
| yaml_tag = u'!Monster' | |
| def __init__(self, name, hp, ac, attacks): | |
| self.name = name | |
| self.hp = hp | |
| self.ac = ac | |
| self.attacks = attacks | |
| def __repr__(self): | |
| return "%s(name=%r, hp=%r, ac=%r, attacks=%r)" % ( | |
| self.__class__.__name__, self.name, self.hp, self.ac, self.attacks) | |
| class Animal(yaml.YAMLObject): | |
| yaml_tag = u'!Animal' | |
| def __init__(self, **kwargs): | |
| self.mapping = kwargs | |
| def __repr__(self): | |
| return "{}(name={name}, hp={hp}, ac={ac}, attacks={attacks})".format(self.__class__.__name__, **self.__dict__) | |
| animal = yaml.load(""" | |
| --- !Animal | |
| name: Cave spider | |
| hp: [2,6] # 2d6 | |
| ac: 16 | |
| attacks: [BITE, HURT] | |
| """) | |
| monster = yaml.load(""" | |
| --- !Monster | |
| name: Cave spider | |
| hp: [2,6] # 2d6 | |
| ac: 16 | |
| attacks: [BITE, HURT] | |
| """) | |
| print(yaml.dump(Monster(name='Cave lizard', hp=[3, 6], ac=16, attacks=['BITE', 'HURT']))) | |
| print("animal: %s " % animal.__dict__) | |
| print("monster: %s " % monster.__dict__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment