11 from sets
import Set
as set
15 class MyLoader(Loader
):
17 class MyDumper(Dumper
):
20 class MyTestClass1(object):
22 def __init__(self
, x
, y
=0, z
=0):
27 def __eq__(self
, other
):
28 if isinstance(other
, MyTestClass1
):
29 return self
.__class
__, self
.__dict
__ == other
.__class
__, other
.__dict
__
33 def construct1(constructor
, node
):
34 mapping
= constructor
.construct_mapping(node
)
35 return MyTestClass1(**mapping
)
36 def represent1(representer
, native
):
37 return representer
.represent_mapping("!tag1", native
.__dict
__)
39 class MyTestClass2(MyTestClass1
, YAMLObject
):
41 yaml_loader
= MyLoader
42 yaml_dumper
= MyDumper
45 def from_yaml(cls
, constructor
, node
):
46 x
= constructor
.construct_yaml_int(node
)
48 from_yaml
= classmethod(from_yaml
)
50 def to_yaml(cls
, representer
, native
):
51 return representer
.represent_scalar(cls
.yaml_tag
, str(native
.x
))
52 to_yaml
= classmethod(to_yaml
)
54 class MyTestClass3(MyTestClass2
):
58 def from_yaml(cls
, constructor
, node
):
59 mapping
= constructor
.construct_mapping(node
)
65 from_yaml
= classmethod(from_yaml
)
67 def to_yaml(cls
, representer
, native
):
68 return representer
.represent_mapping(cls
.yaml_tag
, native
.__dict
__)
69 to_yaml
= classmethod(to_yaml
)
71 MyLoader
.add_constructor("!tag1", construct1
)
72 MyDumper
.add_representer(MyTestClass1
, represent1
)
74 class YAMLObject1(YAMLObject
):
75 yaml_loader
= MyLoader
76 yaml_dumper
= MyDumper
78 yaml_flow_style
= True
80 def __init__(self
, my_parameter
=None, my_another_parameter
=None):
81 self
.my_parameter
= my_parameter
82 self
.my_another_parameter
= my_another_parameter
84 def __eq__(self
, other
):
85 if isinstance(other
, YAMLObject1
):
86 return self
.__class
__, self
.__dict
__ == other
.__class
__, other
.__dict
__
90 class TestTypeRepresenter(test_appliance
.TestAppliance
):
92 def _testTypes(self
, test_name
, data_filename
, code_filename
):
93 data1
= eval(file(code_filename
, 'rb').read())
97 output
= dump(data1
, Dumper
=MyDumper
)
98 data2
= load(output
, Loader
=MyLoader
)
100 self
.failUnlessEqual(data1
, data2
)
101 except AssertionError:
102 if isinstance(data1
, dict):
103 data1
= data1
.items()
106 data2
= data2
.items()
115 print "NATIVES1:", data1
116 print "NATIVES2:", data2
119 TestTypeRepresenter
.add_tests('testTypes', '.data', '.code')