File: utils.py

package info (click to toggle)
python-thriftpy 0.3.9%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 560 kB
  • sloc: python: 3,287; ansic: 30; makefile: 7
file content (37 lines) | stat: -rw-r--r-- 1,055 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-

from __future__ import absolute_import

import binascii

from .transport import TMemoryBuffer
from .protocol.binary import TBinaryProtocolFactory


def serialize(thrift_object, proto_factory=TBinaryProtocolFactory()):
    transport = TMemoryBuffer()
    protocol = proto_factory.get_protocol(transport)
    thrift_object.write(protocol)
    protocol.write_message_end()
    return transport.getvalue()


def deserialize(thrift_object, buf, proto_factory=TBinaryProtocolFactory()):
    transport = TMemoryBuffer(buf)
    protocol = proto_factory.get_protocol(transport)
    thrift_object.read(protocol)
    return thrift_object


def hexlify(byte_array, delimeter=' '):
    s = binascii.hexlify(byte_array).decode('utf-8')
    return delimeter.join(a+b for a, b in zip(s[::2], s[1::2]))


def hexprint(byte_array, delimeter=' ', count=10):
    print("Bytes:")
    print(byte_array)

    print("\nHex:")
    g = hexlify(byte_array, delimeter).split(delimeter)
    print('\n'.join(' '.join(g[i:i+10]) for i in range(0, len(g), 10)))