Changes between Version 25 and Version 26 of PyYAMLDocumentation


Ignore:
Timestamp:
05/12/06 05:06:16 (9 years ago)
Author:
xi
Comment:

Make the Dice example less confusing; thanks ingy for suggestion.

Legend:

Unmodified
Added
Removed
Modified
  • PyYAMLDocumentation

    v25 v26  
    291291...         return tuple.__new__(cls, [a, b]) 
    292292...     def __repr__(self): 
    293 ...         return "%sd%s" % self 
     293...         return "Dice(%s,%s)" % self 
    294294 
    295295>>> print Dice(3,6) 
     296Dice(3,6) 
     297}}} 
     298 
     299The default representation for '''`Dice`''' objects is not nice: 
     300{{{ 
     301#!python 
     302>>> print yaml.dump(Dice(3,6)) 
     303 
     304!!python/object/new:__main__.Dice 
     305- !!python/tuple [3, 6] 
     306}}} 
     307 
     308Suppose you want a '''`Dice`''' object to represented as '''`AdB`''' in YAML: 
     309{{{ 
     310#!python 
     311>>> print yaml.dump(Dice(3,6)) 
     312 
    2963133d6 
    297314}}} 
     
    302319#!python 
    303320>>> def dice_representer(dumper, data): 
    304 ...     return dumper.represent_scalar(u'!dice', unicode(data)) 
     321...     return dumper.represent_scalar(u'!dice', u'%sd%s' % data) 
    305322 
    306323>>> yaml.add_representer(Dice, dice_representer) 
     
    332349... """) 
    333350 
    334 {'initial hit points': 8d4} 
     351{'initial hit points': Dice(8,4)} 
    335352}}} 
    336353 
     
    356373... """) 
    357374 
    358 {'damage': 5d10} 
     375{'damage': Dice(5,10)} 
    359376}}} 
    360377 
     
    470487- Ring of Teleportation 
    471488- Ring of Speed 
     489 
    472490right hand: 
    473491- Ring of Resist Fire