Changes between Version 4 and Version 5 of PyYAMLDocumentation


Ignore:
Timestamp:
04/23/06 10:53:49 (9 years ago)
Author:
xi
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PyYAMLDocumentation

    v4 v5  
    4242by checking the '''BOM''' (byte order mark) sequence at the beginning of the 
    4343string/file. If no '''BOM''' is present, the '''utf-8''' encoding is assumed. 
     44 
     45'''`yaml.load`''' returns a Python object. 
     46 
    4447{{{ 
    4548#!python 
     
    134137 
    135138=== dump() === 
     139 
     140The '''`yaml.dump`''' function accepts a Python object and produces a YAML document. 
     141{{{ 
     142#!python 
     143>>> print yaml.dump({'name': 'Silenthand Olleander', 'race': 'Human', 
     144... 'traits': ['ONE_HAND', 'ONE_EYE']}) 
     145 
     146name: Silenthand Olleander 
     147race: Human 
     148traits: [ONE_HAND, ONE_EYE] 
     149}}} 
     150 
     151'''`yaml.dump`''' accepts the second optional argument, which must be an open file. 
     152In this case, '''`yaml.dump`''' will write the produced YAML document into the file. 
     153Otherwise, '''`yaml.dump`''' returns the produced document. 
     154 
     155{{{ 
     156#!python 
     157>>> stream = file('document.yaml', 'w') 
     158>>> yaml.dump(data, stream)    # Write a YAML representation of data to 'document.yaml'. 
     159>>> print yaml.dump(data)      # Output the document to the screen. 
     160}}} 
     161 
     162If you need to dump several YAML documents to a single stream, use the function 
     163'''`yaml.dump_all`'''. '''`yaml.dump_all`''' accepts a list or a generator producing 
     164Python objects to be serialized into a YAML document. The second optional argument is 
     165an open file. 
     166 
     167{{{ 
     168#!python 
     169>>> print yaml.dump([1,2,3], explicit_start=True) 
     170--- [1, 2, 3] 
     171 
     172>>> print yaml.dump_all([1,2,3], explicit_start=True) 
     173--- 1 
     174--- 2 
     175--- 3 
     176}}} 
     177 
     178You may even dump instances of Python classes. 
     179 
     180{{{ 
     181#!python 
     182>>> class Hero: 
     183...     def __init__(self, name, hp, sp): 
     184...         self.name = name 
     185...         self.hp = hp 
     186...         self.sp = sp 
     187...     def __repr__(self): 
     188...         return "%s(name=%r, hp=%r, sp=%r)" % ( 
     189...             self.__class__.__name__, self.name, self.hp, self.sp) 
     190 
     191>>> print yaml.dump(("Galain Ysseleg", hp=-3, sp=2)) 
     192 
     193!!python/object:__main__.Hero {hp: -3, name: Galain Ysseleg, sp: 2} 
     194}}} 
     195 
     196'''`yaml.dump`''' supports a number of keyword arguments that specify 
     197formatting details for the emitter. For instance, you may set the 
     198preferred intendation and width or use the canonical YAML format. 
     199{{{ 
     200#!python 
     201>>> print yaml.dump(range(50)) 
     202 
     203[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
     204  23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 
     205  43, 44, 45, 46, 47, 48, 49] 
     206 
     207>>> print yaml.dump(range(50), width=50, indent=4) 
     208 
     209[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
     210    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 
     211    28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
     212    40, 41, 42, 43, 44, 45, 46, 47, 48, 49] 
     213 
     214>>> print yaml.dump(range(5), canonical=True) 
     215 
     216--- 
     217!!seq [ 
     218  !!int "0", 
     219  !!int "1", 
     220  !!int "2", 
     221  !!int "3", 
     222  !!int "4", 
     223] 
     224}}} 
    136225 
    137226== YAML syntax ==