From b832bea9ed06063131d21887a6d9aaa481ea4f1a Mon Sep 17 00:00:00 2001 From: Ofir Farchy Date: Wed, 13 Apr 2016 18:34:18 +0300 Subject: [PATCH 1/5] Added a Random class (for, you know, random progress) Fixed tests to reflect --- progress/bar.py | 36 +++++++++++++++++++++++++++++++++++- test_progress.py | 9 ++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/progress/bar.py b/progress/bar.py index 26008e9..09317f9 100644 --- a/progress/bar.py +++ b/progress/bar.py @@ -15,8 +15,9 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. from __future__ import unicode_literals -from . import Progress +from . import Progress, Infinite from .helpers import WritelnMixin +import random class Bar(WritelnMixin, Progress): @@ -82,3 +83,36 @@ def update(self): class ShadyBar(IncrementalBar): phases = (' ', '░', '▒', '▓', '█') + + +class Random(WritelnMixin, Infinite): + width = 32 + message = '' + suffix = '%(index)d' + bar_prefix = ' |' + bar_suffix = '| ' + empty_fill = ' ' + fill = '█' + hide_cursor = True + + def update(self): + self.index = random.randint(0, 100) + filled_length = int(self.width * self.index / 100) + empty_length = self.width - filled_length + + message = self.message % self + bar = self.fill * filled_length + empty = self.empty_fill * empty_length + suffix = self.suffix % self + line = ''.join([message, self.bar_prefix, bar, empty, self.bar_suffix, + suffix]) + self.writeln(line) + + def finish(self): + self.index = 100 + message = self.message % self + suffix = self.suffix % self + bar = self.fill * self.width + line = ''.join([message, self.bar_prefix, bar, self.bar_suffix, + suffix]) + self.writeln(line) diff --git a/test_progress.py b/test_progress.py index ae7bb19..b82ec73 100755 --- a/test_progress.py +++ b/test_progress.py @@ -6,7 +6,7 @@ import time from progress.bar import (Bar, ChargingBar, FillingSquaresBar, - FillingCirclesBar, IncrementalBar, ShadyBar) + FillingCirclesBar, IncrementalBar, ShadyBar, Random) from progress.spinner import Spinner, PieSpinner, MoonSpinner, LineSpinner from progress.counter import Counter, Countdown, Stack, Pie @@ -39,8 +39,7 @@ def sleep(): sleep() print() -bar = IncrementalBar('Random', suffix='%(index)d') -for i in range(100): - bar.goto(random.randint(0, 100)) +bar = Random(Random.__name__) +for i in bar.iter(range(100)): sleep() -bar.finish() +print() From 4da19e2d954ab841851de67c112648169e1439cd Mon Sep 17 00:00:00 2001 From: Ofir Farchy Date: Wed, 13 Apr 2016 18:36:56 +0300 Subject: [PATCH 2/5] Updated README.rst with the addition of Random --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index cce3023..0b9cada 100644 --- a/README.rst +++ b/README.rst @@ -12,7 +12,7 @@ Easy progress reporting for Python Bars ---- -There are 6 progress bars to choose from: +There are 7 progress bars to choose from: - ``Bar`` - ``ChargingBar`` @@ -20,6 +20,7 @@ There are 6 progress bars to choose from: - ``FillingCirclesBar`` - ``IncrementalBar`` - ``ShadyBar`` +- ``Random`` To use them, just call ``next`` to advance and ``finish`` to finish: From 6f931a445bc74d25afb1f33301a6b698c20f92c3 Mon Sep 17 00:00:00 2001 From: Ofir Farchy Date: Tue, 19 Apr 2016 21:09:28 +0300 Subject: [PATCH 3/5] first shot at the adapting bar --- progress/bar.py | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/progress/bar.py b/progress/bar.py index 09317f9..8a3c47c 100644 --- a/progress/bar.py +++ b/progress/bar.py @@ -85,15 +85,39 @@ class ShadyBar(IncrementalBar): phases = (' ', '░', '▒', '▓', '█') -class Random(WritelnMixin, Infinite): - width = 32 - message = '' +class AdaptiveBar(Bar): + phases = (' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉', '█') + suffix = '%(percent)d%%' + + def update(self): + nphases = len(self.phases) + filled_len = self.width * self.progress + nfull = int(filled_len) # Number of full chars + phase = int((filled_len - nfull) * nphases) # Phase of last char + nempty = self.width - nfull # Number of empty chars + + message = self.message % self + bar = self.phases[-1] * nfull + current = self.phases[phase] if phase > 0 else '' + empty = self.empty_fill * max(0, nempty - len(current)) + suffix = self.suffix % self + line = ''.join([message, self.bar_prefix, bar, current, empty, + self.bar_suffix, suffix]) + self.writeln(line) + + def finish(self): + self.index = 100 + message = self.message % self + # suffix = self.suffix % self + suffix = "Done." + bar = bar = self.phases[-1] * self.width + line = ''.join([message, self.bar_prefix, bar, self.bar_suffix, suffix]) + self.writeln(line) + + +class Random(AdaptiveBar): suffix = '%(index)d' - bar_prefix = ' |' - bar_suffix = '| ' - empty_fill = ' ' fill = '█' - hide_cursor = True def update(self): self.index = random.randint(0, 100) @@ -107,12 +131,3 @@ def update(self): line = ''.join([message, self.bar_prefix, bar, empty, self.bar_suffix, suffix]) self.writeln(line) - - def finish(self): - self.index = 100 - message = self.message % self - suffix = self.suffix % self - bar = self.fill * self.width - line = ''.join([message, self.bar_prefix, bar, self.bar_suffix, - suffix]) - self.writeln(line) From 84cd2e673f6349c98fabc6175834130920384899 Mon Sep 17 00:00:00 2001 From: Ofir Farchy Date: Tue, 19 Apr 2016 21:30:25 +0300 Subject: [PATCH 4/5] reverted changes --- README.rst | 3 +-- progress/bar.py | 36 +----------------------------------- test_progress.py | 9 +++++---- 3 files changed, 7 insertions(+), 41 deletions(-) diff --git a/README.rst b/README.rst index 0b9cada..cce3023 100644 --- a/README.rst +++ b/README.rst @@ -12,7 +12,7 @@ Easy progress reporting for Python Bars ---- -There are 7 progress bars to choose from: +There are 6 progress bars to choose from: - ``Bar`` - ``ChargingBar`` @@ -20,7 +20,6 @@ There are 7 progress bars to choose from: - ``FillingCirclesBar`` - ``IncrementalBar`` - ``ShadyBar`` -- ``Random`` To use them, just call ``next`` to advance and ``finish`` to finish: diff --git a/progress/bar.py b/progress/bar.py index 09317f9..26008e9 100644 --- a/progress/bar.py +++ b/progress/bar.py @@ -15,9 +15,8 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. from __future__ import unicode_literals -from . import Progress, Infinite +from . import Progress from .helpers import WritelnMixin -import random class Bar(WritelnMixin, Progress): @@ -83,36 +82,3 @@ def update(self): class ShadyBar(IncrementalBar): phases = (' ', '░', '▒', '▓', '█') - - -class Random(WritelnMixin, Infinite): - width = 32 - message = '' - suffix = '%(index)d' - bar_prefix = ' |' - bar_suffix = '| ' - empty_fill = ' ' - fill = '█' - hide_cursor = True - - def update(self): - self.index = random.randint(0, 100) - filled_length = int(self.width * self.index / 100) - empty_length = self.width - filled_length - - message = self.message % self - bar = self.fill * filled_length - empty = self.empty_fill * empty_length - suffix = self.suffix % self - line = ''.join([message, self.bar_prefix, bar, empty, self.bar_suffix, - suffix]) - self.writeln(line) - - def finish(self): - self.index = 100 - message = self.message % self - suffix = self.suffix % self - bar = self.fill * self.width - line = ''.join([message, self.bar_prefix, bar, self.bar_suffix, - suffix]) - self.writeln(line) diff --git a/test_progress.py b/test_progress.py index b82ec73..ae7bb19 100755 --- a/test_progress.py +++ b/test_progress.py @@ -6,7 +6,7 @@ import time from progress.bar import (Bar, ChargingBar, FillingSquaresBar, - FillingCirclesBar, IncrementalBar, ShadyBar, Random) + FillingCirclesBar, IncrementalBar, ShadyBar) from progress.spinner import Spinner, PieSpinner, MoonSpinner, LineSpinner from progress.counter import Counter, Countdown, Stack, Pie @@ -39,7 +39,8 @@ def sleep(): sleep() print() -bar = Random(Random.__name__) -for i in bar.iter(range(100)): +bar = IncrementalBar('Random', suffix='%(index)d') +for i in range(100): + bar.goto(random.randint(0, 100)) sleep() -print() +bar.finish() From ffc01fdffbacbc92a1cc9787b9afb1a07dd2d90e Mon Sep 17 00:00:00 2001 From: Ofir Farchy Date: Wed, 20 Apr 2016 19:16:57 +0300 Subject: [PATCH 5/5] Finished with adaptive progress bar, Added tests for the AdaptiveBar Fixed README.rst --- README.rst | 3 ++- progress/bar.py | 21 ++++++++++++++------- test_progress.py | 10 ++++++++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/README.rst b/README.rst index cce3023..402579a 100644 --- a/README.rst +++ b/README.rst @@ -12,7 +12,7 @@ Easy progress reporting for Python Bars ---- -There are 6 progress bars to choose from: +There are 7 progress bars to choose from: - ``Bar`` - ``ChargingBar`` @@ -20,6 +20,7 @@ There are 6 progress bars to choose from: - ``FillingCirclesBar`` - ``IncrementalBar`` - ``ShadyBar`` +- ``AdaptiveBar`` To use them, just call ``next`` to advance and ``finish`` to finish: diff --git a/progress/bar.py b/progress/bar.py index be72acb..12d60bb 100644 --- a/progress/bar.py +++ b/progress/bar.py @@ -17,7 +17,6 @@ from __future__ import unicode_literals from . import Progress, Infinite from .helpers import WritelnMixin -import random class Bar(WritelnMixin, Progress): @@ -85,13 +84,22 @@ class ShadyBar(IncrementalBar): phases = (' ', '░', '▒', '▓', '█') -class AdaptiveBar(Bar): +class AdaptiveBar(WritelnMixin, Infinite): phases = (' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉', '█') - suffix = '%(percent)d%%' + suffix = '%(index)d%%' + _count = 0 + width = 50 + bar_prefix = ' |' + bar_suffix = '| ' + empty_fill = ' ' def update(self): + import math + self.percent = float(math.pow(self._count, 2)/(math.pow(self._count, 2)+1000)) + self.index = int(self.percent * 100) + self._count += 1 nphases = len(self.phases) - filled_len = self.width * self.progress + filled_len = self.width * self.percent nfull = int(filled_len) # Number of full chars phase = int((filled_len - nfull) * nphases) # Phase of last char nempty = self.width - nfull # Number of empty chars @@ -108,8 +116,7 @@ def update(self): def finish(self): self.index = 100 message = self.message % self - # suffix = self.suffix % self - suffix = "Done." + suffix = self.suffix % self bar = bar = self.phases[-1] * self.width line = ''.join([message, self.bar_prefix, bar, self.bar_suffix, suffix]) - self.writeln(line) \ No newline at end of file + self.writeln(line) diff --git a/test_progress.py b/test_progress.py index ae7bb19..e46e76e 100755 --- a/test_progress.py +++ b/test_progress.py @@ -5,7 +5,7 @@ import random import time -from progress.bar import (Bar, ChargingBar, FillingSquaresBar, +from progress.bar import (Bar, ChargingBar, FillingSquaresBar, AdaptiveBar, FillingCirclesBar, IncrementalBar, ShadyBar) from progress.spinner import Spinner, PieSpinner, MoonSpinner, LineSpinner from progress.counter import Counter, Countdown, Stack, Pie @@ -16,7 +16,6 @@ def sleep(): t += t * random.uniform(-0.1, 0.1) # Add some variance time.sleep(t) - for bar_cls in (Bar, ChargingBar, FillingSquaresBar, FillingCirclesBar): suffix = '%(index)d/%(max)d [%(elapsed)d / %(eta)d]' bar = bar_cls(bar_cls.__name__, suffix=suffix) @@ -29,6 +28,13 @@ def sleep(): for i in bar.iter(range(200)): sleep() +for bar_cls in [AdaptiveBar]: + suffix = '%(index)d%% [%(elapsed_td)s]' + bar = bar_cls(bar_cls.__name__, suffix=suffix) + for i in bar.iter(range(200)): + sleep() + print() + for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner): for i in spin(spin.__name__ + ' ').iter(range(100)): sleep()