Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Some dumb tests and a fix #9

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject

# PyCharm
.idea
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Note
----

Please use this version https://github.com/kmike/tqdm as it contains other useful pull requests not merged by the author of the library.

tqdm
====

Expand Down Expand Up @@ -31,3 +36,9 @@ def trange(*args, **kwargs):
"""A shortcut for writing tqdm(xrange)"""
return tqdm(xrange(*args), **kwargs)
```

Running tests
-------------

Please make sure tox (http://tox.testrun.org/) is installed and type
`tox` from the command line
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
python_files = test*.py
70 changes: 70 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from __future__ import unicode_literals

import csv
import sys
from six import StringIO
from tqdm import format_interval, format_meter, tqdm


def test_format_interval():
assert format_interval(60) == '01:00'
assert format_interval(6160) == '1:42:40'
assert format_interval(238113) == '66:08:33'


def test_format_meter():
assert format_meter(0, 1000, 13) == \
"|----------| 0/1000 0% [elapsed: " \
"00:13 left: ?, 0.00 iters/sec]"
assert format_meter(231, 1000, 392) == \
"|##--------| 231/1000 23% [elapsed: " \
"06:32 left: 21:44, 0.59 iters/sec]"


def test_nothing_fails():
""" Just make sure we're able to iterate using tqdm """
for i in tqdm(range(10)):
pass


def test_iterate_over_csv_rows():
# Create a test csv pseudo file
test_csv_file = StringIO()
writer = csv.writer(test_csv_file)
for i in range(3):
writer.writerow(['test', 'test', 'test'])
test_csv_file.seek(0)

# Test that nothing fails if we iterate over rows
reader = csv.DictReader(test_csv_file, fieldnames=('row1', 'row2', 'row3'))
for row in tqdm(reader):
pass


def test_file_output():
""" Tests that output to arbitrary file-like objects works """
our_file = StringIO()
for i in tqdm(range(3), file=our_file):
if i == 1:
our_file.seek(0)
assert '0/3' in our_file.read()


def test_leave_option():
"""
Tests that if leave=True, tqdm will leave the info
about the last iteration on the screen
"""
our_file = StringIO()
for i in tqdm(range(3), file=our_file, leave=True):
pass
our_file.seek(0)
assert '3/3 100%' in our_file.read()
our_file.close()

our_file2 = StringIO()
for i in tqdm(range(3), file=our_file2, leave=False):
pass
our_file2.seek(0)
assert '3/3 100%' not in our_file2.read()
our_file2.close()
13 changes: 13 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tox (http://tox.testrun.org/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.

[tox]
envlist = py26, py27, py32

[testenv]
commands = py.test
deps =
six
pytest
9 changes: 6 additions & 3 deletions tqdm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
__all__ = ['tqdm', 'trange']
from __future__ import division

import sys
import time


__all__ = ['tqdm', 'trange']


def format_interval(t):
mins, s = divmod(int(t), 60)
h, m = divmod(mins, 60)
Expand All @@ -17,7 +20,7 @@ def format_meter(n, total, elapsed):
# n - number of finished iterations
# total - total number of iterations, or None
# elapsed - number of seconds passed since start
if n > total:
if total and n > total:
total = None

elapsed_str = format_interval(elapsed)
Expand Down Expand Up @@ -71,7 +74,7 @@ def tqdm(iterable, desc='', total=None, leave=False, file=sys.stderr,
if total is None:
try:
total = len(iterable)
except TypeError:
except (TypeError, AttributeError):
total = None

prefix = desc+': ' if desc else ''
Expand Down