return parser
+def _make_struct_deserializer(tag, fmt):
+ fmt = '!' + fmt
+ size = struct.calcsize(fmt)
+ unpacker = functools.partial(struct.unpack, fmt)
+
+ def parser(b):
+ b = b.read(size)
+ assert len(b) == size
+ return TaggedObject(tag = tag, instance = unpacker(b)[0])
+
+ return parser
+
_TAGS_TO_PARSERS = {
TAG_NULL: _make_tag_only_parser(TAG_NULL, None),
TAG_TRUE: _make_tag_only_parser(TAG_TRUE, True),
TAG_FALSE: _make_tag_only_parser(TAG_FALSE, False),
+ TAG_UINT8: _make_struct_deserializer(TAG_UINT8, 'B'),
+ TAG_UINT16: _make_struct_deserializer(TAG_UINT16, 'H'),
+ TAG_UINT32: _make_struct_deserializer(TAG_UINT32, 'I'),
+ TAG_UINT64: _make_struct_deserializer(TAG_UINT64, 'Q'),
}
def deserialize(b):
import binary
-class SerializeTests(unittest.TestCase):
- def test_serializes_tag_only_types(self):
- self.assertEqual(
- binary.serialize(binary.TaggedObject(
- tag = binary.TAG_NULL,
- instance = None,
- )),
- b'\x00',
- )
- self.assertEqual(
- binary.serialize(binary.TaggedObject(
- tag = binary.TAG_TRUE,
- instance = True,
- )),
- b'\x01',
- )
- self.assertEqual(
- binary.serialize(binary.TaggedObject(
- tag = binary.TAG_FALSE,
- instance = False,
- )),
- b'\x02',
- )
-
- def test_serializes_unsigned_integer_types(self):
- self.assertEqual(
- binary.serialize(binary.TaggedObject(
- tag = binary.TAG_UINT8,
- instance = 7,
- )),
- b'\x03\x07',
- )
- self.assertEqual(
- binary.serialize(binary.TaggedObject(
- tag = binary.TAG_UINT16,
- instance = 7,
- )),
- b'\x04\x00\x07',
- )
- self.assertEqual(
- binary.serialize(binary.TaggedObject(
- tag = binary.TAG_UINT32,
- instance = 7,
- )),
- b'\x05\x00\x00\x00\x07',
- )
- self.assertEqual(
- binary.serialize(binary.TaggedObject(
- tag = binary.TAG_UINT64,
- instance = 7,
- )),
- b'\x06\x00\x00\x00\x00\x00\x00\x00\x07',
- )
+EXAMPLE_REPRESENTATIONS = [
+ (binary.TAG_NULL, None, b'\x00'),
+ (binary.TAG_TRUE, True, b'\x01'),
+ (binary.TAG_FALSE, False, b'\x02'),
+ (binary.TAG_UINT8, 7, b'\x03\x07'),
+ (binary.TAG_UINT16, 7, b'\x04\x00\x07'),
+ (binary.TAG_UINT32, 7, b'\x05\x00\x00\x00\x07'),
+ (binary.TAG_UINT64, 7, b'\x06\x00\x00\x00\x00\x00\x00\x00\x07'),
+]
+class SerializeTests(unittest.TestCase):
+ def test_serialize(self):
+ for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
+ self.assertEqual(
+ binary.serialize(binary.TaggedObject(
+ tag = tag,
+ instance = instance,
+ )),
+ representation,
+ )
class DeserializeTests(unittest.TestCase):
- def test_deserializes_tag_only_types(self):
- self.assertEqual(
- binary.deserialize(b'\x00'),
- binary.TaggedObject(
- tag = binary.TAG_NULL,
- instance = None,
- ),
- )
- self.assertEqual(
- binary.deserialize(b'\x01'),
- binary.TaggedObject(
- tag = binary.TAG_TRUE,
- instance = True,
- ),
- )
- self.assertEqual(
- binary.deserialize(b'\x02'),
- binary.TaggedObject(
- tag = binary.TAG_FALSE,
- instance = False,
+ def test_deserialize(self):
+ for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
+ self.assertEqual(
+ binary.deserialize(representation),
+ binary.TaggedObject(
+ tag = tag,
+ instance = instance,
+ ),
)
- )
unittest.main()