10 from sets
import Set
as set
16 class MyLoader(Loader
):
18 class MyDumper(Dumper
):
23 def __init__(self
, x
, y
=0, z
=0):
28 def __eq__(self
, other
):
29 if isinstance(other
, MyTestClass1
):
30 return self
.__class
__, self
.__dict
__ == other
.__class
__, other
.__dict
__
34 def construct1(constructor
, node
):
35 mapping
= constructor
.construct_mapping(node
)
36 return MyTestClass1(**mapping
)
37 def represent1(representer
, native
):
38 return representer
.represent_mapping("!tag1", native
.__dict
__)
40 add_constructor("!tag1", construct1
, Loader
=MyLoader
)
41 add_representer(MyTestClass1
, represent1
, Dumper
=MyDumper
)
43 class MyTestClass2(MyTestClass1
, YAMLObject
):
45 yaml_loader
= MyLoader
46 yaml_dumper
= MyDumper
49 def from_yaml(cls
, constructor
, node
):
50 x
= constructor
.construct_yaml_int(node
)
52 from_yaml
= classmethod(from_yaml
)
54 def to_yaml(cls
, representer
, native
):
55 return representer
.represent_scalar(cls
.yaml_tag
, str(native
.x
))
56 to_yaml
= classmethod(to_yaml
)
58 class MyTestClass3(MyTestClass2
):
62 def from_yaml(cls
, constructor
, node
):
63 mapping
= constructor
.construct_mapping(node
)
69 from_yaml
= classmethod(from_yaml
)
71 def to_yaml(cls
, representer
, native
):
72 return representer
.represent_mapping(cls
.yaml_tag
, native
.__dict
__)
73 to_yaml
= classmethod(to_yaml
)
75 class YAMLObject1(YAMLObject
):
77 yaml_loader
= MyLoader
78 yaml_dumper
= MyDumper
81 def __init__(self
, my_parameter
=None, my_another_parameter
=None):
82 self
.my_parameter
= my_parameter
83 self
.my_another_parameter
= my_another_parameter
85 def __eq__(self
, other
):
86 if isinstance(other
, YAMLObject1
):
87 return self
.__class
__, self
.__dict
__ == other
.__class
__, other
.__dict
__
91 class YAMLObject2(YAMLObject
):
93 yaml_loader
= MyLoader
94 yaml_dumper
= MyDumper
97 def __init__(self
, foo
=1, bar
=2, baz
=3):
102 def __getstate__(self
):
103 return {1: self
.foo
, 2: self
.bar
, 3: self
.baz
}
105 def __setstate__(self
, state
):
110 def __eq__(self
, other
):
111 if isinstance(other
, YAMLObject2
):
112 return self
.__class
__, self
.__dict
__ == other
.__class
__, other
.__dict
__
116 class AnObject(object):
118 def __new__(cls
, foo
=None, bar
=None, baz
=None):
119 self
= object.__new
__(cls
)
125 def __cmp__(self
, other
):
126 return cmp((type(self
), self
.foo
, self
.bar
, self
.baz
),
127 (type(other
), other
.foo
, other
.bar
, other
.baz
))
129 def __eq__(self
, other
):
130 return type(self
) is type(other
) and \
131 (self
.foo
, self
.bar
, self
.baz
) == (other
.foo
, other
.bar
, other
.baz
)
135 def __init__(self
, foo
=None, bar
=None, baz
=None):
140 def __cmp__(self
, other
):
141 return cmp((type(self
), self
.foo
, self
.bar
, self
.baz
),
142 (type(other
), other
.foo
, other
.bar
, other
.baz
))
144 def __eq__(self
, other
):
145 return type(self
) is type(other
) and \
146 (self
.foo
, self
.bar
, self
.baz
) == (other
.foo
, other
.bar
, other
.baz
)
148 class AState(AnInstance
):
150 def __getstate__(self
):
157 def __setstate__(self
, state
):
158 self
.foo
= state
['_foo']
159 self
.bar
= state
['_bar']
160 self
.baz
= state
['_baz']
162 class ACustomState(AnInstance
):
164 def __getstate__(self
):
165 return (self
.foo
, self
.bar
, self
.baz
)
167 def __setstate__(self
, state
):
168 self
.foo
, self
.bar
, self
.baz
= state
170 class InitArgs(AnInstance
):
172 def __getinitargs__(self
):
173 return (self
.foo
, self
.bar
, self
.baz
)
175 def __getstate__(self
):
178 class InitArgsWithState(AnInstance
):
180 def __getinitargs__(self
):
181 return (self
.foo
, self
.bar
)
183 def __getstate__(self
):
186 def __setstate__(self
, state
):
189 class NewArgs(AnObject
):
191 def __getnewargs__(self
):
192 return (self
.foo
, self
.bar
, self
.baz
)
194 def __getstate__(self
):
197 class NewArgsWithState(AnObject
):
199 def __getnewargs__(self
):
200 return (self
.foo
, self
.bar
)
202 def __getstate__(self
):
205 def __setstate__(self
, state
):
208 class Reduce(AnObject
):
210 def __reduce__(self
):
211 return self
.__class
__, (self
.foo
, self
.bar
, self
.baz
)
213 class ReduceWithState(AnObject
):
215 def __reduce__(self
):
216 return self
.__class
__, (self
.foo
, self
.bar
), self
.baz
218 def __setstate__(self
, state
):
223 def __eq__(self
, other
):
224 return type(self
) is type(other
) and int(self
) == int(other
)
228 def __init__(self
, n
=1):
229 self
.extend([None]*n
)
231 def __eq__(self
, other
):
232 return type(self
) is type(other
) and list(self
) == list(other
)
236 def __init__(self
, n
=1):
240 def __eq__(self
, other
):
241 return type(self
) is type(other
) and dict(self
) == dict(other
)
243 class TestConstructorTypes(test_appliance
.TestAppliance
):
245 def _testTypes(self
, test_name
, data_filename
, code_filename
):
249 data1
= list(load_all(file(data_filename
, 'rb'), Loader
=MyLoader
))
252 data2
= eval(file(code_filename
, 'rb').read())
253 self
.failUnlessEqual(type(data1
), type(data2
))
255 self
.failUnlessEqual(data1
, data2
)
256 except AssertionError:
257 if isinstance(data1
, dict):
258 data1
= [(repr(key
), value
) for key
, value
in data1
.items()]
261 data2
= [(repr(key
), value
) for key
, value
in data2
.items()]
266 elif isinstance(data1
, list):
267 self
.failUnlessEqual(type(data1
), type(data2
))
268 self
.failUnlessEqual(len(data1
), len(data2
))
269 for item1
, item2
in zip(data1
, data2
):
270 if (item1
!= item1
or (item1
== 0.0 and item1
== 1.0)) and \
271 (item2
!= item2
or (item2
== 0.0 and item2
== 1.0)):
273 self
.failUnlessEqual(item1
, item2
)
279 print file(data_filename
, 'rb').read()
281 print file(code_filename
, 'rb').read()
282 print "NATIVES1:", data1
283 print "NATIVES2:", data2
286 TestConstructorTypes
.add_tests('testTypes', '.data', '.code')