alphatwirl / atpbar

Progress bars for threading and multiprocessing tasks on terminal and Jupyter Notebook
https://alphatwirl.github.io/atpbar/
MIT License
93 stars 10 forks source link

While-loop-style iteration #7

Closed jbuckman closed 4 years ago

jbuckman commented 4 years ago

I'd like to use this progress bar when we know the total number of steps, but can increase by more than 1 on any given iteration. For example:

i = 0
while i < 100:
  i += random.randint(1,10)

So, I'd like to simply specify an ATPBar where we know the total, and manually increment it:

i = 0
mybar = atpbar.atpbar(max=100)
while i < 100:
  i += random.randint(1,10)
  mybar.set(i)

Is this possible?

TaiSakuma commented 4 years ago

Thank you for your inquiry.

It is not impossible to increment the counter by more than one in each iteration in a "while" loop.

However, I haven't implemented a clean user interface for such use case. (I intend to do so someday.)

In the current version of atpbar (1.0.4), it is possible to do, for example, with the following awkward code.

import time, random
import os, uuid

from atpbar import find_reporter, flush

pid = os.getpid()
taskid = uuid.uuid4()
reporter = find_reporter()
i = 0
while i < 100:
    i += random.randint(1, 10)
    report = dict(taskid=taskid, name='task name', done=i, total=100, pid=pid, in_main_thread=True)
    reporter.report(report)
    time.sleep(0.1)
flush()

The above code runs in the main thread of the main process.

It is not necessary to know the total in advance. You can give a different number to total in each iteration. The progress bar ends when you give the done a greater number than the total.

jbuckman commented 4 years ago

This failed with the following error:

Traceback (most recent call last):           
  File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner                                  
    self.run()                                                                                              
  File "/home/jacobbuckman/ENV/lib/python3.7/site-packages/atpbar/pickup.py", line 23, in run                       
    self._run_until_the_end_order_arrives()                                              
  File "/home/jacobbuckman/ENV/lib/python3.7/site-packages/atpbar/pickup.py", line 36, in _run_until_the_end_order_arrives
    self._process_report(report)                                                               
  File "/home/jacobbuckman/ENV/lib/python3.7/site-packages/atpbar/pickup.py", line 53, in _process_report
    self.presentation.present(report)                                                                                                                                                                     
  File "/home/jacobbuckman/ENV/lib/python3.7/site-packages/atpbar/presentation/base.py", line 35, in present
    self._present()                                                                        
  File "/home/jacobbuckman/ENV/lib/python3.7/site-packages/atpbar/presentation/bartty.py", line 41, in _present
    self._create_lines()                                                                       
  File "/home/jacobbuckman/ENV/lib/python3.7/site-packages/atpbar/presentation/bartty.py", line 55, in _create_lines
    line = self._create_line(report)                                                                                                                                                                      
  File "/home/jacobbuckman/ENV/lib/python3.7/site-packages/atpbar/presentation/bartty.py", line 70, in _create_line
    name=report['name'])                                                                 
ValueError: Unknown format code 'd' for object of type 'float'                                 

My code:

        pid = os.getpid()
        taskid = uuid.uuid4()
        reporter = find_reporter()
        while ....
            ....
            reporter.report(dict(taskid=taskid, name='Data Collection', done=collected, total=amount, pid=pid, in_main_thread=True))
        flush()

EDIT: Nevermind, easy fix. Just needed to cast collected to an int; it was a float.