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 test_sizes(): r = RingBuffer(5, dtype=(int, 2)) assert r.maxlen == 5 assert len(r) == 0 assert r.shape == (0, 2) r.append([0, 0]) assert r.maxlen == 5 assert len(r) == 1 assert r.shape == (1, 2)
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 __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 test_degenerate(): r = RingBuffer(0) np.testing.assert_equal(r, np.array([])) # this does not error with deque(maxlen=0), so should not error here try: r.append(0) r.append_left(0) except IndexError: assert True is False
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_2d(): r = RingBuffer(5, dtype=(np.float, 2)) r.append([1, 2]) np.testing.assert_equal(r, np.array([[1, 2]])) assert len(r) == 1 assert np.shape(r), (1 == 2) r.append([3, 4]) np.testing.assert_equal(r, np.array([[1, 2], [3, 4]])) assert len(r) == 2 assert np.shape(r), (2 == 2) r.append_left([5, 6]) np.testing.assert_equal(r, np.array([[5, 6], [1, 2], [3, 4]])) assert len(r) == 3 assert np.shape(r), (3 == 2) np.testing.assert_equal(r[0], [5, 6]) np.testing.assert_equal(r[0, :], [5, 6]) np.testing.assert_equal(r[:, 0], [5, 1, 3])
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)
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]))
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_append_left(): r = RingBuffer(5) r.append_left(1) np.testing.assert_equal(r, np.array([1])) assert len(r) == 1 r.append_left(2) np.testing.assert_equal(r, np.array([2, 1])) assert len(r) == 2 r.append_left(3) r.append_left(4) r.append_left(5) np.testing.assert_equal(r, np.array([5, 4, 3, 2, 1])) assert len(r) == 5 r.append_left(6) np.testing.assert_equal(r, np.array([6, 5, 4, 3, 2])) assert len(r) == 5
def test_append(): r = RingBuffer(5) r.append(1) np.testing.assert_equal(r, np.array([1])) assert len(r) == 1 r.append(2) np.testing.assert_equal(r, np.array([1, 2])) assert len(r) == 2 r.append(3) r.append(4) r.append(5) np.testing.assert_equal(r, np.array([1, 2, 3, 4, 5])) assert len(r) == 5 r.append(6) np.testing.assert_equal(r, np.array([2, 3, 4, 5, 6])) assert len(r) == 5 assert r[4] == 6 assert r[-1] == 6
def test_repr(): r = RingBuffer(5, dtype=np.int) for i in range(5): r.append(i) assert repr(r), '<RingBuffer of array([0, 1, 2, 3 == 4])>'
def test_iter(): r = RingBuffer(5) for i in range(5): r.append(i) for i, j in zip(r, range(5)): assert i == j