Changes between Version 4 and Version 5 of PyYAML


Ignore:
Timestamp:
04/17/06 14:18:15 (9 years ago)
Author:
xi
Comment:

Refrect recent changes.

Legend:

Unmodified
Added
Removed
Modified
  • PyYAML

    v4 v5  
    113113 
    114114You may define constructors for your own application-specific tags. You may use 
    115 either the function '''yaml.Constructor.add_constructor''' or subclass from 
    116 '''yaml.YAMLObject'''. 
    117  
    118 If you use '''yaml.YAMLObject''', you need to define the class attribute '''yaml_tag''' 
    119 and the class methods '''from_yaml''', '''to_yaml''': 
     115either the function '''yaml.add_constructor''' or subclass from '''yaml.YAMLObject'''. 
     116 
     117Instances of '''yaml.YAMLObject''' are automatically serialized to YAML and vice versa. 
     118You only need to define the YAML tag with the '''yaml_tag''' variable. 
    120119{{{ 
    121120#!python 
    122121class Person(yaml.YAMLObject): 
    123122    yaml_tag = '!Person' 
    124     @classmethod 
    125     def from_yaml(cls, constructor, node): 
    126         # Convert the node to a dictionary 
    127         attributes = constructor.construct_mapping(node) 
    128         # Convert spaces into underlines 
    129         for key in attributes: 
    130             if ' ' in key: 
    131                 value = attributes[key] 
    132                 del attributes[key] 
    133                 key = key.replace(' ', '_') 
    134                 attributes[key] = value 
    135         # Create an object 
    136         return cls(**attributes) 
    137     @classmethod 
    138     def to_yaml(cls, representer, person): 
    139         # Create mapping node 
    140         mapping = {} 
    141         for attribute in person.__dict__: 
    142             key = attribute.replace('_', ' ') 
    143             value = getattr(person, attribute) 
    144             if value is not None: 
    145                 mapping[key] = getattr(person, attribute) 
    146         return representer.represent_mapping(cls.yaml_tag, mapping) 
    147123    def __init__(self, first_name=None, last_name=None, email=None, birthday=None): 
    148124        self.first_name = first_name 
     
    150126        self.email = email 
    151127        self.birthday = birthday 
    152 }}} 
    153  
    154 After that, PyYAML 3000 will convert '''!Person'''-tagged nodes to '''Person''' objects and vice versa. 
     128    def __repr__(self): 
     129        return "%s(first_name=%r, last_name=%r, email=%r, birthday=%r)"  \ 
     130                % (self.__class__.__name__, self.first_name, self.last_name, 
     131                        self.email, self.birthday) 
     132}}} 
     133 
    155134{{{ 
    156135#!python 
    157136>>> p = yaml.load(""" 
    158137... !Person 
    159 ... first name: Kirill 
    160 ... last name: Simonov 
     138... first_name: Kirill 
     139... last_name: Simonov 
    161140... email: xi(at)resolvent.net 
     141... birthday: none 
    162142... """) 
    163143>>> print p 
    164 <__main__.Person object at 0xb7b5e44c> 
    165 >>> p.first_name, p.last_name, p.email, p.birthday 
    166 ('Kirill', 'Simonov', 'xi(at)resolvent.net', None) 
     144Person(first_name='Kirill', last_name='Simonov', email='xi(at)resolvent.net', birthday='none') 
    167145>>> print yaml.dump(p) 
    168146!Person 
    169 first name: Kirill 
    170 last name: Simonov 
     147last_name: Simonov 
     148first_name: Kirill 
    171149email: xi(at)resolvent.net 
     150birthday: none 
    172151}}} 
    173152 
     
    180159def represent_person(representer, person): 
    181160    # ... 
    182 yaml.Constructor.add_constructor('!Person', construct_person) 
    183 yaml.Representer.add_representer(Person, represent_person) 
    184 }}} 
    185  
    186 === Loading all documents === 
     161yaml.add_constructor('!Person', construct_person) 
     162yaml.add_representer(Person, represent_person) 
     163}}} 
     164 
     165=== Parsing and emitting multiple documents in a stream === 
    187166 
    188167If an input stream contains several documents, you may load all of them using the '''yaml.load_all''' function. 
     
    221200PyYAML 3000 provides low-level event-based and easy-to-use parser and emitter API. 
    222201 
    223 Parser example: 
     202Example: 
    224203{{{ 
    225204#!python 
     
    239218... """ 
    240219>>> for event in yaml.parse(data): print event 
    241  
    242220StreamStartEvent() 
    243  
    244221DocumentStartEvent() 
    245 ScalarEvent(anchor=None, tag=u'!tag', value=u'scalar') 
     222ScalarEvent(anchor=None, tag=u'!tag', implicit=(False, False), value=u'scalar') 
    246223DocumentEndEvent() 
    247  
    248224DocumentStartEvent() 
    249 SequenceStartEvent(anchor=None, tag=None) 
    250 ScalarEvent(anchor=u'anchor', tag=None, value=u'item') 
    251 ScalarEvent(anchor=None, tag=None, value=u'another item') 
     225SequenceStartEvent(anchor=None, tag=None, implicit=True) 
     226ScalarEvent(anchor=u'anchor', tag=None, implicit=(True, False), value=u'item') 
     227ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'another item') 
    252228AliasEvent(anchor=u'anchor') 
    253229SequenceEndEvent() 
    254230DocumentEndEvent() 
    255  
    256231DocumentStartEvent() 
    257 MappingStartEvent(anchor=None, tag=None) 
    258 ScalarEvent(anchor=None, tag=None, value=u'key') 
    259 ScalarEvent(anchor=None, tag=None, value=u'value') 
    260 SequenceStartEvent(anchor=None, tag=None) 
    261 ScalarEvent(anchor=None, tag=None, value=u'complex') 
    262 ScalarEvent(anchor=None, tag=None, value=u'key') 
     232MappingStartEvent(anchor=None, tag=None, implicit=True) 
     233ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'key') 
     234ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'value') 
     235SequenceStartEvent(anchor=None, tag=None, implicit=True) 
     236ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'complex') 
     237ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'key') 
    263238SequenceEndEvent() 
    264 SequenceStartEvent(anchor=None, tag=None) 
    265 ScalarEvent(anchor=None, tag=None, value=u'complex') 
    266 ScalarEvent(anchor=None, tag=None, value=u'value') 
     239SequenceStartEvent(anchor=None, tag=None, implicit=True) 
     240ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'complex') 
     241ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'value') 
    267242SequenceEndEvent() 
    268243MappingEndEvent() 
    269244DocumentEndEvent() 
    270  
    271245StreamEndEvent() 
    272  
    273246>>> events = [ 
    274247... yaml.StreamStartEvent(encoding='utf-8'), 
    275248... yaml.DocumentStartEvent(explicit=True), 
    276 ... yaml.MappingStartEvent(anchor=None, tag=None), 
    277 ... yaml.ScalarEvent(anchor=None, tag=None, value=u'flow sequence', implicit=True), 
    278 ... yaml.SequenceStartEvent(anchor=None, tag=None, flow_style=True), 
    279 ... yaml.ScalarEvent(anchor=None, tag=None, value=u'123', implicit=True), 
    280 ... yaml.ScalarEvent(anchor=None, tag=None, value=u'456', implicit=True), 
     249... yaml.MappingStartEvent(anchor=None, tag=None, implicit=True), 
     250... yaml.ScalarEvent(anchor=None, tag=None, value=u'flow sequence', implicit=(True, True)), 
     251... yaml.SequenceStartEvent(anchor=None, tag=None, flow_style=True, implicit=True), 
     252... yaml.ScalarEvent(anchor=None, tag=None, value=u'123', implicit=(True, False)), 
     253... yaml.ScalarEvent(anchor=None, tag=None, value=u'456', implicit=(True, False)), 
    281254... yaml.SequenceEndEvent(), 
    282 ... yaml.ScalarEvent(anchor=None, tag=None, value=u'block scalar', implicit=True), 
    283 ... yaml.ScalarEvent(anchor=None, tag=None, value=u'YAML\nis\nfun!\n', style='|'), 
     255... yaml.ScalarEvent(anchor=None, tag=None, value=u'block scalar', implicit=(True, True)), 
     256... yaml.ScalarEvent(anchor=None, tag=None, value=u'YAML\nis\nfun!\n', style='|', implicit=(True, True)), 
    284257... yaml.MappingEndEvent(), 
    285258... yaml.DocumentEndEvent(explicit=True),