Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Comment and clean up progress.py a bit.

  • Loading branch information...
commit 3fe624831936e5d353a25e3fd316113ccf5f775f 1 parent 2d6f5fb
Mike Cooper authored

Showing 1 changed file with 55 additions and 17 deletions. Show diff stats Hide diff stats

  1. +55 17 progress.py
72 progress.py
... ... @@ -1,6 +1,12 @@
1 1 #!/usr/bin/env python
  2 +# progress.py
2 3
3   -import os, time, sys, math
  4 +"""A class for drawing a progress bar to the console in various styles."""
  5 +
  6 +import os
  7 +import time
  8 +import sys
  9 +import math
4 10
5 11 class ProgressBar:
6 12
@@ -8,21 +14,29 @@ def __init__(self, maxValue, style="percent"):
8 14 self.style = style
9 15 self.current = 0
10 16 self.maxValue = maxValue
  17 + # Get the width of the current console
11 18 _,self.width = os.popen('stty size', 'r').read().split()
  19 + # It is returned as a string. Fix that.
12 20 self.width = int(self.width)
13 21 self.firstDraw = True
14 22 if style=="percent":
15   - # leave space for the sides, the brackets, and the percent
16   - self.width -= 9
  23 + # [****----] 50%
  24 + # Leave space for the sides (2), the brackets(2), the percent (3)
  25 + # and the space between the brackets and percent (1).
  26 + self.width -= 8
17 27 if style=="plain":
18   - # leave space for the sides and the brackets
19   - self.width -= 5
  28 + # [******------]
  29 + # Leave space for the sides (2) and the brackets (2).
  30 + self.width -= 4
20 31 if style=="numbers":
21   - # Leave space for the sides, the brackets, and the biggest number
22   - # The biggest number is xxx/xxx, so numOfDigits*2+1 for the space
23   - numDigits = math.log10(maxValue)
24   - self.width -= int(math.ceil(numDigits * 2 + 7))
  32 + # [***---] 25/50
  33 + # The biggest number is xxx/xxx, so numOfDigits*2+1 for the numbers
  34 + # then 2 more for spaces on either side of the bar, 2 for the
  35 + # brackets.
  36 + numDigits = math.ceil(math.log10(maxValue))
  37 + self.width -= int(numDigits * 2 + 5)
25 38
  39 + """Update the value of the progress bar, and by default redraw it."""
26 40 def update(self,value,redraw=True):
27 41 self.current = value
28 42 if redraw:
@@ -30,27 +44,51 @@ def update(self,value,redraw=True):
30 44
31 45 templates = {
32 46 'percent': ' [{0}{1}]{2:>3}% ',
33   - 'numbers': ' [{0}{1}] {3}/{4} ',
34   - 'plain': ' [{0}{1}] '
35   - }
  47 + 'numbers': ' [{0}{1}]{3}/{4} ',
  48 + 'plain' : ' [{0}{1}] '
  49 + }
36 50
  51 + """Draw the progress bar to the screen in the specifed style."""
37 52 def draw(self):
38 53 percent = self.current / (self.maxValue * 1.0)
39 54 if percent > 1:
40 55 percent = 1
41 56 stars = int(self.width * percent)
42 57 blanks = self.width - stars
43   - a = ProgressBar.templates[self.style].format('*'*stars,' '*blanks, int(percent*100), self.current, self.maxValue)
  58 +
  59 + bar = ProgressBar.templates[self.style].format('*'*stars, ' '*blanks,
  60 + int(percent*100), self.current, self.maxValue)
  61 +
  62 + # Reset the cursor to the beginning of the line
44 63 sys.stdout.write("\r")
45   - sys.stdout.write(a)
  64 + # Write over the old progress bar
  65 + sys.stdout.write(bar)
  66 + # Refresh the console
46 67 sys.stdout.flush()
47 68
  69 +"""Some testing/demo code."""
48 70 if __name__ == "__main__":
49   - bar = ProgressBar(5,style="numbers")
  71 + bar = ProgressBar(50,style="plain")
  72 + n = 0
  73 + while n <= 50:
  74 + bar.update(n)
  75 + bar.draw()
  76 + n += 1
  77 + time.sleep(0.05)
  78 + print
  79 + bar = ProgressBar(50,style="percent")
  80 + n = 0
  81 + while n <= 50:
  82 + bar.update(n)
  83 + bar.draw()
  84 + n += 1
  85 + time.sleep(0.05)
  86 + print
  87 + bar = ProgressBar(50,style="numbers")
50 88 n = 0
51   - while n <= 5:
  89 + while n <= 50:
52 90 bar.update(n)
53 91 bar.draw()
54 92 n += 1
55   - time.sleep(1)
  93 + time.sleep(0.05)
56 94 print

0 comments on commit 3fe6248

Please sign in to comment.
Something went wrong with that request. Please try again.