Exemple #1
0
 def test_expiry(self):
     fifo = CircularFifo(3)
     assert None == fifo.append(1)
     assert None == fifo.append(2)
     assert None == fifo.append(3)
     for i in range(4, 10):
         assert i - 3 == fifo.append(i)
Exemple #2
0
 def test_expiry(self):
     fifo = CircularFifo(3)
     assert None == fifo.append(1)
     assert None == fifo.append(2)
     assert None == fifo.append(3)
     for i in range(4, 10):
         assert i - 3 == fifo.append(i)
Exemple #3
0
 def test_pop(self):
     fifo = CircularFifo(3)
     for i in range(1, 3):
         for j in range(i):
             assert None == fifo.append(j)
         for j in range(i):
             popped = fifo.pop()
             assert j == popped, "{0} {1} {2}".format(i, j, popped)
     for i in range(4):
         fifo.append(i)
     assert 1 == fifo.pop()
Exemple #4
0
 def test_pop(self):
     fifo = CircularFifo(3)
     for i in range(1, 3):
         for j in range(i):
             assert None == fifo.append(j)
         for j in range(i):
             popped = fifo.pop()
             assert j == popped, '{0} {1} {2}'.format(i, j, popped)
     for i in range(4):
         fifo.append(i)
     assert 1 == fifo.pop()
Exemple #5
0
 def test_list(self):
     fifo = CircularFifo(3)
     for i in range(7):
         fifo.append(i)
     assert [4, 5, 6] == list(fifo)
     fifo.append(7)
     assert [5, 6, 7] == list(fifo)
     fifo.append(8)
     assert [6, 7, 8] == list(fifo)
     fifo.append(9)
     assert [7, 8, 9] == list(fifo)
Exemple #6
0
 def test_list(self):
     fifo = CircularFifo(3)
     for i in range(7):
         fifo.append(i)
     assert [4, 5, 6] == list(fifo)
     fifo.append(7)
     assert [5, 6, 7] == list(fifo)
     fifo.append(8)
     assert [6, 7, 8] == list(fifo)
     fifo.append(9)
     assert [7, 8, 9] == list(fifo)
Exemple #7
0
class _RecordDeepest(_TraceStack):
    '''
    A logger (implemented as a monitor - `MonitorInterface`)
    that records the deepest match found during a parse.
    '''
    
    def __init__(self, n_before=6, n_results_after=2, n_done_after=2):
        super(_RecordDeepest, self).__init__(enabled=True)
        self.n_before = n_before
        self.n_results_after = n_results_after
        self.n_done_after = n_done_after
        self._limited = CircularFifo(n_before)
        self._before = []
        self._results_after = []
        self._done_after = []
        self._deepest = -1e99
        self._countdown_result = 0
        self._countdown_done = 0
        
    def _log_result(self, value, text):
        '''
        Modify `TraceStack` to record the data.
        '''
        if type(value) is tuple:
            self.record(True, text)

    def _log_error(self, text):
        '''
        Modify `TraceStack` to record the data.
        '''
        self.record(True, text)

    def _log_done(self, text):
        '''
        Modify `TraceStack` to record the data.
        '''
        self.record(False, text)

    def record(self, is_result, text):
        '''
        Record the data.
        '''
        try:
            stream = self.generator.stream
            try:
                depth = s_delta(stream)[0]
            except AttributeError: # no .depth()
                depth = -1
            if depth >= self._deepest and is_result:
                self._deepest = depth
                self._countdown_result = self.n_results_after
                self._countdown_done = self.n_done_after
                self._before = list(self._limited)
                self._results_after = []
                self._done_after = []
            elif is_result and self._countdown_result:
                self._countdown_result -= 1
                self._results_after.append(text)
            elif not is_result and self._countdown_done:
                self._countdown_done -= 1
                self._done_after.append(text)
            self._limited.append(text)
        except StopIteration: # end of iterator stream
            pass
        
    def yield_(self, value):
        '''
        Display the result and reset.
        '''
        self._deepest = 0
        self._limited.clear()
        self.__display()
        
    def raise_(self, value):
        '''
        Display the result and reset.
        '''
        self._deepest = 0
        self._limited.clear()
        self.__display()
        
    def __display(self):
        '''
        Display the result.
        '''
        self._info(self.__fmt())
        
    def __fmt(self):
        '''
        fmt the result.
        '''
        return fmt(
            '\nUp to {0} matches before and including longest match:\n{1}\n'
            'Up to {2} failures following longest match:\n{3}\n'
            'Up to {4} successful matches following longest match:\n{5}\n',
            self.n_before, '\n'.join(self._before),
            self.n_done_after, '\n'.join(self._done_after),
            self.n_results_after, '\n'.join(self._results_after))
Exemple #8
0
class _RecordDeepest(_TraceStack):
    '''
    A logger (implemented as a monitor - `MonitorInterface`)
    that records the deepest match found during a parse.
    '''
    def __init__(self, n_before=6, n_results_after=2, n_done_after=2):
        super(_RecordDeepest, self).__init__(enabled=True)
        self.n_before = n_before
        self.n_results_after = n_results_after
        self.n_done_after = n_done_after
        self._limited = CircularFifo(n_before)
        self._before = []
        self._results_after = []
        self._done_after = []
        self._deepest = -1e99
        self._countdown_result = 0
        self._countdown_done = 0

    def _log_result(self, value, text):
        '''
        Modify `TraceStack` to record the data.
        '''
        if type(value) is tuple:
            self.record(True, text)

    def _log_error(self, text):
        '''
        Modify `TraceStack` to record the data.
        '''
        self.record(True, text)

    def _log_done(self, text):
        '''
        Modify `TraceStack` to record the data.
        '''
        self.record(False, text)

    def record(self, is_result, text):
        '''
        Record the data.
        '''
        try:
            stream = self.generator.stream
            try:
                depth = s_delta(stream)[0]
            except AttributeError:  # no .depth()
                depth = -1
            if depth >= self._deepest and is_result:
                self._deepest = depth
                self._countdown_result = self.n_results_after
                self._countdown_done = self.n_done_after
                self._before = list(self._limited)
                self._results_after = []
                self._done_after = []
            elif is_result and self._countdown_result:
                self._countdown_result -= 1
                self._results_after.append(text)
            elif not is_result and self._countdown_done:
                self._countdown_done -= 1
                self._done_after.append(text)
            self._limited.append(text)
        except StopIteration:  # end of iterator stream
            pass

    def yield_(self, value):
        '''
        Display the result and reset.
        '''
        self._deepest = 0
        self._limited.clear()
        self.__display()

    def raise_(self, value):
        '''
        Display the result and reset.
        '''
        self._deepest = 0
        self._limited.clear()
        self.__display()

    def __display(self):
        '''
        Display the result.
        '''
        self._info(self.__fmt())

    def __fmt(self):
        '''
        fmt the result.
        '''
        return fmt(
            '\nUp to {0} matches before and including longest match:\n{1}\n'
            'Up to {2} failures following longest match:\n{3}\n'
            'Up to {4} successful matches following longest match:\n{5}\n',
            self.n_before, '\n'.join(self._before), self.n_done_after,
            '\n'.join(self._done_after), self.n_results_after,
            '\n'.join(self._results_after))