stqdm icon indicating copy to clipboard operation
stqdm copied to clipboard

[BUG] stqdm in for loop with the enumerate function

Open apolinario opened this issue 2 years ago • 2 comments

Describe the bug

stqdm doesn't work on a python loop that uses the enumerate function

To Reproduce

values = ["a", "b", "c"]
for count, value in stqdm(enumerate(values)):
    sleep(1)

doesn't work.

However

values = ["a", "b", "c"]
for value in stqdm(values)):
    sleep(1)

works normally

apolinario avatar Mar 13 '22 21:03 apolinario

Also more broadly, even without the enumerate function, it doesn't work when the for loop is iterating over any generator it seems

apolinario avatar Mar 13 '22 21:03 apolinario

Hi @apolinario!

Thanks for the report and for the examples. Sorry for the late reply.

Also more broadly, even without the enumerate function, it doesn't work when the for loop is iterating over any generator it seems

To be exact, it's about iteratables without length. When you pass your list into enumerate you lose the initial length of the object.

stqdm & tqdm have no way to know when your object will end and can not implement a progress bar. In this situation, tqdm just return the number of iteration by seconds and the total number of iterations in text. Like this :

0it [00:00, ?it/s]
1it [00:01,  1.00s/it]
2it [00:02,  1.00s/it]
3it [00:03,  1.00s/it]

I can and will probably implement this in the next release. Is this what you will be expecting?

If you want the full progress bar tho, you have 2 solutions :

  • provide the expected number of iterations using total (tqdm API) :
from stqdm import stqdm
from time import sleep

values = ["a", "b", "c"]
for count, value in stqdm(enumerate(values), total=len(values)):
    sleep(1)
  • in your case, add the enumerate outside stqdm call so that stqdm/tqdm can take the length of the original object as a default total
from stqdm import stqdm
from time import sleep

values = ["a", "b", "c"]
for count, value in enumerate(stqdm(values)):
    sleep(1)

Wirg avatar Mar 25 '22 09:03 Wirg

In the incoming v0.0.5 version, there should be a fix that display the iteration speed without a real progress as there is no total or length.

image

Wirg avatar Sep 19 '22 11:09 Wirg