From 2b4d73526bfd6d87b26aa950652b04d3d0bb1d65 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Fri, 19 Jun 2015 13:37:14 +0200 Subject: [PATCH 1/4] attempt to set ncols from terminal environs --- tqdm/_tqdm.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index e3da1a7d..e892bdb3 100644 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -175,6 +175,26 @@ def tqdm(iterable, desc=None, total=None, leave=False, file=sys.stderr, except (TypeError, AttributeError): total = None + if (ncols is None) and (file in (sys.stderr, sys.stdout)): + try: + from termios import TIOCGWINSZ + from fcntl import ioctl + from array import array + except ImportError: + pass + else: + try: + ncols = array('h', ioctl(file, TIOCGWINSZ, '\0' * 8))[1] + except SystemExit: + raise + except: + try: + from os import environ + except ImportError: + pass + else: + ncols = int(environ.get('COLUMNS', 1)) - 1 + if miniters is None: miniters = 0 dynamic_miniters = True From 18c430b3d6561778fb3f55f0c1414d1c3874b393 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Thu, 25 Jun 2015 15:38:29 +0100 Subject: [PATCH 2/4] initial attempt to modularise utils --- tqdm/_tqdm.py | 30 ++---------------------------- tqdm/_utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 tqdm/_utils.py diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index e892bdb3..a470db7b 100644 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -8,6 +8,7 @@ Usage: ... """ from __future__ import division, absolute_import +from _utils import _is_utf, _supports_unicode, _environ_cols import sys import time @@ -17,16 +18,6 @@ __author__ = {"github.com/": ["noamraph", "JackMc", "arkottke", "obiwanus", __all__ = ['tqdm', 'trange', 'format_interval', 'format_meter'] -def _is_utf(encoding): - return ('U8' == encoding) or ('utf' in encoding) or ('UTF' in encoding) - - -def _supports_unicode(file): - if not getattr(file, 'encoding', None): - return False - return _is_utf(file.encoding) - - def format_interval(t): mins, s = divmod(int(t), 60) h, m = divmod(mins, 60) @@ -176,24 +167,7 @@ def tqdm(iterable, desc=None, total=None, leave=False, file=sys.stderr, total = None if (ncols is None) and (file in (sys.stderr, sys.stdout)): - try: - from termios import TIOCGWINSZ - from fcntl import ioctl - from array import array - except ImportError: - pass - else: - try: - ncols = array('h', ioctl(file, TIOCGWINSZ, '\0' * 8))[1] - except SystemExit: - raise - except: - try: - from os import environ - except ImportError: - pass - else: - ncols = int(environ.get('COLUMNS', 1)) - 1 + ncols = _environ_cols() if miniters is None: miniters = 0 diff --git a/tqdm/_utils.py b/tqdm/_utils.py new file mode 100644 index 00000000..f71ec04f --- /dev/null +++ b/tqdm/_utils.py @@ -0,0 +1,27 @@ +def _is_utf(encoding): + return ('U8' == encoding) or ('utf' in encoding) or ('UTF' in encoding) + + +def _supports_unicode(file): + if not getattr(file, 'encoding', None): + return False + return _is_utf(file.encoding) + + +def _environ_cols(): # pragma: no cover + try: + from termios import TIOCGWINSZ + from fcntl import ioctl + from array import array + except ImportError: + return None + else: + try: + return array('h', ioctl(file, TIOCGWINSZ, '\0' * 8))[1] + except: + try: + from os import environ + except ImportError: + return None + else: + return int(environ.get('COLUMNS', 1)) - 1 From 21912c821ccb984f79c911f7cb89698a111662ab Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Thu, 25 Jun 2015 16:49:49 +0100 Subject: [PATCH 3/4] import bugfix --- tqdm/_tqdm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index a470db7b..76c9f9d8 100644 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -8,7 +8,7 @@ Usage: ... """ from __future__ import division, absolute_import -from _utils import _is_utf, _supports_unicode, _environ_cols +from ._utils import _supports_unicode, _environ_cols import sys import time From c58a7c3f216c0a681f6abc0ce95b0eb97c77b08f Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Thu, 25 Jun 2015 16:56:34 +0100 Subject: [PATCH 4/4] moved more to utils --- tqdm/_tqdm.py | 18 ++++-------------- tqdm/_utils.py | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index 76c9f9d8..e33463c3 100644 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -8,7 +8,7 @@ Usage: ... """ from __future__ import division, absolute_import -from ._utils import _supports_unicode, _environ_cols +from ._utils import _supports_unicode, _environ_cols, _range, _unich import sys import time @@ -81,13 +81,8 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False): else: bar_length, frac_bar_length = divmod(int(frac * N_BARS * 8), 8) - try: # pragma: no cover - unich = unichr - except NameError: # pragma: no cover - unich = chr - - bar = unich(0x2588)*bar_length - frac_bar = unich(0x2590 - frac_bar_length) \ + bar = _unich(0x2588)*bar_length + frac_bar = _unich(0x2590 - frac_bar_length) \ if frac_bar_length else ' ' if bar_length < N_BARS: @@ -217,9 +212,4 @@ def trange(*args, **kwargs): A shortcut for tqdm(xrange(*args), **kwargs). On Python3+ range is used instead of xrange. """ - try: # pragma: no cover - f = xrange - except NameError: # pragma: no cover - f = range - - return tqdm(f(*args), **kwargs) + return tqdm(_range(*args), **kwargs) diff --git a/tqdm/_utils.py b/tqdm/_utils.py index f71ec04f..41f78b5f 100644 --- a/tqdm/_utils.py +++ b/tqdm/_utils.py @@ -1,3 +1,15 @@ +try: # pragma: no cover + _range = xrange +except NameError: # pragma: no cover + _range = range + + +try: # pragma: no cover + _unich = unichr +except NameError: # pragma: no cover + _unich = chr + + def _is_utf(encoding): return ('U8' == encoding) or ('utf' in encoding) or ('UTF' in encoding) @@ -20,8 +32,8 @@ def _environ_cols(): # pragma: no cover return array('h', ioctl(file, TIOCGWINSZ, '\0' * 8))[1] except: try: - from os import environ + from os.environ import get except ImportError: return None else: - return int(environ.get('COLUMNS', 1)) - 1 + return int(get('COLUMNS', 1)) - 1