Skip to content

Commit

Permalink
title print support, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Feb 17, 2014
1 parent 809ce77 commit 7d4bb71
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
4 changes: 2 additions & 2 deletions examples/ex2_percent_indicator_allargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/ex2_progressbar_allargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
12 changes: 10 additions & 2 deletions pyprind/prog_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()):
Expand Down Expand Up @@ -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__()
6 changes: 4 additions & 2 deletions pyprind/progbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pyprind/progpercent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 7d4bb71

Please sign in to comment.