def test_disabled(): prog = ProgIter(range(20), enabled=True) prog.begin() assert prog.started prog = ProgIter(range(20), enabled=False) prog.begin() assert not prog.started
def test_tqdm_compatibility(): prog = ProgIter(range(20), total=20, miniters=17, show_times=False) assert prog.pos == 0 assert prog.freq == 17 for _ in prog: pass import ubelt as ub with ub.CaptureStdout() as cap: ProgIter.write('foo') assert cap.text.strip() == 'foo' with ub.CaptureStdout() as cap: prog = ProgIter(show_times=False) prog.set_description('new desc', refresh=False) prog.begin() prog.refresh() prog.close() assert prog.label == 'new desc' assert 'new desc' in cap.text.strip() with ub.CaptureStdout() as cap: prog = ProgIter(show_times=False) prog.set_description('new desc', refresh=True) prog.close() assert prog.label == 'new desc' assert 'new desc' in cap.text.strip() with ub.CaptureStdout() as cap: prog = ProgIter(show_times=False) prog.set_description_str('new desc') prog.begin() prog.refresh() prog.close() assert prog.label == 'new desc' assert 'new desc' in cap.text.strip() import ubelt as ub with ub.CaptureStdout() as cap: prog = ub.ProgIter(show_times=False) prog.set_postfix({'foo': 'bar'}, baz='biz', x=object(), y=2) prog.begin() assert prog.length is None assert 'foo=bar' in cap.text.strip() assert 'baz=biz' in cap.text.strip() assert 'y=2' in cap.text.strip() assert 'x=<object' in cap.text.strip() import ubelt as ub with ub.CaptureStdout() as cap: prog = ub.ProgIter(show_times=False) prog.set_postfix_str('bar baz', refresh=False) assert 'bar baz' not in cap.text.strip()
def test_adjust_freq(): # nothing to check (that I can think of) run test for coverage prog = ProgIter(range(20), enabled=True, eta_window=None) # Adjust frequency up to have each update happen every 1sec or so prog.freq = 1 prog.time_thresh = 1.0 prog._max_between_count = -1.0 prog._max_between_time = -1.0 prog._between_time = 1 prog._between_count = 1000 prog._adjust_frequency() assert prog.freq == 4 # Adjust frequency down to have each update happen every 1sec or so prog.freq = 1000 prog.time_thresh = 1.0 prog._max_between_count = -1.0 prog._max_between_time = -1.0 prog._between_time = 1 prog._between_count = 1 prog._adjust_frequency() assert prog.freq == 1 # No need to adjust frequency to have each update happen every 1sec or so prog.freq = 1 prog.time_thresh = 1.0 prog._max_between_count = -1.0 prog._max_between_time = -1.0 prog._between_time = 1 prog._between_count = 1 prog._adjust_frequency() assert prog.freq == 1
def test_clearline(): """ Make sure a question mark is printed if the total is unknown pytest ubelt/tests/test_progiter.py::test_clearline """ file = cStringIO() # Clearline=False version should simply have a newline at the end. prog = ProgIter(file=file, show_times=False, clearline=False) message = prog.format_message() assert strip_ansi(message).strip(' ') == '0/?... \n' # Clearline=True version should carrage return at the begining and have no # newline at the end. prog = ProgIter(file=file, show_times=False, clearline=True) message = prog.format_message() assert strip_ansi(message).strip(' ') == '\r 0/?...'
def test_progiter_offset_10(): """ pytest -s ~/code/ubelt/ubelt/tests/test_progiter.py::test_progiter_offset_10 xdoctest ~/code/ubelt/tests/test_progiter.py test_progiter_offset_10 """ # Define a function that takes some time file = cStringIO() list( ProgIter(range(10), total=20, verbose=3, start=10, file=file, freq=5, show_times=False)) file.seek(0) want = ['10/20...', '15/20...', '20/20...'] got = [line.strip() for line in file.readlines()] if sys.platform.startswith('win32'): # nocover # on windows \r seems to be mixed up with ansi sequences from xdoctest.utils import strip_ansi got = [strip_ansi(line).strip() for line in got] print('want = {!r}'.format(want)) print('got = {!r}'.format(got)) assert got == want
def test_initial(): """ Make sure a question mark is printed if the total is unknown """ file = cStringIO() prog = ProgIter(initial=9001, file=file, show_times=False, clearline=False) message = prog.format_message() assert strip_ansi(message) == ' 9001/?... \n'
def test_unknown_total(): """ Make sure a question mark is printed if the total is unknown """ iterable = (_ for _ in range(0, 10)) file = cStringIO() prog = ProgIter(iterable, desc='unknown seq', file=file, show_times=False, verbose=1) for n in prog: pass file.seek(0) got = [line.strip() for line in file.readlines()] # prints an eroteme if total is unknown assert len(got) > 0, 'should have gotten something' assert all('?' in line for line in got), 'all lines should have an eroteme'
def test_progiter(): from ubelt import Timer # Define a function that takes some time def is_prime(n): return n >= 2 and not any(n % i == 0 for i in range(2, n)) N = 500 if False: file = cStringIO() prog = ProgIter(range(N), clearline=False, file=file, freq=N // 10, adjust=False) file.seek(0) print(file.read()) prog = ProgIter(range(N), clearline=False) for n in prog: was_prime = is_prime(n) prog.set_extra('n=%r, was_prime=%r' % ( n, was_prime, )) if (n + 1) % 128 == 0 and was_prime: prog.set_extra('n=%r, was_prime=%r EXTRA' % ( n, was_prime, )) file.seek(0) print(file.read()) total = 200 # N = 5000 N = 500 N0 = N - total print('N = %r' % (N, )) print('N0 = %r' % (N0, )) print('\n-----') print('Demo #0: progress can be disabled and incur essentially 0 overhead') print('However, the overhead of enabled progress is minimal and typically ' 'insignificant') print('this is verbosity mode verbose=0') sequence = (is_prime(n) for n in range(N0, N)) with Timer('demo0'): psequence = ProgIter(sequence, total=total, desc='demo0', enabled=False) list(psequence) print('\n-----') print('Demo #1: progress is shown by default in the same line') print('this is verbosity mode verbose=1') sequence = (is_prime(n) for n in range(N0, N)) with Timer('demo1'): psequence = ProgIter(sequence, total=total, desc='demo1') list(psequence) # Default behavior adjusts frequency of progress reporting so # the performance of the loop is minimally impacted print('\n-----') print('Demo #2: clearline=False prints multiple lines.') print('Progress is only printed as needed') print('Notice the adjustment behavior of the print frequency') print('this is verbosity mode verbose=2') with Timer('demo2'): sequence = (is_prime(n) for n in range(N0, N)) psequence = ProgIter(sequence, total=total, clearline=False, desc='demo2') list(psequence) # import utool as ut # print(ut.repr4(psequence.__dict__)) print('\n-----') print('Demo #3: Adjustments can be turned off to give constant feedback') print('this is verbosity mode verbose=3') sequence = (is_prime(n) for n in range(N0, N)) with Timer('demo3'): psequence = ProgIter(sequence, total=total, adjust=False, clearline=False, freq=100, desc='demo3') list(psequence)
def test_eta_window_None(): # nothing to check (that I can think of) run test for coverage prog = ProgIter(range(20), enabled=True, eta_window=None) for _ in prog: pass
def test_tqdm_compatibility(): prog = ProgIter(range(20), total=20, miniters=17, show_times=False) assert prog.freq == 17 for _ in prog: pass