Rockhopper-Technologies / enlighten

Enlighten Progress Bar for Python Console Apps
https://python-enlighten.readthedocs.io
Mozilla Public License 2.0
416 stars 25 forks source link

Counter Attempt to Format the desc and unit Arguments #45

Closed ZivRonen closed 2 years ago

ZivRonen commented 2 years ago

Describe the bug Enlighten Counter try to format the description/unit fields - it will cause issues if '{' or '}' are present, in most cases raising ValueError. This happen in both progress bar and counter modes.

To Reproduce

"""
Progress bar
"""
import enlighten
with enlighten.Counter(total=1, desc='A{') as pbar:
    pass
"""
Raw counter
"""
import enlighten
with enlighten.Counter(unit='B}') as pbar:
    pass

Environment (please complete the following information):

Additional context N/A

avylove commented 2 years ago

This is because Enlighten uses str.format() internally to do variable substitution. To escape a curly brace, you need to double it.

with enlighten.Counter(total=1, desc='A{{') as pbar:
    pass

Let me know if that resolves your issue. This should probably be in the FAQ.

ZivRonen commented 2 years ago

Yes. I just thought it can be solved in the library, or at least be documented (if it was, I missed it).

Note: In my case the values are generated dynamically (os.walk of an unknown client) so I used:

with enlighten.Counter(total=1, desc=path.replace('{', '{{').replace('}', '}}')) as pbar:
   pass

which solved the issue for me.

Thanks for the quick response

P.S: If you want, I can create a pull request to solve this more generally by perform format twice. It will however slow the run a little, and will change the behavior as '{0}' will no longer be replaced in the description/unit (which I assume is unintentional, but I am not sure)

avylove commented 2 years ago

I just pushed a commit to take care of it. Let me know if it works for you. It would probably still be a good idea to add tests to protect against regression.

ZivRonen commented 2 years ago

Checked it on my sample and it worked great! Also checked the code and it looked fine to me, I don't see a real case that someone will use the new place holder by mistake :)

avylove commented 2 years ago

Released in 1.10.2

ZivRonen commented 2 years ago

Thank you!