Changes between Version 23 and Version 24 of PyYAMLDocumentation
- Timestamp:
- 05/07/06 10:01:32 (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
PyYAMLDocumentation
v23 v24 1 1 = PyYAML Documentation = 2 3 ''work in progress''4 2 5 3 ''RPG-ish descriptions are stolen from [http://www.thangorodrim.net/ the Angband rogue-like game]. … … 195 193 ... self.__class__.__name__, self.name, self.hp, self.sp) 196 194 197 >>> print yaml.dump( ("Galain Ysseleg", hp=-3, sp=2))195 >>> print yaml.dump(Hero("Galain Ysseleg", hp=-3, sp=2)) 198 196 199 197 !!python/object:__main__.Hero {hp: -3, name: Galain Ysseleg, sp: 2} … … 244 242 to define a subclass of '''`yaml.YAMLObject`''': 245 243 {{{ 244 >>> class Monster(yaml.YAMLObject): 245 ... yaml_tag = u'!Monster' 246 ... def __init__(self, name, hp, ac, attacks): 247 ... self.name = name 248 ... self.hp = hp 249 ... self.ac = ac 250 ... self.attacks = attacks 251 ... def __repr__(self): 252 ... return "%s(name=%r, hp=%r, ac=%r, attacks=%r)" % ( 253 ... self.__class__.__name__, self.name, self.hp, self.ac, self.attacks) 254 }}} 255 256 The above definition is enough to automatically load and dump `Monster` objects: 257 {{{ 246 258 #!python 247 259 >>> yaml.load(""" … … 285 297 }}} 286 298 287 First we define a representer ,that convert a dice object to scalar node299 First we define a representer that convert a dice object to scalar node 288 300 with the tag `!dice` and register it. 289 301 {{{ … … 320 332 ... """) 321 333 322 {'initial hit points': 8d4 334 {'initial hit points': 8d4} 323 335 }}} 324 336 … … 769 781 '''Warning: API stability is not guaranteed!''' 770 782 783 771 784 === The yaml package === 772 785 … … 943 956 Paths elements can be string values, integers, or `None`. The `kind` of a node can 944 957 be `str`, `list`, `dict`, or `None`. 958 945 959 946 960 === YAMLError === … … 1440 1454 === YAMLObject === 1441 1455 1456 {{{ 1457 #!python 1458 class MyYAMLObject(YAMLObject): 1459 yaml_loader = Loader 1460 yaml_dumper = Dumper 1461 1462 yaml_tag = u'...' 1463 yaml_flow_style = ... 1464 1465 @classmethod 1466 def from_yaml(cls, loader, node): 1467 # ... 1468 return data 1469 1470 @classmethod 1471 def to_yaml(cls, dumper, data): 1472 # ... 1473 return node 1474 }}} 1475 1476 Subclassing '''`YAMLObject`''' is an easy way to define tags, constructors, and representers 1477 for your classes. You only need to override the `yaml_tag` attribute. If you want 1478 to define your custom constructor and representer, redefine the `from_yaml` and `to_yaml` method 1479 correspondingly. 1442 1480 1443 1481
