Re: [Q] How to compose, serialize, and emit in PyYAML?
"Makoto Kuwata" <[email protected]>
| Newsgroups | gmane.text.yaml.general |
|---|---|
| Message-ID | <[email protected]> |
Thank you, Kirill.
I'm playing with PyYAML and I love it so much.
Today I wrote the following script which changes YAML tag
from '!!python/object:__main__.Color' to '!color'.
This is very useful for data exchange between Python and
other language.
--------------------
import yaml
def yaml_register_class(klass, ytag):
suffix = '%s.%s' % (klass.__module__, klass.__name__)
f1 = lambda dumper, obj: dumper.represent_mapping(ytag, obj.__dict__)
f2 = lambda loader, node: loader.construct_python_object(suffix, node)
yaml.add_representer(klass, f1)
yaml.add_constructor(ytag, f2)
class Color(object):
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
yaml_register_class(Color, '!color')
obj = Color(256, 128, 0)
print yaml.dump(obj) #=> !color {b: 0, g: 128, r: 256}
s = '!color {b: 0, g: 128, r: 256}'
obj = yaml.load(s)
print obj #=> <__main__.Color object at 0x5be510>
print obj.__dict__ #=> {'r': 256, 'b': 0, 'g': 128}
--------------------
PyYAML is great.
--
regards,
makoto kuwata
On Jan 8, 2008 8:01 PM, Kirill Simonov <[email protected]> wrote:
> Makoto Kuwata wrote:
> > Hi,
> > I'm investigating PyYAML 3.05 and have some questions.
> >
> > Q1. How to compose nodes from event list?
> > Q2. How to serialize node into event list?
> > Q3. How to emit event list into stram?
>
> PyYAML has no direct support for Q1 and Q2. For Q3, you may use:
>
> yaml.emit(events)
>
> Basically, PyYAML only supports serializing events/nodes/objects to the
> output YAML stream and deserializing tokens/events/nodes/objects from
> the input YAML stream. However it is relatively easy to write
> nodes->events and events->nodes converters using existing PyYAML
> infrastructure.
>
> For Q1 (events to nodes):
>
> class EventsToNodes(yaml.composer.Composer, yaml.resolver.Resolver):
>
> def __init__(self, events):
> super(EventsToNodes, self).__init__()
> if isinstance(events, list):
> events = iter(events)
> self.events = events
> self.current_event = None
>
> def check_event(self, *choices):
> if self.current_event is None:
> self.current_event = self.events.next()
> if not choices:
> return True
> for choice in choices:
> if isinstance(self.current_event, choice):
> return True
> return False
>
> def peek_event(self):
> if self.current_event is None:
> self.current_event = self.events.next()
> return self.current_event
>
> def get_event(self):
> if self.current_event is None:
> self.current_event = self.events.next()
> value = self.current_event
> self.current_event = None
> return value
>
> node = yaml.compose(events, Loader=EventsToNodes)
> print node
>
> For Q2 (nodes to events):
>
> class NodesToEvents(yaml.serializer.Serializer, yaml.resolver.Resolver):
>
> def __init__(self, events, **parameters):
> super(NodesToEvents, self).__init__()
> self.events = events
>
> def emit(self, event):
> self.events.append(event)
>
> events = []
> yaml.serialize(node, events, Dumper=NodesToEvents)
> print events
>
>
> Thanks,
> Kirill
>
>
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace