wolph / python-progressbar

Progressbar 2 - A progress bar for Python 2 and Python 3 - "pip install progressbar2"
http://progressbar-2.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
859 stars 103 forks source link

MultiBar does not work if any of the node has an empty string as key #284

Closed micrenda closed 1 year ago

micrenda commented 1 year ago

Description

When an empty string is used as the key, this error message is displayed:

Traceback (most recent call last):
  File "/home/mrenda/tmp/./test_multibar.py", line 22, in <module>
    multibar.render()
  File "/home/mrenda/.venv/lib/python3.11/site-packages/progressbar/multi.py", line 208, in render
    update()
  File "/home/mrenda/.venv/lib/python3.11/site-packages/progressbar/multi.py", line 182, in update
    output.append(bar_.fd.line)
                  ^^^^^^^^^^^^
AttributeError: '_io.TextIOWrapper' object has no attribute 'line'

MultiBar should support an empty string as key or, if not possible, an assert should be added, so the issue is caught early.

Code

This code reproduces the issue:

import random
import threading
import time

import progressbar

BARS = 5
N = 50

multibar = progressbar.MultiBar()

for i in range(BARS):
    multibar[f'Thread label here {i}' if i != 0 else ''] = progressbar.ProgressBar(max_value=N)

for n in range(N):

    for name in multibar:
        bar = multibar[name]
        bar.update(n)
        time.sleep(0.1)

    multibar.render()

Ps. Feel also free to add a polished version code (without the empty key section) in the example to show how to create a non-threaded version of the MultiBar

Versions

wolph commented 1 year ago

Thank you very much for letting me know, I've fixed it right away and I'll push it soon (currently working on the testing/linting configuration)