예제 #1
0
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)
예제 #2
0
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
예제 #3
0
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])
예제 #4
0
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
예제 #5
0
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)
예제 #6
0
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])>'
예제 #7
0
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