# Msgpack parser with typed arrays # Based on u-msgpack-python v2.4.1 - v at sergeev.io # https://github.com/vsergeev/u-msgpack-python # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # import struct import io import numpy as np def _pack_integer(obj, fp): if obj < 0: if obj >= -32: fp.write(struct.pack("b", obj)) elif obj >= -2**(8 - 1): fp.write(b"\xd0" + struct.pack("b", obj)) elif obj >= -2**(16 - 1): fp.write(b"\xd1" + struct.pack("= -2**(32 - 1): fp.write(b"\xd2" + struct.pack("= -2**(64 - 1): fp.write(b"\xd3" + struct.pack(" 0 and isinstance(obj[0], float): fp.write(b"\xca") for e in obj: fp.write(struct.pack(" 0 and isinstance(obj[0], int): fp.write(b"\xd2") for e in obj: fp.write(struct.pack(" 0 and isinstance(obj[0], np.float32): fp.write(b"\xca") fp.write(obj.tobytes()) # Int32 elif len(obj) > 0 and isinstance(obj[0], np.int32): fp.write(b"\xd2") fp.write(obj.tobytes()) # Int16 elif len(obj) > 0 and isinstance(obj[0], np.int16): fp.write(b"\xd1") fp.write(obj.tobytes()) # Regular else: for e in obj: pack(e, fp) def _pack_map(obj, fp): if len(obj) <= 15: fp.write(struct.pack("B", 0x80 | len(obj))) elif len(obj) <= 2**16 - 1: fp.write(b"\xde" + struct.pack("