本文整理汇总了Python中msgpack.Unpacker类的典型用法代码示例。如果您正苦于以下问题:Python Unpacker类的具体用法?Python Unpacker怎么用?Python Unpacker使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Unpacker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ClientProtocol
class ClientProtocol(asyncio.Protocol):
def __init__(self):
self._cpt = -1
self.packer = Unpacker()
self._responses = dict()
def connection_made(self, transport):
print("connected")
self.transport = transport
def request(self, name, args, f):
print("send request")
self._cpt += 1
self._responses[self._cpt] = f
self.transport.write(packb([0, self._cpt, name, args]))
def data_received(self, data):
self.packer.feed(data)
for msg in self.packer:
if msg[0] == 1:
self._responses[msg[1]].set_result(msg)
def connection_lost(self, exc):
pass
示例2: MsgpackProtocol
class MsgpackProtocol(asyncio.Protocol):
def __init__(self, routes):
self.__routes = routes
self.packer = Unpacker()
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
print('Connection from {}'.format(peername))
self.transport = transport
self.transport.write(packb([2, 'peername', peername]))
def data_received(self, data):
self.packer.feed(data)
for msg in self.packer:
assert_request(msg)
self.routing(msg)
def routing(self, cmd):
assert cmd[2] in self.__routes
t = asyncio.ensure_future(response(cmd[1], self.transport,
self.__routes[cmd[2]], cmd[3]))
def eof_received(self):
return True
示例3: SReader
class SReader():
""" Define an asyncio msgpack stream decoder. """
def __init__(self, reader, writer):
""" Pass ina stream reader to unmarshall msgpack objects from. """
self.reader = reader
self.writer = writer
self.decoder = make_decoder()
self.unpacker = Unpacker(ext_hook=self.decoder, encoding="utf8")
self.obj_buf = []
@asyncio.coroutine
def get(self):
""" The co-routine providing objects. """
while len(self.obj_buf) == 0:
buf = yield from self.reader.read(1000)
self.unpacker.feed(buf)
for o in self.unpacker:
self.obj_buf.append(o)
return self.obj_buf.pop(0)
def put(self, obj):
""" Write an object to the channel. """
self.writer.write(encode(obj))
示例4: anomalies
def anomalies():
resp = 'handle_data([])'
try:
analyzer_key_node = REDIS_BACKENDS.get_node(settings.ANALYZER_ANOMALY_KEY)
anomaly_keys = RING.run('smembers', settings.ANALYZER_ANOMALY_KEY)
anomalies = {}
if not anomaly_keys:
logger.info("No anomaly key found!")
return resp, 200
for key in list(anomaly_keys):
raw_anomalies = RING.run('get',key)
if not raw_anomalies:
logger.info("Can't get anomalies for key %s, removing it from set" % key)
RING.run('srem', settings.ANALYZER_ANOMALY_KEY, key)
continue
unpacker = Unpacker(use_list = False)
unpacker.feed(raw_anomalies)
for item in unpacker:
anomalies.update(item)
anomaly_list = []
for anom, value in anomalies.iteritems():
anomaly_list.append([value, anom])
if len(anomaly_list) > 0:
anomaly_list.sort(key=operator.itemgetter(1))
resp = 'handle_data(%s)' % anomaly_list
except Exception as e:
logger.error("Error getting anomalies: %s" % str(e))
return resp, 200
示例5: test3
def test3():
start = 0
end = 10
metric = "marion.channel-0"
raw_series = REDIS_CONN.get(settings.FULL_NAMESPACE + metric)
if not raw_series:
resp = json.dumps({'results': 'Error: No metric by that name'})
return resp, 404
else:
unpacker = Unpacker(use_list = False)
unpacker.feed(raw_series)
timeseries = []
point = {'x':datapoint[0],'y':datapoint[1]}
if (start is None) and (end is not None):
for datapoint in unpacker:
if datapoint[0] < int(end):
timeseries.append(point)
elif (start is not None) and (end is None):
for datapoint in unpacker:
if datapoint[0] > int(start):
timeseries.append(point)
elif (start is not None) and (end is not None):
for datapoint in unpacker:
if (datapoint[0] > int(start)) and (datapoint[0] < int(end)):
timeseries.append(point)
elif (start is None) and (end is None):
timeseries = [{'x':datapoint[0],'y':datapoint[1]} for datapoint in unpacker]
resp = json.dumps({'results': timeseries})
return resp, 200
示例6: data
def data():
metric = request.args.get('metric', None)
start = request.args.get('start', None)
end = request.args.get('end', None)
if metric is None:
metrics = ['channel-0', 'channel-1', 'channel-2', 'channel-3', 'channel-4', 'channel-5', 'channel-6', 'channel-7']
else:
metrics = [metric]
try:
all_channels_data = []
for metric in metrics:
single_channel_data = {}
raw_series = REDIS_CONN.get(settings.FULL_NAMESPACE + metric)
if not raw_series:
resp = json.dumps({'results': 'Error: No metric by that name'})
return resp, 404
else:
unpacker = Unpacker(use_list = False)
unpacker.feed(raw_series)
timeseries = []
if (start is None) and (end is not None):
for datapoint in unpacker:
if datapoint[0] < int(end):
point = {'x' : datapoint[0], 'y':datapoint[1]}
timeseries.append(point)
elif (start is not None) and (end is None):
for datapoint in unpacker:
if datapoint[0] > int(start):
point = {'x' : datapoint[0], 'y':datapoint[1]}
timeseries.append(point)
elif (start is not None) and (end is not None):
for datapoint in unpacker:
if (datapoint[0] > int(start)) and (datapoint[0] < int(end)):
point = {'x' : datapoint[0], 'y':datapoint[1]}
timeseries.append(point)
elif (start is None) and (end is None):
timeseries = [{'x' : datapoint[0], 'y':datapoint[1]} for datapoint in unpacker]
single_channel_data['key'] = metric
single_channel_data['values'] = timeseries
all_channels_data.append(single_channel_data)
resp = json.dumps({'results': all_channels_data})
return resp, 200
except Exception as e:
error = "Error: " + e
resp = json.dumps({'results': error})
return resp, 500
except Exception as e:
error = "Error: " + e
resp = json.dumps({'results': error})
return resp, 500
示例7: test_incorrect_type_nested_map
def test_incorrect_type_nested_map():
unpacker = Unpacker()
unpacker.feed(packb([{"a": "b"}]))
try:
unpacker.read_map_header()
assert 0, "should raise exception"
except UnexpectedTypeException:
assert 1, "okay"
示例8: test_correct_type_nested_array
def test_correct_type_nested_array():
unpacker = Unpacker()
unpacker.feed(packb({"a": ["b", "c", "d"]}))
try:
unpacker.read_array_header()
assert 0, "should raise exception"
except UnexpectedTypeException:
assert 1, "okay"
示例9: mpdecode
def mpdecode(iterable):
unpacker = Unpacker(encoding='utf8')
for chunk in iterable:
unpacker.feed(chunk)
# Each chunk can have none or many objects,
# so here we dispatch any object ready
for obj in unpacker:
yield obj
示例10: __init__
def __init__(self, cb):
self.cb = cb
def listhook(obj):
return self.cb(obj)
self.listhook = listhook
Unpacker.__init__(self, list_hook=self.listhook)
示例11: test_incorrect_type_array
def test_incorrect_type_array():
unpacker = Unpacker()
unpacker.feed(packb(1))
try:
unpacker.read_array_header()
assert 0, 'should raise exception'
except UnexpectedTypeException:
assert 1, 'okay'
示例12: test_auto_max_array_len
def test_auto_max_array_len():
packed = b'\xde\x00\x06zz'
with pytest.raises(UnpackValueError):
unpackb(packed, raw=False)
unpacker = Unpacker(max_buffer_size=5, raw=False)
unpacker.feed(packed)
with pytest.raises(UnpackValueError):
unpacker.unpack()
示例13: setUp
def setUp(self):
address = 0xfa1afe1
device = "LivewareProblem"
raw_packet = encode_erase_flash_page(address, device)
unpacker = Unpacker()
unpacker.feed(raw_packet)
self.command = list(unpacker)[1:]
示例14: unpack_gen
def unpack_gen(file, size):
u = Unpacker()
while True:
data = file.read(size)
if not data:
break
u.feed(data)
for o in u:
yield o
示例15: test_write_bytes_multi_buffer
def test_write_bytes_multi_buffer():
long_val = (5) * 100
expected = packb(long_val)
unpacker = Unpacker(six.BytesIO(expected), read_size=3, max_buffer_size=3)
f = six.BytesIO()
unpacked = unpacker.unpack(f.write)
assert unpacked == long_val
assert f.getvalue() == expected