Skip to content

Commit

Permalink
Merge branch 'release/v3.41.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed May 29, 2019
2 parents 4076cb8 + f1a9ff9 commit e913c59
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
3 changes: 2 additions & 1 deletion examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,15 @@ def dynamic_message():
progressbar.Percentage(),
progressbar.Bar(),
progressbar.DynamicMessage('loss'),
progressbar.DynamicMessage('username', width=12, precision=12),,
]
with progressbar.ProgressBar(max_value=100, widgets=widgets) as bar:
min_so_far = 1
for i in range(100):
val = random.random()
if val < min_so_far:
min_so_far = val
bar.update(i, loss=min_so_far)
bar.update(i, loss=min_so_far, username='Some user %02d' % i)


@example
Expand Down
2 changes: 1 addition & 1 deletion progressbar/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
long running operations.
'''.strip().split())
__email__ = '[email protected]'
__version__ = '3.40.0'
__version__ = '3.41.0'
__license__ = 'BSD'
__copyright__ = 'Copyright 2015 Rick van Hattem (Wolph)'
__url__ = 'https://github.com/WoLpH/python-progressbar'
29 changes: 23 additions & 6 deletions progressbar/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,12 @@ def __call__(self, progress, data):
class DynamicMessage(FormatWidgetMixin, WidgetBase):
'''Displays a custom variable.'''

def __init__(self, name):
def __init__(self, name, format='{name}: {formatted_value}',
width=6, precision=3):
'''Creates a DynamicMessage associated with the given name.'''
self.format = format
self.width = width
self.precision = precision
if not isinstance(name, str):
raise TypeError('DynamicMessage(): argument must be a string')
if len(name.split()) > 1:
Expand All @@ -726,11 +730,24 @@ def __init__(self, name):
self.name = name

def __call__(self, progress, data):
val = data['dynamic_messages'][self.name]
if val:
return self.name + ': ' + '{:6.3g}'.format(val)
else:
return self.name + ': ' + 6 * '-'
value = data['dynamic_messages'][self.name]
context = data.copy()
context['value'] = value
context['name'] = self.name
context['width'] = self.width
context['precision'] = self.precision

try:
context['formatted_value'] = '{value:{width}.{precision}}'.format(
**context)
except (TypeError, ValueError):
if value:
context['formatted_value'] = '{value:{width}}'.format(
**context)
else:
context['formatted_value'] = '-' * self.width

return self.format.format(**context)


class CurrentTime(FormatWidgetMixin, TimeSensitiveWidgetBase):
Expand Down
11 changes: 10 additions & 1 deletion tests/test_custom_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,20 @@ def test_dynamic_message_widget():
progressbar.Bar(),
' (', progressbar.ETA(), ') ',
progressbar.DynamicMessage('loss'),
progressbar.DynamicMessage('text'),
progressbar.DynamicMessage('error', precision=None),
]

p = progressbar.ProgressBar(widgets=widgets, max_value=1000)
p.start()
for i in range(0, 200, 5):
time.sleep(0.1)
p.update(i + 1, loss=.5)
p.update(i + 1, loss=.5, text='spam', error=1)

i += 1
p.update(i, text=None)
i += 1
p.update(i, text=False)
i += 1
p.update(i, text=True, error='a')
p.finish()

0 comments on commit e913c59

Please sign in to comment.