Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to have shorter loops within larger loop #762

Closed
ethanabrooks opened this issue Jun 21, 2019 · 4 comments
Closed

Best way to have shorter loops within larger loop #762

ethanabrooks opened this issue Jun 21, 2019 · 4 comments

Comments

@ethanabrooks
Copy link

ethanabrooks commented Jun 21, 2019

This is a simple usage question. I have an outer loop

for i in itertools.count():

and there are events that occur at intervals:

if i % log_interval == 0:
    perform_logging()
if i % save_interval == 0:
    save()
# etc.

I would like to use tqdm to track the progress toward the next log/save/etc.
Ideally, tqdm would print several progress bars simultaneously, a la
image

What's the best way to do this? Thanks!

@ethanabrooks
Copy link
Author

Judging from #755 it looks like you prefer these questions to be asked on stackoverflow. Posted here: https://stackoverflow.com/questions/56704176/tqdm-with-intervals-inside-larger-loop

@casperdcl
Copy link
Member

yup thanks. note

  1. you'll probably need a nested manual (rather than iterable wrapper) tqdm
  2. itertools may need manual total too https://github.com/tqdm/tqdm#faq-and-known-issues

@casperdcl
Copy link
Member

casperdcl commented Jun 21, 2019

FYI

from __future__ import division, print_function
import itertools
from math import ceil
from tqdm import tqdm
from time import sleep

total = 987
log_interval = 2
save_interval = 9
with tqdm(total=total, desc="overall") as tOverall:
  with tqdm(total=ceil(total / log_interval), unit="log") as tLog:
    with tqdm(total=ceil(total / save_interval), unit="save") as tSave:
      for i in itertools.count(0, 1):
        sleep(0.01)
        if i % log_interval == 0:
          #perform_logging()
          tLog.update()
        if i % save_interval == 0:
          #save()
          tSave.update()
        if i + 1 == total:
          break
        tOverall.update()
print('\n')

@ethanabrooks
Copy link
Author

I ended up making a bit of an adjustment to this. For explanation and new version, see https://stackoverflow.com/a/56777870/4176597.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants