Changes between Version 23 and Version 24 of PyYAMLDocumentation


Ignore:
Timestamp:
05/07/06 10:01:32 (9 years ago)
Author:
xi
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PyYAMLDocumentation

    v23 v24  
    11= PyYAML Documentation = 
    2  
    3 ''work in progress'' 
    42 
    53''RPG-ish descriptions are stolen from [http://www.thangorodrim.net/ the Angband rogue-like game]. 
     
    195193...             self.__class__.__name__, self.name, self.hp, self.sp) 
    196194 
    197 >>> print yaml.dump(("Galain Ysseleg", hp=-3, sp=2)) 
     195>>> print yaml.dump(Hero("Galain Ysseleg", hp=-3, sp=2)) 
    198196 
    199197!!python/object:__main__.Hero {hp: -3, name: Galain Ysseleg, sp: 2} 
     
    244242to define a subclass of '''`yaml.YAMLObject`''': 
    245243{{{ 
     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 
     256The above definition is enough to automatically load and dump `Monster` objects: 
     257{{{ 
    246258#!python 
    247259>>> yaml.load(""" 
     
    285297}}} 
    286298 
    287 First we define a representer, that convert a dice object to scalar node 
     299First we define a representer that convert a dice object to scalar node 
    288300with the tag `!dice` and register it. 
    289301{{{ 
     
    320332... """) 
    321333 
    322 {'initial hit points': 8d4 
     334{'initial hit points': 8d4} 
    323335}}} 
    324336 
     
    769781'''Warning: API stability is not guaranteed!''' 
    770782 
     783 
    771784=== The yaml package === 
    772785 
     
    943956Paths elements can be string values, integers, or `None`. The `kind` of a node can 
    944957be `str`, `list`, `dict`, or `None`. 
     958 
    945959 
    946960=== YAMLError === 
     
    14401454=== YAMLObject === 
    14411455 
     1456{{{ 
     1457#!python 
     1458class 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 
     1476Subclassing '''`YAMLObject`''' is an easy way to define tags, constructors, and representers 
     1477for your classes. You only need to override the `yaml_tag` attribute. If you want 
     1478to define your custom constructor and representer, redefine the `from_yaml` and `to_yaml` method 
     1479correspondingly. 
    14421480 
    14431481