示例#1
0
    def _basetest():
        print 'Base test:'

        s = Stream(128)

        print 'Pack/Unpack base data type:'
        # byte, char, bool, int16, int16, int32, int32, int64, int64, float, double, str
        will_pack = ('s', 'c', True, 16, -16, 32, -32, 64, -64, 1.3, 2.6,
                     'hello world')
        print 'pack() test, will pack data: {}'.format(will_pack)

        fmt = 'bbBssiiqqfdS'
        s.pack(fmt, *will_pack)
        print 'after pack, pos: {}'.format(s.pos)
        print 'raw data: {}'.format(s)

        s.pos = 0
        unpacked = s.unpack(fmt)
        print 'unpacked, data: {}'.format(unpacked)
        print

        print 'Pack/Unpack list:'
        s.pos = 0
        will_pack = ['hello', 'world']
        print 'Before pack: {}'.format(will_pack)
        s.pack('[S]', will_pack)

        s.pos = 0
        unpacked = s.unpack('[S]')
        print 'After unpack list: {}'.format(unpacked)
        print

        print 'Pack/Unpack tuple:'
        s.pos = 0
        will_pack = ('hello', 'world')
        print 'Before pack: {}'.format(will_pack)
        s.pack('(S)', will_pack)

        s.pos = 0
        unpacked = s.unpack('[S]')
        print 'After unpack tuple: {}'.format(unpacked)
        print

        print 'Pack/Unpack dict:'
        s.pos = 0
        will_pack = {'name': 'Judy', 'age': '18', 'home': 'California'}
        print 'Before pack: {}'.format(will_pack)
        s.pack('{S:S}', will_pack)

        s.pos = 0
        unpacked = s.unpack('{S:S}')
        print 'After unpack dict: {}'.format(unpacked)
        print
示例#2
0
    def _basetest():
        print "Base test:"

        s = Stream(128)

        print "Pack/Unpack base data type:"
        # byte, char, bool, int16, int16, int32, int32, int64, int64, float, double, str
        will_pack = ("s", "c", True, 16, -16, 32, -32, 64, -64, 1.3, 2.6, "hello world")
        print "pack() test, will pack data: {}".format(will_pack)

        fmt = "bbBssiiqqfdS"
        s.pack(fmt, *will_pack)
        print "after pack, pos: {}".format(s.pos)
        print "raw data: {}".format(s)

        s.pos = 0
        unpacked = s.unpack(fmt)
        print "unpacked, data: {}".format(unpacked)
        print

        print "Pack/Unpack list:"
        s.pos = 0
        will_pack = ["hello", "world"]
        print "Before pack: {}".format(will_pack)
        s.pack("[S]", will_pack)

        s.pos = 0
        unpacked = s.unpack("[S]")
        print "After unpack list: {}".format(unpacked)
        print

        print "Pack/Unpack tuple:"
        s.pos = 0
        will_pack = ("hello", "world")
        print "Before pack: {}".format(will_pack)
        s.pack("(S)", will_pack)

        s.pos = 0
        unpacked = s.unpack("[S]")
        print "After unpack tuple: {}".format(unpacked)
        print

        print "Pack/Unpack dict:"
        s.pos = 0
        will_pack = {"name": "Judy", "age": "18", "home": "California"}
        print "Before pack: {}".format(will_pack)
        s.pack("{S:S}", will_pack)

        s.pos = 0
        unpacked = s.unpack("{S:S}")
        print "After unpack dict: {}".format(unpacked)
        print
示例#3
0
    def _obj_pack_test(self):
        print 'Obj/Class pack test:'

        s = Stream()
        obj = PackableObjA()
        print 'Before pack obj: {}'.format(obj)
        s.pack('C<PackableObjA>', obj)

        print 'After pack object, size: {}'.format(s.pos)

        s.pos = 0
        unpacked = s.unpack('C<PackableObjA>')[0]
        print 'After unpack obj: {}'.format(unpacked)
        print
示例#4
0
    def _composite_test(self):
        print "Composite test:"

        s = Stream()
        will_pack = {1: {"hello": ["Hello", True, None, 3], "world": ["World", False, None, -3]}}
        print "Will pack data: {}".format(will_pack)

        s.pack("{i:{S:[SBNC<int>]}}", will_pack)
        print "Pack done, size: {}".format(s.pos)

        s.pos = 0
        unpacked = s.unpack("{i:{S:[SBNC<int>]}}")
        print "After unpack data: {}".format(unpacked)
        print
示例#5
0
    def _obj_pack_test(self):
        print "Obj/Class pack test:"

        s = Stream()
        obj = PackableObjA()
        print "Before pack obj: {}".format(obj)
        s.pack("C<PackableObjA>", obj)

        print "After pack object, size: {}".format(s.pos)

        s.pos = 0
        unpacked = s.unpack("C<PackableObjA>")[0]
        print "After unpack obj: {}".format(unpacked)
        print
示例#6
0
    def _perf_test(self):
        print "Performance test:"

        s = Stream()
        will_pack = {1: {"hello": ["Hello", True, None, 3], "world": ["World", False, None, -3]}}
        print "Will pack data: {}".format(will_pack)

        beg = time()
        print "Pack/Unpack times: 1000000"
        for i in range(1000000):
            s.pos = 0
            s.pack("{i:{S:[SBNC<int>]}}", will_pack)

            s.pos = 0
            unpacked = s.unpack("{i:{S:[SBNC<int>]}}")

        elapsed = time() - beg
        print "Done, used time: {}".format(elapsed)
示例#7
0
    def _composite_test(self):
        print 'Composite test:'

        s = Stream()
        will_pack = {
            1: {
                'hello': ['Hello', True, None, 3],
                'world': ['World', False, None, -3]
            }
        }
        print 'Will pack data: {}'.format(will_pack)

        s.pack('{i:{S:[SBNC<int>]}}', will_pack)
        print 'Pack done, size: {}'.format(s.pos)

        s.pos = 0
        unpacked = s.unpack('{i:{S:[SBNC<int>]}}')
        print 'After unpack data: {}'.format(unpacked)
        print
示例#8
0
    def _perf_test(self):
        print 'Performance test:'

        s = Stream()
        will_pack = {
            1: {
                'hello': ['Hello', True, None, 3],
                'world': ['World', False, None, -3]
            }
        }
        print 'Will pack data: {}'.format(will_pack)

        beg = time()
        print 'Pack/Unpack times: 1000000'
        for i in range(1000000):
            s.pos = 0
            s.pack('{i:{S:[SBNC<int>]}}', will_pack)

            s.pos = 0
            unpacked = s.unpack('{i:{S:[SBNC<int>]}}')

        elapsed = time() - beg
        print 'Done, used time: {}'.format(elapsed)