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

desc field is ignored by update #26

Closed davidhyman closed 4 years ago

davidhyman commented 4 years ago

Describe the bug When updating a progress bar (Counter) I would like to update the desc field so that it's reflected in the formatted output:

I'd like a progress bar, but with text on it, describing the current action:

with enlighten.get_manager() as manager:
    steps_bar_format = "{desc: <20}{desc_pad}{percentage:3.0f}%|{bar}| {count:{len_total}d}/{total:d}"
    steps_bar = manager.counter(total=4, desc="progress", unit="steps", bar_format=steps_bar_format)
    steps_bar.update(force=True, desc="loading framework")

currently the description for this bar will remain as "progress":

progress              25%|████████████████▊                                                  | 1/4

I'm unsure if this is an intended api to work around it, but I can achieve what I want with:

with enlighten.get_manager() as manager:
    steps_bar_format = "{desc: <20}{desc_pad}{percentage:3.0f}%|{bar}| {count:{len_total}d}/{total:d}"
    steps_bar = manager.counter(total=4, unit="steps", bar_format=steps_bar_format)
    steps_bar.desc = "loading framework"
    steps_bar.update(force=True)

giving:

loading framework     25%|████████████████▊                                                  | 1/4

Currently I think this might be a bug because the fields are documented as being settable from the update call.

Environment:

enlighten==1.6.0

Additional context On having a cursory browse, I am not sure whether the self._fields update should come after the ln504 update, or whether that second update would be better off checking the contents of the fields dictionary for each item e.g. something like

fields["desc"] = fields["desc"] or self.desc or u''

I imagine this may also affect those other fields e.g. unit, unit pad.

Alternatively, if modifying the desc field isn't intended, perhaps the documentation could be updated to remove the affected fields from the "available fields".

https://github.com/Rockhopper-Technologies/enlighten/blob/master/enlighten/_counter.py#L504

Also, thanks for this repo - this project is real progress! A step in the right direction. The best bar, bar none.

avylove commented 4 years ago

desc is not a user-defined field, so it can't be overwritten in update(). See the API documentation.

You have two options. The first one is, like in your example, is to set the desc attribute explicitly. The alternative is to specify a different name from desc, such a description, in bar_format. As long as it's not the name of a standard field, you can use it.

avylove commented 4 years ago

What might be useful is to detect when standard fields are specified and emit a warning.

davidhyman commented 4 years ago

Thanks for the prompt response, that works, and makes sense (and of course, format will protect from any missing arguments).

with enlighten.get_manager() as manager:
    steps_bar_format = "{description: <20}{desc_pad}{percentage:3.0f}%|{bar}| {count:{len_total}d}/{total:d}"
    steps_bar = manager.counter(total=4, unit="steps", bar_format=steps_bar_format)
    steps_bar.update(force=True, description="loading framework")

Agreed about the warning for 'reserved fields' or similar, would be nice to have. Please feel free to close this ticket!

avylove commented 4 years ago

I'll leave this open for now to remind me to add the warning.

avylove commented 4 years ago

Warning code is in master. It was a bit more involved than I thought due to the way keywords are treated in Enlighten. Should be out in a release soon.

avylove commented 4 years ago

Warnings released in 1.6.1