Changes between Version 4 and Version 5 of PyYAML
- Timestamp:
- 04/17/06 14:18:15 (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
PyYAML
v4 v5 113 113 114 114 You 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''': 115 either the function '''yaml.add_constructor''' or subclass from '''yaml.YAMLObject'''. 116 117 Instances of '''yaml.YAMLObject''' are automatically serialized to YAML and vice versa. 118 You only need to define the YAML tag with the '''yaml_tag''' variable. 120 119 {{{ 121 120 #!python 122 121 class Person(yaml.YAMLObject): 123 122 yaml_tag = '!Person' 124 @classmethod125 def from_yaml(cls, constructor, node):126 # Convert the node to a dictionary127 attributes = constructor.construct_mapping(node)128 # Convert spaces into underlines129 for key in attributes:130 if ' ' in key:131 value = attributes[key]132 del attributes[key]133 key = key.replace(' ', '_')134 attributes[key] = value135 # Create an object136 return cls(**attributes)137 @classmethod138 def to_yaml(cls, representer, person):139 # Create mapping node140 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)147 123 def __init__(self, first_name=None, last_name=None, email=None, birthday=None): 148 124 self.first_name = first_name … … 150 126 self.email = email 151 127 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 155 134 {{{ 156 135 #!python 157 136 >>> p = yaml.load(""" 158 137 ... !Person 159 ... first name: Kirill160 ... last name: Simonov138 ... first_name: Kirill 139 ... last_name: Simonov 161 140 ... email: xi(at)resolvent.net 141 ... birthday: none 162 142 ... """) 163 143 >>> 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) 144 Person(first_name='Kirill', last_name='Simonov', email='xi(at)resolvent.net', birthday='none') 167 145 >>> print yaml.dump(p) 168 146 !Person 169 first name: Kirill 170 last name: Simonov 147 last_name: Simonov 148 first_name: Kirill 171 149 email: xi(at)resolvent.net 150 birthday: none 172 151 }}} 173 152 … … 180 159 def represent_person(representer, person): 181 160 # ... 182 yaml. Constructor.add_constructor('!Person', construct_person)183 yaml. Representer.add_representer(Person, represent_person)184 }}} 185 186 === Loading all documents===161 yaml.add_constructor('!Person', construct_person) 162 yaml.add_representer(Person, represent_person) 163 }}} 164 165 === Parsing and emitting multiple documents in a stream === 187 166 188 167 If an input stream contains several documents, you may load all of them using the '''yaml.load_all''' function. … … 221 200 PyYAML 3000 provides low-level event-based and easy-to-use parser and emitter API. 222 201 223 Parser example:202 Example: 224 203 {{{ 225 204 #!python … … 239 218 ... """ 240 219 >>> for event in yaml.parse(data): print event 241 242 220 StreamStartEvent() 243 244 221 DocumentStartEvent() 245 ScalarEvent(anchor=None, tag=u'!tag', value=u'scalar')222 ScalarEvent(anchor=None, tag=u'!tag', implicit=(False, False), value=u'scalar') 246 223 DocumentEndEvent() 247 248 224 DocumentStartEvent() 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')225 SequenceStartEvent(anchor=None, tag=None, implicit=True) 226 ScalarEvent(anchor=u'anchor', tag=None, implicit=(True, False), value=u'item') 227 ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'another item') 252 228 AliasEvent(anchor=u'anchor') 253 229 SequenceEndEvent() 254 230 DocumentEndEvent() 255 256 231 DocumentStartEvent() 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')232 MappingStartEvent(anchor=None, tag=None, implicit=True) 233 ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'key') 234 ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'value') 235 SequenceStartEvent(anchor=None, tag=None, implicit=True) 236 ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'complex') 237 ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'key') 263 238 SequenceEndEvent() 264 SequenceStartEvent(anchor=None, tag=None )265 ScalarEvent(anchor=None, tag=None, value=u'complex')266 ScalarEvent(anchor=None, tag=None, value=u'value')239 SequenceStartEvent(anchor=None, tag=None, implicit=True) 240 ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'complex') 241 ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'value') 267 242 SequenceEndEvent() 268 243 MappingEndEvent() 269 244 DocumentEndEvent() 270 271 245 StreamEndEvent() 272 273 246 >>> events = [ 274 247 ... yaml.StreamStartEvent(encoding='utf-8'), 275 248 ... 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)), 281 254 ... 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)), 284 257 ... yaml.MappingEndEvent(), 285 258 ... yaml.DocumentEndEvent(explicit=True),
