本文整理汇总了Python中msgpack.Unpacker.feed方法的典型用法代码示例。如果您正苦于以下问题:Python Unpacker.feed方法的具体用法?Python Unpacker.feed怎么用?Python Unpacker.feed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msgpack.Unpacker
的用法示例。
在下文中一共展示了Unpacker.feed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_foobar
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
def test_foobar():
unpacker = Unpacker(read_size=3)
unpacker.feed('foobar')
assert_equal(unpacker.unpack(), ord('f'))
assert_equal(unpacker.unpack(), ord('o'))
assert_equal(unpacker.unpack(), ord('o'))
assert_equal(unpacker.unpack(), ord('b'))
assert_equal(unpacker.unpack(), ord('a'))
assert_equal(unpacker.unpack(), ord('r'))
try:
o = unpacker.unpack()
print "Oops!", o
assert 0
except StopIteration:
assert 1
else:
assert 0
unpacker.feed('foo')
unpacker.feed('bar')
k = 0
for o, e in zip(unpacker, 'foobarbaz'):
assert o == ord(e)
k += 1
assert k == len('foobar')
示例2: MsgpackProtocol
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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: ClientProtocol
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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
示例6: test_foobar
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
def test_foobar():
unpacker = Unpacker(read_size=3)
unpacker.feed("foobar")
assert unpacker.unpack() == ord(b"f")
assert unpacker.unpack() == ord(b"o")
assert unpacker.unpack() == ord(b"o")
assert unpacker.unpack() == ord(b"b")
assert unpacker.unpack() == ord(b"a")
assert unpacker.unpack() == ord(b"r")
try:
o = unpacker.unpack()
print "Oops!", o
assert 0
except StopIteration:
assert 1
else:
assert 0
unpacker.feed(b"foo")
unpacker.feed(b"bar")
k = 0
for o, e in zip(unpacker, b"foobarbaz"):
assert o == ord(e)
k += 1
assert k == len(b"foobar")
示例7: test3
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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
示例8: test_foobar
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
def test_foobar():
unpacker = Unpacker(read_size=3)
unpacker.feed(b'foobar')
assert unpacker.unpack() == ord(b'f')
assert unpacker.unpack() == ord(b'o')
assert unpacker.unpack() == ord(b'o')
assert unpacker.unpack() == ord(b'b')
assert unpacker.unpack() == ord(b'a')
assert unpacker.unpack() == ord(b'r')
try:
o = unpacker.unpack()
print(("Oops!", o))
assert 0
except StopIteration:
assert 1
else:
assert 0
unpacker.feed(b'foo')
unpacker.feed(b'bar')
k = 0
for o, e in zip(unpacker, b'foobarbaz'):
assert o == e
k += 1
assert k == len(b'foobar')
示例9: data
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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
示例10: test_incorrect_type_nested_map
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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'
示例11: test_incorrect_type_array
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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_correct_type_nested_array
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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"
示例13: mpdecode
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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
示例14: setUp
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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:]
示例15: unpack_gen
# 需要导入模块: from msgpack import Unpacker [as 别名]
# 或者: from msgpack.Unpacker import feed [as 别名]
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