source: pyyaml/trunk/lib/yaml/nodes.py @ 222

Revision 222, 1.4 KB checked in by xi, 8 years ago (diff)

Subclass all base classes from object.

Hold references to the objects being represented (should fix #22).

The value of a mapping node is represented as a list of pairs (key, value)
now.

Sort dictionary items (fix #23).

Recursive structures are now loaded and dumped correctly, including complex
structures like recursive tuples (fix #5). Thanks Peter Murphy for the patches.
To make it possible, representer functions are allowed to be generators.
In this case, the first generated value is an object. Other values produced
by the representer are ignored.

Make Representer not try to guess !!pairs when a list is represented.
You need to construct a !!pairs node explicitly now.

Do not check for duplicate mapping keys as it didn't work correctly anyway.

RevLine 
[53]1
[222]2class Node(object):
[116]3    def __init__(self, tag, value, start_mark, end_mark):
[53]4        self.tag = tag
5        self.value = value
[116]6        self.start_mark = start_mark
7        self.end_mark = end_mark
[53]8    def __repr__(self):
9        value = self.value
[153]10        #if isinstance(value, list):
11        #    if len(value) == 0:
12        #        value = '<empty>'
13        #    elif len(value) == 1:
14        #        value = '<1 item>'
15        #    else:
16        #        value = '<%d items>' % len(value)
17        #else:
18        #    if len(value) > 75:
19        #        value = repr(value[:70]+u' ... ')
20        #    else:
21        #        value = repr(value)
22        value = repr(value)
[53]23        return '%s(tag=%r, value=%s)' % (self.__class__.__name__, self.tag, value)
24
25class ScalarNode(Node):
[55]26    id = 'scalar'
[136]27    def __init__(self, tag, value,
[133]28            start_mark=None, end_mark=None, style=None):
[132]29        self.tag = tag
30        self.value = value
31        self.start_mark = start_mark
32        self.end_mark = end_mark
[133]33        self.style = style
[53]34
35class CollectionNode(Node):
[133]36    def __init__(self, tag, value,
37            start_mark=None, end_mark=None, flow_style=None):
38        self.tag = tag
39        self.value = value
40        self.start_mark = start_mark
41        self.end_mark = end_mark
42        self.flow_style = flow_style
[53]43
44class SequenceNode(CollectionNode):
[55]45    id = 'sequence'
[53]46
47class MappingNode(CollectionNode):
[55]48    id = 'mapping'
[53]49
Note: See TracBrowser for help on using the repository browser.