def test_getitem(): r = RingBuffer(5) r.extend([1, 2, 3]) r.extend_left([4, 5]) expected = np.array([4, 5, 1, 2, 3]) np.testing.assert_equal(r, expected) for i in range(r.maxlen): assert expected[i] == r[i] ii = [0, 4, 3, 1, 2] np.testing.assert_equal(r[ii], expected[ii])
def test_unwrap(): r = RingBuffer(5, dtype=np.int64) r.extend([1, 2, 3]) r.extend([4, 5, 6]) r.extend([7, 8, 9]) r.extend([10, 11, 12]) r.extend([13, 14, 15]) r.extend([16, 17, 18]) e = r.unwrap() np.testing.assert_equal(e, np.array([14, 15, 16, 17, 18]))
def test_extend(): r = RingBuffer(5) r.extend([1, 2, 3]) np.testing.assert_equal(r, np.array([1, 2, 3])) r.extend([4, 5, 6]) np.testing.assert_equal(r, np.array([2, 3, 4, 5, 6])) r.extend_left([0, 1]) np.testing.assert_equal(r, np.array([0, 1, 2, 3, 4])) r.extend_left([1, 2, 3, 4, 5, 6, 7]) np.testing.assert_equal(r, np.array([1, 2, 3, 4, 5])) r.extend([1, 2, 3, 4, 5, 6, 7]) np.testing.assert_equal(r, np.array([3, 4, 5, 6, 7]))
class RollingLogger(logging.Handler): def __init__(self, preferences, size=1000, parent=None): super().__init__() self.__buffer = RingBuffer(size, dtype=np.object) self.__signals = MessageSignals() self.__visible = False self.__window = None self.__preferences = preferences self.__excludes = [] self.parent = parent self.setFormatter( logging.Formatter( '%(asctime)s - %(levelname)s - %(threadName)s - %(name)s - %(funcName)s - %(message)s' )) level = self.__preferences.get(LOGGING_LEVEL) if level is not None and level in logging._nameToLevel: level = logging._nameToLevel[level] else: level = logging.INFO self.__root = self.__init_root_logger(level) self.__levelName = logging.getLevelName(level) def __init_root_logger(self, level): root_logger = logging.getLogger() root_logger.setLevel(level) root_logger.addHandler(self) return root_logger def emit(self, record): msg = self.format(record) if not any(e in msg for e in self.__excludes): self.__buffer.append(msg) self.__signals.append_msg.emit(msg) def show_logs(self): ''' Creates a new log viewer window. ''' if self.__window is None: self.__window = LogViewer(self.__buffer.maxlen) self.__window.set_size.connect(self.set_size) self.__window.change_level.connect(self.change_level) self.__window.set_exclude_filter.connect(self.set_excludes) self.__signals.append_msg.connect(self.__window.append_msg) level_idx = self.__window.logLevel.findText(self.__levelName) self.__window.logLevel.setCurrentIndex(level_idx) self.__window.show() self.__window.refresh(self.__buffer) def set_excludes(self, excludes): self.__excludes = excludes.split(',') if len(self.__excludes) > 0: old_buf = self.__buffer self.__buffer = RingBuffer(old_buf.maxlen, dtype=np.object) for m in old_buf: if any(e in m for e in self.__excludes): pass else: self.__buffer.append(m) if self.__window is not None: self.__window.refresh(self.__buffer) def set_size(self, size): ''' Changes the size of the log cache. ''' old_buf = self.__buffer self.__buffer = RingBuffer(size, dtype=np.object) self.__buffer.extend(old_buf) if self.__window is not None: self.__window.refresh(self.__buffer) def change_level(self, level): ''' Change the root logger level. :param level: the new level name. ''' logging.info(f"Changing log level from {self.__levelName} to {level}") self.__root.setLevel(level) self.__levelName = level self.__preferences.set(LOGGING_LEVEL, self.__levelName)