From 7d4bb714352ad88e267064ce9f52f19543172825 Mon Sep 17 00:00:00 2001 From: rasbt Date: Mon, 17 Feb 2014 11:53:50 -0500 Subject: [PATCH] title print support, refactoring --- CHANGELOG.txt | 3 +++ README.md | 6 +++++- examples/ex2_percent_indicator_allargs.py | 4 ++-- examples/ex2_progressbar_allargs.py | 4 ++-- pyprind/prog_class.py | 12 ++++++++++-- pyprind/progbar.py | 6 ++++-- pyprind/progpercent.py | 6 ++++-- 7 files changed, 30 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index aaa5b8b..2cfca0f 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,7 +1,10 @@ VERSION 2.3.0 ================ +- added native print() support + prints title and elapsed time of an tracked object after loop completed - data member self.end stores elapsed time when loop completed +- data member self.title saves title of the tracking objects VERSION 2.2.0 diff --git a/README.md b/README.md index 52127a0..9e35148 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,11 @@ Changelog **VERSION 2.3.0** -- +- added native print() support + prints title and elapsed time of an tracked object after loop completed +- data member self.end stores elapsed time when loop completed +- data member self.title saves title of the tracking objects + **VERSION 2.2.0** diff --git a/examples/ex2_percent_indicator_allargs.py b/examples/ex2_percent_indicator_allargs.py index 2567c04..105bf0f 100644 --- a/examples/ex2_percent_indicator_allargs.py +++ b/examples/ex2_percent_indicator_allargs.py @@ -6,11 +6,11 @@ def example_2(): n = 1000000 - my_per = pyprind.ProgPercent(n, stream=1, track_time=True) + my_per = pyprind.ProgPercent(n, stream=1, track_time=True, title='My Percent Indicator') for i in range(n): # do some computation my_per.update() - print('Print elapsed time again ...') + print('\n\nPrint tracking object ...\n') print(my_per) if __name__ == '__main__': diff --git a/examples/ex2_progressbar_allargs.py b/examples/ex2_progressbar_allargs.py index 0d02f09..51d2ff0 100644 --- a/examples/ex2_progressbar_allargs.py +++ b/examples/ex2_progressbar_allargs.py @@ -6,11 +6,11 @@ def example_2(): n = 1000000 - my_bar = pyprind.ProgBar(n, stream=1, track_time=True) + my_bar = pyprind.ProgBar(n, stream=1, track_time=True, title='My Progress Bar') for i in range(n): # do some computation my_bar.update() - print('Print elapsed time again ...') + print('\n\nPrint tracking object ...\n') print(my_bar) if __name__ == '__main__': diff --git a/pyprind/prog_class.py b/pyprind/prog_class.py index 4f99372..b29824a 100644 --- a/pyprind/prog_class.py +++ b/pyprind/prog_class.py @@ -3,8 +3,9 @@ import os class Prog(): - def __init__(self, iterations, track_time, stream): + def __init__(self, iterations, track_time, stream, title): self.cnt = 0 + self.title = title self.max_iter = float(iterations) # to support Python 2.x self.track = track_time self.start = time.time() @@ -13,6 +14,8 @@ def __init__(self, iterations, track_time, stream): self._stream_out = self._no_stream self._stream_flush = self._no_stream self._check_stream() + self._print_title() + def _check_stream(self): if self.stream == 1 and os.isatty(sys.stdout.fileno()): @@ -51,8 +54,13 @@ def _finish(self): self._stream_out('\n{}'.format(self.__repr__())) self._stream_out('\n') + def _print_title(self): + if self.title: + self._stream_out('{}\n'.format(self.title)) + self._stream_flush() + def __repr__(self): - return 'Total time elapsed: {:.3f} sec'.format(self.end) + return 'Title: {}\nTotal time elapsed: {:.3f} sec'.format(self.title, self.end) def __str__(self): return self.__repr__() diff --git a/pyprind/progbar.py b/pyprind/progbar.py index 16f05a0..bb71cb5 100644 --- a/pyprind/progbar.py +++ b/pyprind/progbar.py @@ -16,9 +16,11 @@ class ProgBar(Prog): iterations (int): number of iterations of the computation track_time (bool): prints elapsed time when loop has finished stream: takes 1 for stdout, 2 for stderr, or given stream object + title (str): A title for the progress bar + """ - def __init__(self, iterations, track_time=True, width=30, stream=2): - Prog.__init__(self, iterations, track_time, stream) + def __init__(self, iterations, track_time=True, width=30, stream=2, title=''): + Prog.__init__(self, iterations, track_time, stream, title) self.bar_width = width self._adjust_width() self.last_progress = 0 diff --git a/pyprind/progpercent.py b/pyprind/progpercent.py index 0c0d075..ee8f5c7 100644 --- a/pyprind/progpercent.py +++ b/pyprind/progpercent.py @@ -16,9 +16,11 @@ class ProgPercent(Prog): width (int): width of the progress bar in characters track_time (bool): prints elapsed time and estimated time left stream: takes 1 for stdout, 2 for stderr, or given stream object + title (str): A title for the percent indicator + """ - def __init__(self, iterations, track_time=True, stream=2): - Prog.__init__(self, iterations, track_time, stream) + def __init__(self, iterations, track_time=True, stream=2, title=''): + Prog.__init__(self, iterations, track_time, stream, title) self.perc = 0 self._print_update()