本文整理汇总了Python中msgpack.Packer方法的典型用法代码示例。如果您正苦于以下问题:Python msgpack.Packer方法的具体用法?Python msgpack.Packer怎么用?Python msgpack.Packer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msgpack
的用法示例。
在下文中一共展示了msgpack.Packer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: msgpack_appendable_pack
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def msgpack_appendable_pack(o, path):
open(path, 'a+').close() # touch
with open(path, mode='r+b') as f:
packer = msgpack.Packer()
unpacker = msgpack.Unpacker(f)
if type(o) == list:
try:
previous_len = unpacker.read_array_header()
except msgpack.OutOfData:
previous_len = 0
# calculate and replace header
header = packer.pack_array_header(previous_len + len(o))
f.seek(0)
f.write(header)
f.write(bytes(1) * (MAX_MSGPACK_ARRAY_HEADER_LEN - len(header)))
# append new elements
f.seek(0, 2)
for element in o:
f.write(packer.pack(element))
else:
f.write(packer.pack(o))
示例2: msgpack_appendable_unpack
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def msgpack_appendable_unpack(path):
# if not list?
# return msgpack.unpackb(f.read())
with open(path, 'rb') as f:
packer = msgpack.Packer()
unpacker = msgpack.Unpacker(f, encoding='utf-8')
length = unpacker.read_array_header()
header_lenght = len(packer.pack_array_header(length))
unpacker.read_bytes(MAX_MSGPACK_ARRAY_HEADER_LEN - header_lenght)
f.seek(MAX_MSGPACK_ARRAY_HEADER_LEN)
return [unpacker.unpack() for _ in range(length)]
示例3: __init__
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def __init__(self, io, opts={}):
self.io = io
self.packer = msgpack.Packer(autoreset=False)
nopts = MsgPackMarshaler.default_opts.copy()
nopts.update(opts)
Marshaler.__init__(self, nopts)
示例4: __init__
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def __init__(self, fileobj=None, filepath=None):
if all([fileobj is None, filepath is None]):
raise ValueError('require fileobj or filepath')
self._filepath = filepath
self._fileobj = fileobj
self.packer = msgpack.Packer(use_bin_type=True)
self.unpacker_buffer_size = 16 * 1024 * 1024
self.buffer_size = 16 * 1024
self.wal_size = 0
示例5: handle
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def handle(self):
packer = Packer(use_bin_type=True)
unpacker = Unpacker(raw=False, max_buffer_size=10 * 1024 * 1024)
while True:
buf = self.cli_sock.recv(1024)
if not buf:
break
unpacker.feed(buf)
for request in unpacker:
response = self.process(request)
self.cli_sock.sendall(packer.pack(response))
示例6: request
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def request(self, command, **params):
packer = Packer(use_bin_type=True)
unpacker = Unpacker(raw=False, max_buffer_size=10 * 1024 * 1024)
request = BackdoorRequest(command, params)
LOG.debug(f'backdoor client sending request {request}')
self.sock.sendall(packer.pack(request.to_dict()))
while True:
buf = self.sock.recv(1024)
if not buf:
break
unpacker.feed(buf)
for response in unpacker:
response = BackdoorResponse(response['ok'], response['content'])
LOG.debug(f'backdoor client received response {response}')
return response
示例7: __init__
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def __init__(self, socket, outgoing_msg_sink_iter):
super(RpcSession, self).__init__("RpcSession(%s)" % socket)
import msgpack
self._packer = msgpack.Packer()
self._unpacker = msgpack.Unpacker()
self._next_msgid = 0
self._socket = socket
self._outgoing_msg_sink_iter = outgoing_msg_sink_iter
示例8: __init__
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def __init__(self):
super(MessageEncoder, self).__init__()
# note: on-wire msgpack has no notion of encoding.
# the msgpack-python library implicitly converts unicode to
# utf-8 encoded bytes by default. we don't want to rely on
# the behaviour though because it seems to be going to change.
# cf. https://gist.github.com/methane/5022403
self._packer = msgpack.Packer(encoding=None)
self._unpacker = msgpack.Unpacker(encoding=None)
self._next_msgid = 0
示例9: _pack
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def _pack(self, chunk):
packer = msgpack.Packer(autoreset=False)
for _, row in chunk.iterrows():
# row.dtype can be non-object (such as numpy.int64 or numpy.float64)
# when column types are homogeneous. In this case, packer.pack raises
# an exception because it doesn't know how to encode those data types.
if row.dtype.name != "object":
row = row.astype("object")
row.dropna(inplace=True)
packer.pack(dict(row))
return packer.bytes()
示例10: _pack_gz
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def _pack_gz(self, result):
packer = msgpack.Packer(autoreset=False)
for row in result:
packer.pack(row)
buff = io.BytesIO()
with gzip.GzipFile(fileobj=buff, mode="wb") as f:
f.write(packer.bytes())
return buff.getvalue()
示例11: create_msgpack
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def create_msgpack(items):
"""Create msgpack streaming bytes from list
Args:
items (list of dict): target list
Returns:
Converted msgpack streaming (bytes)
Examples:
>>> t1 = int(time.time())
>>> l1 = [{"a": 1, "b": 2, "time": t1}, {"a":3, "b": 6, "time": t1}]
>>> create_msgpack(l1)
b'\\x83\\xa1a\\x01\\xa1b\\x02\\xa4time\\xce]\\xa5X\\xa1\\x83\\xa1a\\x03\\xa1b\\x06\\xa4time\\xce]\\xa5X\\xa1'
"""
stream = io.BytesIO()
packer = msgpack.Packer()
for item in items:
try:
mp = packer.pack(item)
except (OverflowError, ValueError):
packer.reset()
mp = packer.pack(normalized_msgpack(item))
stream.write(mp)
return stream.getvalue()
示例12: _prepare_file
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def _prepare_file(self, file_like, fmt, **kwargs):
fp = tempfile.TemporaryFile()
with contextlib.closing(gzip.GzipFile(mode="wb", fileobj=fp)) as gz:
packer = msgpack.Packer()
with contextlib.closing(self._read_file(file_like, fmt, **kwargs)) as items:
for item in items:
try:
mp = packer.pack(item)
except (OverflowError, ValueError):
packer.reset()
mp = packer.pack(normalized_msgpack(item))
gz.write(mp)
fp.seek(0)
return fp
示例13: test_job_result_success
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def test_job_result_success():
td = api.API("APIKEY")
packer = msgpack.Packer()
rows = [["foo", 123], ["bar", 456], ["baz", 789]]
body = b""
for row in rows:
body += packer.pack(row)
td.get = mock.MagicMock(return_value=make_response(200, body))
result = td.job_result(12345)
td.get.assert_called_with("/v3/job/result/12345", {"format": "msgpack"})
assert result == rows
示例14: test_job_result_each_success
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def test_job_result_each_success():
td = api.API("APIKEY")
packer = msgpack.Packer()
rows = [["foo", 123], ["bar", 456], ["baz", 789]]
body = b""
for row in rows:
body += packer.pack(row)
td.get = mock.MagicMock(return_value=make_response(200, body))
result = []
for row in td.job_result_each(12345):
result.append(row)
td.get.assert_called_with("/v3/job/result/12345", {"format": "msgpack"})
assert result == rows
示例15: __init__
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import Packer [as 别名]
def __init__(self, event_loop):
"""Wrap `event_loop` on a msgpack-aware interface."""
self.loop = event_loop
self._packer = Packer(unicode_errors=unicode_errors_default)
self._unpacker = Unpacker(unicode_errors=unicode_errors_default)
self._message_cb = None