本文整理汇总了Python中serializer.Serializer.unserialize方法的典型用法代码示例。如果您正苦于以下问题:Python Serializer.unserialize方法的具体用法?Python Serializer.unserialize怎么用?Python Serializer.unserialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类serializer.Serializer
的用法示例。
在下文中一共展示了Serializer.unserialize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestSerialize
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import unserialize [as 别名]
class TestSerialize(unittest.TestCase):
@classmethod
def _clearMsgDefs(self):
# Clear all non-test messages
try:
while True:
messages.MESSAGES.pop()
except IndexError:
pass
@classmethod
def setUpClass(self):
self._clearMsgDefs()
messages.MESSAGES.append(__import__("test_complex_pb2", globals(), locals(), [], -1))
def setUp(self):
self.calls = 0
self.s = Serializer()
def test_unnamed_serialize(self):
enc = self.s.serialize("TestComplex", [("normal", ["m1", "m2"])])
def f(obj):
self.assertTrue(obj.IsInitialized())
self.assertEqual(obj.nesteds[0].normal, "normal")
self.assertEqual(obj.nesteds[0].multiple[0], "m1")
self.assertEqual(obj.nesteds[0].multiple[1], "m2")
self.calls += 1
self.s.add_handler("TestComplex", f)
self.s.unserialize(enc)
self.assertEqual(self.calls, 1)
def test_named_serialize(self):
enc = self.s.serialize("TestComplex",
nesteds = [{
"normal": "normal",
"multiple": [
"m1",
"m2"
]
}]
)
def f(obj):
self.assertTrue(obj.IsInitialized())
self.assertEqual(obj.nesteds[0].normal, "normal")
self.assertEqual(obj.nesteds[0].multiple[0], "m1")
self.assertEqual(obj.nesteds[0].multiple[1], "m2")
self.calls += 1
self.s.add_handler("TestComplex", f)
self.s.unserialize(enc)
self.assertEqual(self.calls, 1)
def test_nested_length(self):
with self.assertRaises(FieldTooLongException):
self.s.serialize("TestComplex", [("", ["longerthantwentycharacters"])])
def test_obj_onto_repeated(self):
with self.assertRaises(FieldWrongTypeException):
self.s.serialize("TestComplex", [("", ("m1",))])
def test_repeated_onto_obj(self):
with self.assertRaises(FieldWrongTypeException):
self.s.serialize("TestComplex", [["", ["m1"]]])
def test_unicode_strings(self):
sbs = '}\xc8A\xc1\x8a}D\xe8\xea\x93}'
enc = self.s.serialize("TestComplex", [(sbs, ["m1", "m2"])])
def f(obj):
self.assertTrue(obj.IsInitialized())
for i in range(len(sbs)):
self.assertEqual(ord(obj.nesteds[0].normal[i]), ord(sbs[i]))
self.assertEqual(obj.nesteds[0].multiple[0], "m1")
self.assertEqual(obj.nesteds[0].multiple[1], "m2")
self.calls += 1
self.s.add_handler("TestComplex", f)
self.s.unserialize(enc)
self.assertEqual(self.calls, 1)
def test_optional_missing(self):
enc = self.s.serialize("TestOptional")
def f(obj):
self.assertTrue(obj.IsInitialized())
self.assertTrue(not obj.field)
self.calls += 1
self.s.add_handler("TestOptional", f)
self.s.unserialize(enc)
self.assertEqual(self.calls, 1)
def test_optional_set(self):
enc = self.s.serialize("TestOptional", "test")
def f(obj):
#.........这里部分代码省略.........
示例2: TestSerialize
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import unserialize [as 别名]
class TestSerialize(unittest.TestCase):
@classmethod
def _clearMsgDefs(self):
# Clear all non-test messages
try:
while True:
messages.MESSAGES.pop()
except IndexError:
pass
@classmethod
def setUpClass(self):
self._clearMsgDefs()
messages.MESSAGES.append(__import__("test_pb2", globals(), locals(), [], -1))
self.blank_args = [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, True, 0, "a", "b", "c", "d"]
self.neg_args = [-1.0, 0, -1.0, 0, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, -1, 1, 1, "", "", "", ""]
self.illegal_len_args = [
[0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""]
]
self.too_long_args = [
[0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "aaa", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", "aaa"]
]
def setUp(self):
self.s = Serializer()
def test_id(self):
enc = self.s.serialize("Test", *self.blank_args)
(name, ) = struct.unpack_from('20s', enc)
self.assertTrue(name.startswith('test_pb2Test'))
def test_values(self):
enc = self.s.serialize("Test", *self.blank_args)
def f(obj):
self.assertTrue(obj.IsInitialized())
for (_, value) in obj.ListFields():
self.assertIn(value, self.blank_args)
self.s.add_handler("Test", f)
self.s.unserialize(enc)
def test_explicit_call(self):
enc = self.s.serialize("test.Test", *self.blank_args)
def f(obj):
self.assertTrue(obj.IsInitialized())
for (_, value) in obj.ListFields():
self.assertIn(value, self.blank_args)
self.s.add_handler("Test", f)
self.s.unserialize(enc)
def test_negative_values(self):
enc = self.s.serialize("Test", *self.neg_args)
def f(obj):
self.assertTrue(obj.IsInitialized())
for (_, value) in obj.ListFields():
self.assertIn(value, self.neg_args)
self.s.add_handler("Test", f)
self.s.unserialize(enc)
def test_illegal_length_option(self):
for args in self.illegal_len_args:
with self.assertRaises(FieldLengthUnsupportedException):
self.s.serialize("Test", *args)
def test_too_long_option(self):
for args in self.too_long_args:
with self.assertRaises(FieldTooLongException):
self.s.serialize("Test", *args)
def test_unknown_message(self):
with self.assertRaises(UnknownMessageException):
self.s.serialize("Unknown")
def test_unknown_message_explicit(self):
with self.assertRaises(UnknownMessageException):
#.........这里部分代码省略.........