Example #1
0
    def __call__(self, x):
        """Return element x and potentially print its value"""
        if not self.__should_print(x):
            return x

        self.cnt = 0  # reset counter
        self.time = time.time()  # reset timer

        fmtfunc = self.fmtfunc
        if hasattr(x, 'ndim'):  # is it a numpy array?
            x = x.tolist() if x.ndim else x.item()
        if not fmtfunc:
            text = x
        elif isinstance(fmtfunc, str):
            if isinstance(x, dict):
                text = fmtfunc.format(**x)
            else:
                text = fmtfunc.format(*(x if is_iterable(x) else [x]))
        elif hasattr(fmtfunc, '__call__'):
            text = fmtfunc(x)
        else:
            raise ValueError('Invalid format ' + str(fmtfunc))
        console(text, end=self.end)

        return x
Example #2
0
 def _print_batch(self, batches):
     for batch in batches:
         if len(batch) == 2:
             fmtstr = '[{}, {}]'
             console(fmtstr.format(batchstr(batch[0]), batchstr(batch[1])))
         else:
             console(batchstr(batch))
         yield batch
Example #3
0
 def __rrshift__(self, iterable):
     etafmt = '(eta: {:d}:{:02d}:{:02d})'
     endfmt = '(took: {:d}:{:02d}:{:02d})'
     start_time = time.time()
     up_time = time.time()
     for i, e in enumerate(iterable):
         if (time.time() - up_time) >= self.every_sec:
             up_time = time.time()
             per_done = int(100 * i / self.n)
             sec_consumed = int(time.time() - start_time)
             eta = sec_consumed * (self.n / float(i) - 1) if i else 0
             tstr = timestr(eta, etafmt)
             text = '\r{} {}% {}'.format(self.title, per_done, tstr)
             console(text, end='')
         yield e
     duration = int(time.time() - start_time)
     text = '\r{} 100% {}'.format(self.title, timestr(duration, endfmt))
     console(text)
Example #4
0
def test_console():
    with Redirect() as out:
        console('test')
    assert out.getvalue() == 'test\n'