def test_count(): buf = CircularBuffer(32) buf.write(b'asdf\r\njkl;\r\n1234\r\n') assert buf.count(b'\r\n') == 3 buf.clear() buf.write(b'asdf\r\njkl;\r\n1234\r\na') assert buf.count(b'\r\n') == 3
def test_init(self): c = CircularBuffer(3) assert c.max_size == 3 assert c.size == 0 assert c.start == 0 assert c.end == 0 assert c.is_empty() is True assert c.is_full() is False
def test_rollover(self): cb = CircularBuffer(5, ['A', 'B', 'C', 'D', 'E']) assert cb.front() == 'A' assert cb.length() == 5 assert cb.is_full() is True cb.enqueue('X') assert cb.front() == 'X' assert cb.length() == 5 assert cb.is_full() is True
def test_create(): buf = CircularBuffer(BUFFER_SIZE) assert isinstance(buf, CircularBuffer) assert buf.write_available() == BUFFER_SIZE assert len(buf) == 0 assert str(buf) == '' buf = CircularBuffer(size=BUFFER_SIZE) assert isinstance(buf, CircularBuffer) assert repr(buf) == '<CircularBuffer[0]:>'
def test_init(self): buf = CircularBuffer(5, [0, 1, 2, 3, 4]) assert len(buf) == 5 buf = CircularBuffer(5, [0, 1, 2, 3, 4, 5, 6, 7]) assert len(buf) == 5 buf = CircularBuffer(5, [0, 1, 2]) assert len(buf) == 3 buf = CircularBuffer(5, [0, 'hi', 2, 3, 4, 'why', 6, 7]) assert len(buf) == 5
def test_is_empty(self): c = CircularBuffer(3) assert c.is_empty() is True c.enqueue('A') assert c.is_empty() is False c.dequeue() assert c.is_empty() is True
def test_is_empty(self): cb = CircularBuffer(2) assert cb.is_empty() cb.enqueue('test') assert not cb.is_empty() cb.dequeue() assert cb.is_empty()
def test_size(self): cb = CircularBuffer() assert cb.size == 0 cb.enqueue('A') assert cb.size == 1 cb.enqueue('B') assert cb.size == 2 cb.dequeue() assert cb.size == 1 cb.dequeue() assert cb.size == 0
def add(self, value): """ Adds value to buffer, overwrite as needed. """ if self.isFull(): delta = -self.buffer[self.low] deltaSquare = -(self.buffer[self.low]**2) else: delta = 0 deltaSquare = 0 delta += value deltaSquare += value**2 self.total += delta self.sumSquare += deltaSquare CircularBuffer.add(self, value)
def test_properties(self): cb = CircularBuffer(4) cb.enqueue('a') assert cb.size == 1 assert cb.list == ['a', None, None, None] cb.enqueue('b') assert cb.size == 2 assert cb.list == ['a', 'b', None, None] cb.enqueue('c') assert cb.size == 3 assert cb.list == ['a', 'b', 'c', None] cb.enqueue('d') assert cb.size == 4 assert cb.list == ['a', 'b', 'c', 'd']
def test_init(self): cb = CircularBuffer(2) assert cb.size == 0 assert cb.max == 2 assert cb.list == [None, None] assert cb.head is None assert cb.tail is None
def test_init_with_list(self): cb = CircularBuffer(['A', 'B', 'C']) assert cb.write == 3 assert cb.read == 0 assert cb.buffer[0] == 'A' assert cb.buffer[1] == 'B' assert cb.buffer[2] == 'C'
def test_dequeue(self): cb = CircularBuffer(4) cb.enqueue(3) cb.enqueue(1) cb.enqueue(4) cb.enqueue(1) assert cb.dequeue() == 3 assert cb.dequeue() == 1 assert cb.dequeue() == 4 assert cb.dequeue() == 1
def test_dequeue(self): cb = CircularBuffer(['A', 'B', 'C']) assert cb.dequeue() == 'A' assert cb.size == 2 assert cb.dequeue() == 'B' assert cb.size == 1 assert cb.dequeue() == 'C' assert cb.size == 0 assert cb.is_empty() is True with self.assertRaises(ValueError): cb.dequeue()
def test_sms(): buf = CircularBuffer(1024) regex_OK = compile(br'^\r\nOK\r\n') buf.write(b'\r\nOK\r\n') process_buffer(regex_OK, buf, True) buf.write(b'\r\nOK\r\n') process_buffer(regex_OK, buf, True) buf.write(b'\r\n+CGML:') process_buffer(regex_OK, buf, False)
def test_repr(): buf = CircularBuffer(70) buf.write(b'a' * 32) assert len(buf) == 32 assert repr(buf) == '<CircularBuffer[32]:' + ('a' * 32) + '>' assert str(buf) == 'a' * 32 buf.read(32) assert len(buf) == 0 buf.write(b'b' * 64) assert len(buf) == 64 assert repr(buf) == '<CircularBuffer[64]:' + ('b' * 41) + '..>' assert str(buf) == 'b' * 64
def __init__(self, loop, mgr): self.mgr = mgr self._loop = loop self._buf = CircularBuffer(conf['connection']['buffer_size']) log_name = type(self).__name__ if self.name is not None: log_name += '(%s)' % self.name self._log = getLogger(log_name)
def test_sequence(): buf = CircularBuffer(10) buf.write(b'12345') assert buf[0] == b'1' try: buf[6] except Exception as e: assert isinstance(e, IndexError) assert len(buf) == 5 buf[3] = b'1' assert str(buf) == '12315' assert b'15' in buf assert buf[1:1] == b'' assert buf[1:2] == b'2' assert buf[1:] == b'2315' assert buf[:1] == b'1' assert buf[:-1] == b'1231' assert buf[2:-1] == b'31' assert buf[2:-1:2] == b'3' assert buf[5:1:-2] == b'53'
def test_buffer(): buf = CircularBuffer(15) regex = re.compile(br'(5)') # one segment assert buf.write(b'12345') == 5 with buf as buf_: match = regex.search(buf_) assert match is not None # floated one segment assert buf.read(5) == b'12345' assert buf.write(b'67890') == 5 with buf as buf_: match = regex.search(buf_) assert match is None # two segments assert buf.write(b'1234567') == 7 with buf: match = regex.search(buf) assert match is not None # python2 forced to use context manager if sys.version_info[0] < 3: with raises(TypeError): match = regex.search(buf) else: match = regex.search(buf)
def __init__(self, dbpath, log_format='%(levelname)s:%(asctime)s%(message)s', log_filename=False): # Clean the path before setting it self.dbpath = dbpath # self._lock=Lock() self._lock = False # TODO: make these shared! kid_list = [[0., '']] * params.buffer_length self.kid_buf = CircularBuffer(lst=kid_list) """Latest modified KIDs""" self.nkid = 0 """Current kid index""" log_list = [[0, 0, '', '']] * params.buffer_length self.log_buf = CircularBuffer(lst=log_list) """Latest log lines""" self.nlog = 0 """Current log index""" self.main_logger = False self.log_filename = False self.log_format = log_format self.set_logger(log_format, log_filename=log_filename)
def test_enqueue_and_dequeue(self): abcd = ['a', 'b', 'c', 'd'] cb = CircularBuffer(4) for i in range(4): cb.enqueue(abcd[i]) assert cb.size == i + 1 for i in range(3, 0): cb.dequeue() assert cb.size == i + 1
def __init__(self, nchan=1, sample_freq=1.0, buffer_secs=10.0, init_value=None, fps=30, title="Stream Plot", **kwargs): super(LivePlotWidget, self).__init__(**kwargs) if self.parent() is None: #We are the top level window self.setGeometry(0, 0, 800, 600) self.setWindowTitle(title) self._sample_period = 1.0 / sample_freq self._last_redraw_time = 0.0 self._buf_capacity = int(round(buffer_secs / self._sample_period)) + 1 self._snap_to_latest = True self._nchan = nchan self._data = [ CircularBuffer(self._buf_capacity, init_value=init_value) for _ in range(nchan) ] self._time = self._sample_period * (np.arange(self._buf_capacity) - self._buf_capacity - 1) self._legend = self.getPlotItem().addLegend() self._curves = [] for chan in range(nchan): pen_color = self.DEFAULT_PEN_COLORS[chan % len(self.DEFAULT_PEN_COLORS)] self._curves.append( self.plot(self._time, self._data[chan].data(), pen=QtGraph.mkPen(pen_color, width=1), name=f'{chan}')) self.disableAutoRange() self.setXRange(self._time[0], self._time[-1] + self._sample_period, padding=0) self.showGrid(True, True) self.timer = QtCore.QTimer(self) self.timer.setInterval(1000.0 / fps) self.timer.timeout.connect(self.redraw) self.scene().sigMouseClicked.connect(self.onMouseClicked)
def remove(self): """ Removes Oldest Value from non-empty buffer """ removed = CircularBuffer.remove(self) self.total -= removed return removed
def setUp(self): self.cb = CircularBuffer(4)
class CircularBufferTest(unittest.TestCase): def setUp(self): self.cb = CircularBuffer(4) def test_enqueue(self): self.cb.enqueue('one') self.cb.enqueue('two') self.cb.enqueue('three') self.cb.enqueue('four') self.cb.enqueue('!!!!') #Create a __eq__function assert self.cb.get_at_index(0) == '!!!!' def test_size(self): self.cb.enqueue(101) self.cb.enqueue(202) self.cb.enqueue(303) print(self.cb.size()) assert self.cb.size() == 3 self.cb.enqueue(404) self.cb.enqueue(505) assert self.cb.size() == 4
def test_make_contiguous(): buf = CircularBuffer(10) #'# '# assert buf.write(b'1234') == 4 #'1234# '# buf.make_contiguous() assert str(buf) == '1234' assert buf.write(b'5678') == 4 #'12345678# '# buf.make_contiguous() assert str(buf) == '12345678' assert buf.read(2) == b'12' #' 345678# '# assert buf.write(b'9012') == 4 #'2#345678901'# assert str(buf) == '3456789012' buf.make_contiguous() #'3456789012#'# assert str(buf) == '3456789012' assert buf.read(7) == b'3456789' #' 012#'# assert str(buf) == '012' assert buf.write(b'1234567') == 7 #'234567#0121'# assert str(buf) == '0121234567' buf.make_contiguous() #'0121234567#'# assert str(buf) == '0121234567'
f = [] ff = open('data/filter.txt', 'r') for d in ff: f.append(float(d)) print 'Filter is ' + str(f) # load the data file fd = open('data/signal.txt', 'r') input = [] output = [] correct = [10.0, 14.0, 17.0, 18.0, 18.1] # make a circular buffer of size 5 c = CircularBuffer(len(f)) for d in fd: input.append(int(d)) c.push(int(d)) # convolve the filter with values in the buffer y = zip(f, c) a = sum( map(lambda x: x[0] * x[1], y)) output.append( a ) print "Original signal is:" print input print "Convolved signal is:" print output
def test_resize(): buf = CircularBuffer(10) assert buf.write_available() == 10 buf.resize(5) assert buf.write_available() == 10 buf.resize(15) assert buf.write_available() == 15 buf.resize(size=20) assert buf.write_available() == 20
def test_sequence_two_sections(): buf = CircularBuffer(10) assert buf.write(b'1234567890') == 10 assert buf.write_available() == 0 assert len(buf) == 10 assert str(buf) == '1234567890' # where the first section is longer than the second assert buf.read(3) == b'123' assert buf.write(b'123') == 3 assert buf.write_available() == 0 assert len(buf) == 10 assert str(buf) == '4567890123' # where the first section is equal length with the second assert buf.read(2) == b'45' assert buf.write(b'12') == 2 assert buf.write_available() == 0 assert len(buf) == 10 assert str(buf) == '6789012312' # where the first section is shorter than the second assert buf.read(3) == b'678' assert buf.write(b'123') == 3 assert buf.write_available() == 0 assert len(buf) == 10 assert str(buf) == '9012312123' # where the first section is only one byte assert buf.read(1) == b'9' assert buf.write_available() == 1 assert len(buf) == 9 assert str(buf) == '012312123'
def test_find(): buf = CircularBuffer(32) buf.write(b'asdf\r\njkl;\r\n1234\r\n') assert buf.find(b'\r\n') == 4 assert buf.find(b'x') == -1 assert buf.find(b'\r\n', 5) == 10 buf.clear() buf.write(b'asdf\r\njkl;\r\n1234\r\na') assert buf.find(b'\r\n') == 4 assert buf.find(b'x') == -1 assert buf.find(b'\r\n', 5) == 10 with raises(ValueError): buf.find(b'')
def test_read_write(): buf = CircularBuffer(15) #'# #' assert buf.write_available() == 15 assert buf.write(b'12345') == 5 #'12345# #' assert buf.write_available() == 10 assert str(buf) == '12345' assert buf.read(2) == b'12' #' 345# #' assert buf.write_available() == 12 assert str(buf) == '345' assert buf.read(size=3) == b'345' #' # #' assert buf.write_available() == 15 assert str(buf) == '' assert buf.write(b'1234567890') == 10 #' 1234567890##' assert buf.write_available() == 5 assert str(buf) == '1234567890' assert buf.write(b'12') == 2 #'2# 12345678901#' assert buf.write_available() == 3 assert str(buf) == '123456789012' assert buf.write(b'123456') == 3 #'2123#12345678901#' assert buf.write_available() == 0 assert str(buf) == '123456789012123' assert buf.resize(20) == 20 #'2123#12345678901 #' assert buf.write_available() == 0 assert str(buf) == '123456789012123' assert buf.read(10) == b'1234567890' #'2123# 1 #' assert buf.write_available() == 10 assert str(buf) == '12123' assert buf.read(2) == b'12' #' 123# #' assert buf.write_available() == 17 assert str(buf) == '123'
def test_front(self): c = CircularBuffer(3) assert c.front() is None c.enqueue('A') assert c.front() == 'A' c.enqueue('B') assert c.front() == 'A' c.dequeue() assert c.front() == 'B' c.dequeue() assert c.front() is None c.enqueue('C') assert c.front() == 'C'
def test_dequeue(self): c = CircularBuffer(3) with self.assertRaises(ValueError): c.dequeue() c.enqueue('A') c.enqueue('B') c.enqueue('C') assert c.dequeue() == 'A' c.enqueue('D') assert c.dequeue() == 'B' assert c.dequeue() == 'C' c.enqueue('E') c.enqueue('F') assert c.dequeue() == 'D' assert c.dequeue() == 'E' assert c.dequeue() == 'F' with self.assertRaises(ValueError): c.dequeue()
class Database(object): out = False """Buffering object for data output""" def __init__(self, dbpath, log_format='%(levelname)s:%(asctime)s%(message)s', log_filename=False): # Clean the path before setting it self.dbpath = dbpath # self._lock=Lock() self._lock = False # TODO: make these shared! kid_list = [[0., '']] * params.buffer_length self.kid_buf = CircularBuffer(lst=kid_list) """Latest modified KIDs""" self.nkid = 0 """Current kid index""" log_list = [[0, 0, '', '']] * params.buffer_length self.log_buf = CircularBuffer(lst=log_list) """Latest log lines""" self.nlog = 0 """Current log index""" self.main_logger = False self.log_filename = False self.log_format = log_format self.set_logger(log_format, log_filename=log_filename) def set_logger(self, log_format='%(levelname)s:%(asctime)s%(message)s', log_filename=False): self.log_format = log_format self.formatter = logging.Formatter(log_format) self.main_logger = logging.getLogger('misura') self.main_logger.setLevel(logging.DEBUG) if not len(self.main_logger.handlers): handler = logging.StreamHandler() handler.setFormatter(self.formatter) self.main_logger.addHandler(handler) else: print 'Already defined logging handlers', self.main_logger.handlers if not log_filename: return self.log_filename = log_filename self.addFileHandler(log_filename, params.log_file_dimension, params.log_backup_count) def __getstate__(self): d = self.__dict__.copy() for k in ['main_logger', 'formatter']: if d.has_key(k): del d[k] return d def __setstate__(self, state): map(lambda a: setattr(self, *a), state.items()) if 'log_filename' in state and 'log_format' in state: self.set_logger(state['log_format'], state['log_filename']) def addFileHandler(self, filename, max_dim=2 * 10**6, backups=10): """Adds logrotate logging""" print 'Database.addFileHandler', filename, self.log_format handler = logging.handlers.RotatingFileHandler(filename, maxBytes=max_dim, backupCount=backups) handler.setFormatter(self.formatter) self.main_logger.addHandler(handler) def close(self): pass # LOGGING FUNCTIONS def get_log(self, fromt=False, priority=0, owner=False, tot=False, maxn=None): """Search in the log buffer. Require priority equal or bigger than `priority` and emitted by `owner`. Maximum time `tot`, maximum number of entries `maxn`.""" t, obuf = utils.get_history(self.log_buf, fromt) buf = [] if type(obuf[0]) == type(1.): obuf = [obuf] if tot is False: tot = obuf[-1][0] + 1 for b in obuf: if b[0] > fromt and b[0] < tot: buf.append(b) if not owner and priority == 0: return t, buf[:maxn] r = [] # time, prio, owner, msg for l in buf: if len(l) < 3: break if priority > 0: if l[1] < priority: continue if owner: if l[2] != owner: continue if l is None: l = 'None' r.append(l) return t, r[:maxn] def put_log(self, *msg, **po): """General, short-memory logging""" t, st, p, o, msg, pmsg = logger.formatMsg(*msg, **po) self.log_buf.append([t, p, o, pmsg]) self.nlog += 1 self.main_logger.log(p, pmsg) return p, msg
def __init__(self, size): """Stores buffer in given storage""" CircularBuffer.__init__(self, size) self.total = 0 self.sumSquare = 0