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] 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