Python Progress Bar ValueError: Value out of range
Asked Answered
B

5

6

My progress bar reaches 100% and then throws the error

from progressbar import Percentage, ProgressBar,Bar,ETA

pbar = ProgressBar(widgets=[Bar('=', '[', ']'), ' ',
                                            Percentage(), ' ',
                                            ETA()]).start()
            for i,row in enumerate(cursor):

               '''
                do some work here

               ''' 

                pbar.update(i)

here is what i get

Traceback (most recent call last):=========================] 100% ETA:  0:00:00
  File "X:\src\dbtest\PymssqlCheck.py", line 27, in <module>
    fiddler.getRows(condetails, dbdetails, 'compliance', 'doctable', '*', '1000')
  File "X:\src\utilities\fiddler.py", line 45, in getRows
    pbar.update(i)
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 271, in update
    raise ValueError('Value out of range')
ValueError: Value out of range

why does it reach 100% and then fail? i am using

https://github.com/niltonvolpato/python-progressbar

i even tried

 i=0                                
            for row in cursor:

                ''' do some work here ''' 

                if i < numrows:
                    pbar.update(i)
                    i=i+1

but i still get the same error

Edit

i tried Tomasz Jakub Rup answer

pbar = ProgressBar(widgets=[Bar('=', '[', ']'), ' ',
                                        Percentage(), ' ',
                                        ETA()])
for row in pbar(cursor):
    ''' do some work here ''' 

and i get

File "X:\fiddler.py", line 41, in getRows
    for row in pbar(cursor):
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 180, in __next__
    if self.start_time is None: self.start()
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 311, in start
    self.update(0)
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 283, in update
    self.fd.write(self._format_line() + '\r')
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 243, in _format_line
    widgets = ''.join(self._format_widgets())
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 223, in _format_widgets
    widget = format_updatable(widget, self)
  File "X:\Anaconda2\lib\site-packages\progressbar\widgets.py", line 38, in format_updatable
    if hasattr(updatable, 'update'): return updatable.update(pbar)
  File "X:\Anaconda2\lib\site-packages\progressbar\widgets.py", line 184, in update
    return '%3d%%' % pbar.percentage()
  File "X:\Anaconda2\lib\site-packages\progressbar\__init__.py", line 208, in percentage
    return self.currval * 100.0 / self.maxval
TypeError: unsupported operand type(s) for /: 'float' and 'classobj'

any idea why?

Boabdil answered 20/11, 2015 at 19:57 Comment(2)
What is a cursor? List? Dict?Chufa
i think its a list. its the pymssql cursor for executing queriesBoabdil
B
3

David and Tomasz, both of you guys came pretty close. the solution that worked is

pbar = ProgressBar(widgets=[Bar('>', '[', ']'), ' ',
                                            Percentage(), ' ',
                                            ETA()],maxval=someMaxValue)
            for row in pbar(cursor):
                ''' do some work '''
Boabdil answered 20/11, 2015 at 20:45 Comment(1)
Great work! The documentation has a typo maxval <-max_val!!! Thank you!Scatterbrain
E
2

Because the progress bar is full a 100 by default. You should specify a maxval=N if you have N steps.

For example:

from progressbar import Percentage, ProgressBar,Bar,ETA

N = 300

pbar = ProgressBar(widgets=[Bar('=', '[', ']'), ' ', Percentage(), ' ', ETA()],
                   maxval=N).start()

for i in range(N+1):
    pbar.update(i)
Ecru answered 20/11, 2015 at 20:4 Comment(0)
T
1

progress = ProgressBar(maxval=my.objects.count() or None).start() Fixed for me

Reference: https://github.com/niltonvolpato/python-progressbar/issues/36

Topper answered 8/3, 2019 at 18:42 Comment(0)
C
0

Try:

pbar = ProgressBar(widgets=[Bar('=', '[', ']'), ' ',
                                        Percentage(), ' ',
                                        ETA()])
for row in pbar(cursor.fetchall()):
    ''' do some work here ''' 

In this case You don't need to update pbar and don't need to start and finish pbar.

Chufa answered 20/11, 2015 at 20:6 Comment(2)
i tried your approach, but now i get a different error. please see the editBoabdil
I like universal solutions. Cursor.fetchall() returns list.Chufa
C
0

set max_error = False

ProgressBar(max_error = False)

https://progressbar-2.readthedocs.io/en/latest/_modules/progressbar/bar.html#ProgressBar

Chiccory answered 13/11, 2023 at 2:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.