本文整理汇总了Python中circular_buffer.CircularBuffer类的典型用法代码示例。如果您正苦于以下问题:Python CircularBuffer类的具体用法?Python CircularBuffer怎么用?Python CircularBuffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CircularBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Peer
class Peer(object):
def __init__(self, peer_id, socket):
self.peer_id = peer_id
self.socket = socket
self.pending_requests = {} # req_id -> ValueEvent
self.incoming_buffer = msgpack.Unpacker()
self.outgoing_buffer = CircularBuffer(OUTGOING_PEER_BUFFER_SIZE)
self.lock = threading.Lock()
def close(self):
try:
self.lock.release()
except threading.ThreadError:
pass # lock may not have been acquired.
self.socket.close()
def add_request(self, req_id, request_bytes, value_event):
"""Add ValueEvent to pending requests and write request_bytes to
outgoing_buffer"""
with self.lock:
self.pending_requests[req_id] = value_event
self.outgoing_buffer.write(request_bytes)
def handle_response(self, response):
"""Extract req_id from response and check if there is a pending request
for the req_id. If so set the value.
"""
with self.lock:
req_id, status, message = response
if req_id in self.pending_requests: # request may have timed out
self.pending_requests[req_id].set((status, message))
def remove_request(self, req_id):
with self.lock:
del self.pending_requests[req_id]
示例2: test_read_back_oldest_item
def test_read_back_oldest_item(self):
buf = CircularBuffer(3)
buf.write('1')
buf.write('2')
buf.read()
buf.write('3')
buf.read()
self.assertEqual('3', buf.read())
示例3: __init__
def __init__(self, peer_id, socket):
self.peer_id = peer_id
self.socket = socket
self.pending_requests = {} # req_id -> ValueEvent
self.incoming_buffer = msgpack.Unpacker()
self.outgoing_buffer = CircularBuffer(OUTGOING_PEER_BUFFER_SIZE)
self.lock = threading.Lock()
示例4: test_overwrite_full_buffer
def test_overwrite_full_buffer(self):
buf = CircularBuffer(2)
buf.write('1')
buf.write('2')
buf.overwrite('A')
self.assertEqual('2', buf.read())
self.assertEqual('A', buf.read())
with self.assertRaises(BufferEmptyException):
buf.read()
示例5: test_overwrite_replaces_oldest_item
def test_overwrite_replaces_oldest_item(self):
buf = CircularBuffer(2)
buf.write('1')
buf.write('2')
buf.overwrite('3')
self.assertEqual(buf.read(), '2')
self.assertEqual(buf.read(), '3')
示例6: test_write_and_read_back_multiple_items
def test_write_and_read_back_multiple_items(self):
buf = CircularBuffer(2)
buf.write('1')
buf.write('2')
self.assertEqual('1', buf.read())
self.assertEqual('2', buf.read())
with self.assertRaises(BufferEmptyException):
buf.read()
示例7: test_write_full_buffer
def test_write_full_buffer(self):
buf = CircularBuffer(2)
buf.write('1')
buf.write('2')
with self.assertRaises(BufferFullException):
buf.write('A')
示例8: test_read_just_written_item
def test_read_just_written_item(self):
buf = CircularBuffer(1)
buf.write('1')
self.assertEqual(buf.read(), '1')
示例9: test_alternate_write_and_read
def test_alternate_write_and_read(self):
buf = CircularBuffer(2)
buf.write('1')
self.assertEqual('1', buf.read())
buf.write('2')
self.assertEqual('2', buf.read())
示例10: test_clearing_buffer
def test_clearing_buffer(self):
buf = CircularBuffer(3)
for c in '123':
buf.write(c)
buf.clear()
with self.assertRaises(BufferEmptyException):
buf.read()
buf.write('1')
buf.write('2')
self.assertEqual('1', buf.read())
buf.write('3')
self.assertEqual('2', buf.read())
示例11: range
is_init = False
niveau_count = 0
platau_count = 0
print "signal walking"
for i in range(1, buffer.size-1):
ksum = 0
for k in range(-1,2):
w = -k
ksum += buffer.value(i+k) * w
print ksum
if is_init:
diff = ksum - last_ksum
last_ksum = ksum
is_init = True
size = 10
data = [0 for i in range(size)]
buffer = CircularBuffer(data, size)
for i in range(25):
#buffer.add((i-5)**2.0)
buffer.add(i)
peak_detect(buffer)
示例12: test_write_and_read_back_one_item
def test_write_and_read_back_one_item(self):
buf = CircularBuffer(1)
buf.write('1')
self.assertEqual('1', buf.read())
with self.assertRaises(BufferEmptyException):
buf.read()
示例13: test_read_empty_buffer
def test_read_empty_buffer(self):
buf = CircularBuffer(1)
with self.assertRaises(BufferEmptyException):
buf.read()
示例14: test_write_and_read_back_multiple_items_ordered
def test_write_and_read_back_multiple_items_ordered(self):
buf = CircularBuffer(2)
buf.write('1')
buf.write('2')
self.assertEqual(buf.read(), '1')
self.assertEqual(buf.read(), '2')
示例15: test_overwrite_non_full_buffer
def test_overwrite_non_full_buffer(self):
buf = CircularBuffer(2)
buf.write('1')
buf.overwrite('2')
self.assertEqual(buf.read(), '1')
self.assertEqual(buf.read(), '2')