当前位置: 首页>>代码示例>>Python>>正文


Python msgpack.Packer方法代码示例

本文整理汇总了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)) 
开发者ID:simonacca,项目名称:zatt,代码行数:26,代码来源:utils.py

示例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)] 
开发者ID:simonacca,项目名称:zatt,代码行数:15,代码来源:utils.py

示例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) 
开发者ID:cognitect,项目名称:transit-python,代码行数:8,代码来源:writer.py

示例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 
开发者ID:anyant,项目名称:rssant,代码行数:11,代码来源:storage.py

示例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)) 
开发者ID:anyant,项目名称:rssant,代码行数:13,代码来源:server.py

示例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 
开发者ID:anyant,项目名称:rssant,代码行数:17,代码来源:client.py

示例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 
开发者ID:OpenState-SDN,项目名称:ryu,代码行数:11,代码来源:net_ctrl.py

示例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 
开发者ID:OpenState-SDN,项目名称:ryu,代码行数:12,代码来源:rpc.py

示例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() 
开发者ID:treasure-data,项目名称:pandas-td,代码行数:13,代码来源:td.py

示例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() 
开发者ID:treasure-data,项目名称:pandas-td,代码行数:10,代码来源:test_td.py

示例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() 
开发者ID:treasure-data,项目名称:td-client-python,代码行数:29,代码来源:util.py

示例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 
开发者ID:treasure-data,项目名称:td-client-python,代码行数:16,代码来源:api.py

示例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 
开发者ID:treasure-data,项目名称:td-client-python,代码行数:13,代码来源:job_api_test.py

示例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 
开发者ID:treasure-data,项目名称:td-client-python,代码行数:15,代码来源:job_api_test.py

示例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 
开发者ID:neovim,项目名称:pynvim,代码行数:8,代码来源:msgpack_stream.py


注:本文中的msgpack.Packer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。