Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Comment and clean up progress.py a bit.

  • Loading branch information...
commit 3fe624831936e5d353a25e3fd316113ccf5f775f 1 parent 2d6f5fb
@mythmon authored
Showing with 55 additions and 17 deletions.
  1. +55 −17 progress.py
View
72 progress.py
@@ -1,6 +1,12 @@
#!/usr/bin/env python
+# progress.py
-import os, time, sys, math
+"""A class for drawing a progress bar to the console in various styles."""
+
+import os
+import time
+import sys
+import math
class ProgressBar:
@@ -8,21 +14,29 @@ def __init__(self, maxValue, style="percent"):
self.style = style
self.current = 0
self.maxValue = maxValue
+ # Get the width of the current console
_,self.width = os.popen('stty size', 'r').read().split()
+ # It is returned as a string. Fix that.
self.width = int(self.width)
self.firstDraw = True
if style=="percent":
- # leave space for the sides, the brackets, and the percent
- self.width -= 9
+ # [****----] 50%
+ # Leave space for the sides (2), the brackets(2), the percent (3)
+ # and the space between the brackets and percent (1).
+ self.width -= 8
if style=="plain":
- # leave space for the sides and the brackets
- self.width -= 5
+ # [******------]
+ # Leave space for the sides (2) and the brackets (2).
+ self.width -= 4
if style=="numbers":
- # Leave space for the sides, the brackets, and the biggest number
- # The biggest number is xxx/xxx, so numOfDigits*2+1 for the space
- numDigits = math.log10(maxValue)
- self.width -= int(math.ceil(numDigits * 2 + 7))
+ # [***---] 25/50
+ # The biggest number is xxx/xxx, so numOfDigits*2+1 for the numbers
+ # then 2 more for spaces on either side of the bar, 2 for the
+ # brackets.
+ numDigits = math.ceil(math.log10(maxValue))
+ self.width -= int(numDigits * 2 + 5)
+ """Update the value of the progress bar, and by default redraw it."""
def update(self,value,redraw=True):
self.current = value
if redraw:
@@ -30,27 +44,51 @@ def update(self,value,redraw=True):
templates = {
'percent': ' [{0}{1}]{2:>3}% ',
- 'numbers': ' [{0}{1}] {3}/{4} ',
- 'plain': ' [{0}{1}] '
- }
+ 'numbers': ' [{0}{1}]{3}/{4} ',
+ 'plain' : ' [{0}{1}] '
+ }
+ """Draw the progress bar to the screen in the specifed style."""
def draw(self):
percent = self.current / (self.maxValue * 1.0)
if percent > 1:
percent = 1
stars = int(self.width * percent)
blanks = self.width - stars
- a = ProgressBar.templates[self.style].format('*'*stars,' '*blanks, int(percent*100), self.current, self.maxValue)
+
+ bar = ProgressBar.templates[self.style].format('*'*stars, ' '*blanks,
+ int(percent*100), self.current, self.maxValue)
+
+ # Reset the cursor to the beginning of the line
sys.stdout.write("\r")
- sys.stdout.write(a)
+ # Write over the old progress bar
+ sys.stdout.write(bar)
+ # Refresh the console
sys.stdout.flush()
+"""Some testing/demo code."""
if __name__ == "__main__":
- bar = ProgressBar(5,style="numbers")
+ bar = ProgressBar(50,style="plain")
+ n = 0
+ while n <= 50:
+ bar.update(n)
+ bar.draw()
+ n += 1
+ time.sleep(0.05)
+ print
+ bar = ProgressBar(50,style="percent")
+ n = 0
+ while n <= 50:
+ bar.update(n)
+ bar.draw()
+ n += 1
+ time.sleep(0.05)
+ print
+ bar = ProgressBar(50,style="numbers")
n = 0
- while n <= 5:
+ while n <= 50:
bar.update(n)
bar.draw()
n += 1
- time.sleep(1)
+ time.sleep(0.05)
print
Please sign in to comment.
Something went wrong with that request. Please try again.