Skip to content

Commit a433ef4

Browse files
committed
Modularize custom symbols to tqdm_custom
Signed-off-by: Stephen L. <[email protected]>
1 parent 96c782c commit a433ef4

3 files changed

Lines changed: 302 additions & 87 deletions

File tree

tqdm/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
from ._tqdm import trange
33
from ._tqdm_gui import tqdm_gui
44
from ._tqdm_gui import tgrange
5+
from ._tqdm_custom import tqdm_custom
6+
from ._tqdm_custom import tcrange
57
from ._tqdm_pandas import tqdm_pandas
68
from ._main import main
79
from ._version import __version__ # NOQA
810
from ._tqdm import TqdmTypeError, TqdmKeyError, TqdmDeprecationWarning
911

1012
__all__ = ['tqdm', 'tqdm_gui', 'trange', 'tgrange', 'tqdm_pandas',
11-
'tqdm_notebook', 'tnrange', 'main',
13+
'tqdm_notebook', 'tnrange', 'tqdm_custom', 'tcrange', 'main',
1214
'TqdmTypeError', 'TqdmKeyError', 'TqdmDeprecationWarning',
1315
'__version__']
1416

tqdm/_tqdm.py

Lines changed: 9 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def print_status(s):
121121
@staticmethod
122122
def format_meter(n, total, elapsed, ncols=None, prefix='',
123123
ascii=False, unit='it', unit_scale=False, rate=None,
124-
bar_format=None, custom_symbols=None):
124+
bar_format=None):
125125
"""
126126
Return a string-based progress bar given some parameters
127127
@@ -251,47 +251,15 @@ def format_meter(n, total, elapsed, ncols=None, prefix='',
251251
N_BARS = max(1, ncols - len(l_bar) - len(r_bar)) if ncols \
252252
else 10
253253

254-
# custom symbols format
255-
# need to provide both ascii and unicode versions of custom symbols
256-
if custom_symbols:
257-
# get ascii or unicode template
258-
if ascii:
259-
c_symb = custom_symbols[1]
260-
else:
261-
c_symb = custom_symbols[2]
262-
# looping symbols: just update the symbol animation at each iteration
263-
if custom_symbols[0] == 'loop':
264-
# increment one step in the animation at each step
265-
bar = c_symb[divmod(n, len(c_symb))[1]]
266-
frac_bar = ''
267-
268-
bar_length = N_BARS # avoid the filling
269-
frac_bar_length = len(frac_bar)
270-
# normal progress symbols
271-
else:
272-
nb_symb = len(c_symb)
273-
len_filler = len(c_symb[-1])
274-
bar_length, frac_bar_length = divmod(
275-
int((frac/len_filler) * N_BARS * nb_symb), nb_symb)
276-
277-
bar = c_symb[-1] * bar_length # last symbol is always the filler
278-
frac_bar = c_symb[frac_bar_length] if frac_bar_length \
279-
else ' '
280-
# update real bar length (if symbols > 1 char) for correct filler
281-
bar_length = bar_length * len_filler
282-
283-
# ascii format
284-
elif ascii:
285-
# get the remainder of the division of current fraction with number of symbols
286-
# this will tell us which symbol we should pick
254+
# format bar depending on availability of unicode/ascii chars
255+
if ascii:
287256
bar_length, frac_bar_length = divmod(
288257
int(frac * N_BARS * 10), 10)
289258

290259
bar = '#' * bar_length
291260
frac_bar = chr(48 + frac_bar_length) if frac_bar_length \
292261
else ' '
293262

294-
# unicode format (if available)
295263
else:
296264
bar_length, frac_bar_length = divmod(int(frac * N_BARS * 8), 8)
297265

@@ -302,7 +270,7 @@ def format_meter(n, total, elapsed, ncols=None, prefix='',
302270
# whitespace padding
303271
if bar_length < N_BARS:
304272
full_bar = bar + frac_bar + \
305-
' ' * max(N_BARS - bar_length - len(frac_bar), 0)
273+
' ' * max(N_BARS - bar_length - 1, 0)
306274
else:
307275
full_bar = bar + \
308276
' ' * max(N_BARS - bar_length, 0)
@@ -610,46 +578,6 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,
610578
if ascii is None:
611579
ascii = not _supports_unicode(file)
612580

613-
614-
# Custom symbols extraction
615-
custom_symbols = None
616-
if bar_format:
617-
looping = None
618-
c_symbols_ascii = None
619-
c_symbols_unicode = None
620-
found_tag = False
621-
for tag in ['bar_symbols', 'bar_symbols_ascii', 'bar_symbols_loop', 'bar_symbols_loop_ascii']:
622-
start_tag = '{' + tag + '}'
623-
end_tag = '{/' + tag + '}'
624-
# Check if tag is found in the template
625-
if start_tag in bar_format and end_tag in bar_format:
626-
found_tag = True
627-
# Extract custom symbols enclosed by tags, with the first character being the separator.
628-
# Eg, extract_symbols('before{start},1,2,3,4,#{end}after', '{start}', '{end}')
629-
start = bar_format.find(start_tag)
630-
start_content = start+len(start_tag)
631-
end_content = bar_format.find(end_tag)
632-
end = end_content+len(end_tag)
633-
sep = bar_format[start_content:start_content+1]
634-
c_symbols = bar_format[start_content+1:end_content].split(sep)
635-
# Cleanup all weird tags from bar_format else .format() crash
636-
bar_format = bar_format[:start] + bar_format[end:]
637-
638-
if 'ascii' in tag:
639-
c_symbols_ascii = c_symbols
640-
else:
641-
c_symbols_unicode = c_symbols
642-
643-
# Looping symbol?
644-
if 'loop' in tag:
645-
looping = True
646-
else:
647-
looping = False
648-
649-
# Compile the ascii/unicode bars in a nice argument for format_meter
650-
if found_tag:
651-
custom_symbols = ['loop' if looping else 'bar', c_symbols_ascii, c_symbols_unicode]
652-
653581
if bar_format and not ascii:
654582
# Convert bar format into unicode since terminal uses unicode
655583
bar_format = _unicode(bar_format)
@@ -678,7 +606,6 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,
678606
self.avg_time = None
679607
self._time = time
680608
self.bar_format = bar_format
681-
self.custom_symbols = custom_symbols
682609

683610
# Init the iterations counters
684611
self.last_print_n = initial
@@ -695,8 +622,7 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,
695622
self.moveto(self.pos)
696623
self.sp(self.format_meter(self.n, total, 0,
697624
(dynamic_ncols(file) if dynamic_ncols else ncols),
698-
self.desc, ascii, unit, unit_scale, None,
699-
bar_format, custom_symbols))
625+
self.desc, ascii, unit, unit_scale, None, bar_format))
700626
if self.pos:
701627
self.moveto(-self.pos)
702628

@@ -723,8 +649,7 @@ def __repr__(self):
723649
time() - self.last_print_t,
724650
self.ncols, self.desc, self.ascii, self.unit,
725651
self.unit_scale, 1 / self.avg_time
726-
if self.avg_time else None,
727-
self.bar_format, self.custom_symbols)
652+
if self.avg_time else None, self.bar_format)
728653

729654
def __lt__(self, other):
730655
# try:
@@ -781,7 +706,6 @@ def __iter__(self):
781706
smoothing = self.smoothing
782707
avg_time = self.avg_time
783708
bar_format = self.bar_format
784-
custom_symbols = self.custom_symbols
785709
_time = self._time
786710
format_meter = self.format_meter
787711

@@ -820,8 +744,7 @@ def __iter__(self):
820744
(dynamic_ncols(self.fp) if dynamic_ncols
821745
else ncols),
822746
self.desc, ascii, unit, unit_scale,
823-
1 / avg_time if avg_time else None,
824-
bar_format, custom_symbols))
747+
1 / avg_time if avg_time else None, bar_format))
825748

826749
if self.pos:
827750
self.moveto(-self.pos)
@@ -907,7 +830,7 @@ def update(self, n=1):
907830
else self.ncols),
908831
self.desc, self.ascii, self.unit, self.unit_scale,
909832
1 / self.avg_time if self.avg_time else None,
910-
self.bar_format, self.custom_symbols))
833+
self.bar_format))
911834

912835
if self.pos:
913836
self.moveto(-self.pos)
@@ -974,7 +897,7 @@ def fp_write(s):
974897
(self.dynamic_ncols(self.fp) if self.dynamic_ncols
975898
else self.ncols),
976899
self.desc, self.ascii, self.unit, self.unit_scale, None,
977-
self.bar_format, self.custom_symbols))
900+
self.bar_format))
978901
if pos:
979902
self.moveto(-pos)
980903
else:

0 commit comments

Comments
 (0)