本文整理汇总了Python中msgpack.loads方法的典型用法代码示例。如果您正苦于以下问题:Python msgpack.loads方法的具体用法?Python msgpack.loads怎么用?Python msgpack.loads使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msgpack
的用法示例。
在下文中一共展示了msgpack.loads方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: msgpackext_loads
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def msgpackext_loads(data: bytes) -> Any:
"""Deserializes a msgpack byte representation of known objects into those objects.
Parameters
----------
data : bytes
The serialized msgpack byte array.
Returns
-------
Any
The deserialized Python objects.
"""
which_import("msgpack", raise_error=True, raise_msg=_msgpack_which_msg)
return msgpack.loads(data, object_hook=msgpackext_decode, raw=False)
## JSON Ext
示例2: jsonext_loads
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def jsonext_loads(data: Union[str, bytes]) -> Any:
"""Deserializes a json representation of known objects into those objects.
Parameters
----------
data : str or bytes
The byte-serialized JSON blob.
Returns
-------
Any
The deserialized Python objects.
"""
return json.loads(data, object_hook=jsonext_decode)
## JSON
示例3: json_loads
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def json_loads(data: str) -> Any:
"""Deserializes a json representation of known objects into those objects.
Parameters
----------
data : str
The serialized JSON blob.
Returns
-------
Any
The deserialized Python objects.
"""
# Doesn't hurt anything to try to load JSONext as well
return json.loads(data, object_hook=jsonext_decode)
## Helper functions
示例4: do_POST
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def do_POST(self):
if not self.check_authorization():
self.respond_unauthorized(request_authentication=True)
return
content_length = int(self.headers.get('content-length', 0))
data = self.rfile.read(content_length)
self.raw_query_data = data
content_type = self.headers.get('content-type', '')
content_type = content_type.split(';', 1)[0]
self.query_data = {}
try:
if not isinstance(data, str):
data = data.decode(self.get_content_type_charset())
if content_type.startswith('application/json'):
data = json.loads(data)
if isinstance(data, dict):
self.query_data = dict([(i[0], [i[1]]) for i in data.items()])
else:
self.query_data = urllib.parse.parse_qs(data, keep_blank_values=1)
except Exception:
self.respond_server_error(400)
else:
self.dispatch_handler(self.query_data)
return
示例5: loads
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def loads(self, data):
"""
Deserialize the data into it's original python object.
:param bytes data: The serialized object to load.
:return: The original python object.
"""
if not isinstance(data, bytes):
raise TypeError("loads() argument 1 must be bytes, not {0}".format(type(data).__name__))
if self._compression == 'zlib':
data = zlib.decompress(data)
if sys.version_info[0] == 3 and self.name.startswith('application/'):
data = data.decode(self._charset)
data = g_serializer_drivers[self.name]['loads'](data, (self._charset if sys.version_info[0] == 3 else None))
if isinstance(data, list):
data = tuple(data)
return data
示例6: load
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def load(cls, bmessage):
"""
From a bytestring given by a (distant) call to Message.dump(), retrieve the original message
:param bmessage: bytestring given by a .dump() call on a message
:return: the original message
"""
message_dict = msgpack.loads(bmessage, use_list=False)
try:
obj = MessageMeta._registered_messages[message_dict["type"]].__new__(MessageMeta._registered_messages[message_dict["type"]])
object.__setattr__(obj, "__dict__", message_dict)
except:
raise TypeError("Unknown message type") from None
if not obj._verify(): # pylint: disable=protected-access
raise TypeError("Invalid message content")
return obj
示例7: load
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def load(self):
try:
with open(self.store_file, "rb") as fp:
data = fp.read()
data = msgpack.loads(data)
self.plans = {}
for plan in data.get(b"plans", []):
plan = Plan.loads(plan)
self.plans[plan.key] = plan
self.time_plans = defaultdict(dict)
for key, plans in data.get(b"time_plans", {}).items():
self.time_plans[key] = {}
for plan_key in (plans or []):
if not isinstance(plan_key, unicode_type):
plan_key = unicode_type(plan_key, "utf-8")
if plan_key in self.plans:
self.time_plans[key][plan_key] = self.plans[plan_key]
current_time = data.get(b"current_time")
self.current_time = current_time if current_time and current_time > 0 else self.current_time
logging.info("store mem load session %s", self.store_file)
except Exception as e:
logging.error("store mem load session error: %s %s", self.store_file, e)
示例8: test_json
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def test_json(self):
ts = msprime.simulate(10, random_seed=1)
tables = ts.dump_tables()
nodes = tables.nodes
# For each node, we create some Python metadata that can be JSON encoded.
metadata = [
{"one": j, "two": 2 * j, "three": list(range(j))} for j in range(len(nodes))
]
encoded, offset = tskit.pack_strings(map(json.dumps, metadata))
nodes.set_columns(
flags=nodes.flags,
time=nodes.time,
population=nodes.population,
metadata_offset=offset,
metadata=encoded,
)
self.assertTrue(np.array_equal(nodes.metadata_offset, offset))
self.assertTrue(np.array_equal(nodes.metadata, encoded))
ts1 = tables.tree_sequence()
for j, node in enumerate(ts1.nodes()):
decoded_metadata = json.loads(node.metadata.decode())
self.assertEqual(decoded_metadata, metadata[j])
ts1.dump(self.temp_file)
ts2 = tskit.load(self.temp_file)
self.assertEqual(ts1.tables.nodes, ts2.tables.nodes)
示例9: test_pickle
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def test_pickle(self):
ts = msprime.simulate(10, random_seed=1)
tables = ts.dump_tables()
# For each node, we create some Python metadata that can be pickled
metadata = [
{"one": j, "two": 2 * j, "three": list(range(j))}
for j in range(ts.num_nodes)
]
encoded, offset = tskit.pack_bytes(list(map(pickle.dumps, metadata)))
tables.nodes.set_columns(
flags=tables.nodes.flags,
time=tables.nodes.time,
population=tables.nodes.population,
metadata_offset=offset,
metadata=encoded,
)
self.assertTrue(np.array_equal(tables.nodes.metadata_offset, offset))
self.assertTrue(np.array_equal(tables.nodes.metadata, encoded))
ts1 = tables.tree_sequence()
for j, node in enumerate(ts1.nodes()):
decoded_metadata = pickle.loads(node.metadata)
self.assertEqual(decoded_metadata, metadata[j])
ts1.dump(self.temp_file)
ts2 = tskit.load(self.temp_file)
self.assertEqual(ts1.tables.nodes, ts2.tables.nodes)
示例10: test_mutations
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def test_mutations(self):
tables = tskit.TableCollection(sequence_length=1)
metadata = ExampleMetadata(one="node1", two="node2")
pickled = pickle.dumps(metadata)
tables.nodes.add_row(time=0)
tables.sites.add_row(position=0.1, ancestral_state="A")
tables.mutations.add_row(site=0, node=0, derived_state="T", metadata=pickled)
ts = tables.tree_sequence()
mutation = ts.site(0).mutations[0]
self.assertEqual(mutation.site, 0)
self.assertEqual(mutation.node, 0)
self.assertEqual(mutation.derived_state, "T")
self.assertEqual(mutation.metadata, pickled)
unpickled = pickle.loads(mutation.metadata)
self.assertEqual(unpickled.one, metadata.one)
self.assertEqual(unpickled.two, metadata.two)
示例11: send
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def send(hub, worker, payload):
'''
Send the given payload to the given worker, yield iterations based on the
returns from the remote.
'''
mp = msgpack.dumps(payload, use_bin_type=True)
mp += hub.proc.DELIM
reader, writer = await asyncio.open_unix_connection(path=worker['path'])
writer.write(mp)
await writer.drain()
final_ret = True
while True:
ret = await reader.readuntil(hub.proc.DELIM)
p_ret = ret[:-len(hub.proc.DELIM)]
i_flag = p_ret[-1:]
ret = msgpack.loads(p_ret[:-1], raw=False)
if i_flag == hub.proc.D_FLAG:
# break for the end of the sequence
break
yield ret
final_ret = False
if final_ret:
yield ret
示例12: un
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def un(byts):
'''
Use msgpack to de-serialize a python object.
Args:
byts (bytes): The bytes to de-serialize
Notes:
String objects are decoded using utf8 encoding. In order to handle
potentially malformed input, ``unicode_errors='surrogatepass'`` is set
to allow decoding bad input strings.
Returns:
obj: The de-serialized object
'''
# This uses a subset of unpacker_kwargs
return msgpack.loads(byts, use_list=False, raw=False, unicode_errors='surrogatepass')
示例13: add_to_refmap
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def add_to_refmap(self, result: ResultStorer) -> None:
"""
:param result: the result of the compare function
This method verifies whether the comparison in input is the best available for the
candidate reference gene, and loads it into the refmap dictionary if such is the case.
"""
if result is not None and result.ccode != ("u",):
gid, tid = result.ref_gene[0], result.ref_id[0]
if gid not in self.gene_matches:
self.gene_matches[gid] = dict()
if tid not in self.gene_matches[gid]:
self.gene_matches[gid][tid] = list()
self.gene_matches[gid][tid].append(result)
return
示例14: loads
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def loads(buf):
"""
Args:
buf: the output of `dumps`.
"""
# Since 0.6, the default max size was set to 1MB.
# We change it to approximately 1G.
return msgpack.loads(buf, raw=False,
max_bin_len=MAX_MSGPACK_LEN,
max_array_len=MAX_MSGPACK_LEN,
max_map_len=MAX_MSGPACK_LEN,
max_str_len=MAX_MSGPACK_LEN)
示例15: load
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import loads [as 别名]
def load(cls, file_h, *args, **kwargs):
"""
Load encoded data from the specified file.
:param file file_h: The file to read and load encoded data from.
:param bool strict: Do not try remove trailing commas from the JSON data.
:return: The Python object represented by the encoded data.
"""
return cls.loads(file_h.read(), *args, **kwargs)