root/galaxy-central/eggs/bx_python-0.5.0_dev_f74aec067563-py2.6-macosx-10.6-universal-ucs2.egg/bx/cookbook/progress_bar.py @ 3

リビジョン 3, 2.3 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1"""
2An ASCII text progress bar. See __main__ for command line use (using \r to
3move the cursor back to the start of the current line is the key, on
4terminals that do not support this functionality the progress bar will
5not work as well).
6
7http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/168639
8"""
9
10import sys
11
12class ProgressBar:
13    def __init__(self, minValue = 0, maxValue = 10, totalWidth=12):
14        self.progBar = "[]"   # This holds the progress bar string
15        self.min = minValue
16        self.max = maxValue
17        self.span = maxValue - minValue
18        self.width = totalWidth
19        self.amount = 0       # When amount == max, we are 100% done
20        self.update(0)  # Build progress bar string
21
22    def update(self, newAmount = 0):
23        if newAmount < self.min: newAmount = self.min
24        if newAmount > self.max: newAmount = self.max
25        self.amount = newAmount
26
27        # Figure out the new percent done, round to an integer
28        diffFromMin = float(self.amount - self.min)
29        percentDone = (diffFromMin / float(self.span)) * 100.0
30        percentDone = round(percentDone)
31        percentDone = int(percentDone)
32
33        # Figure out how many hash bars the percentage should be
34        allFull = self.width - 2
35        numHashes = (percentDone / 100.0) * allFull
36        numHashes = int(round(numHashes))
37
38        # build a progress bar with hashes and spaces
39        if allFull == numHashes:
40            self.progBar = "[" + '='*(numHashes) + "]"
41        else:
42            self.progBar = "[" + '='*(numHashes-1) + '>' + ' '*(allFull-numHashes) + "]"
43
44        # figure out where to put the percentage, roughly centered
45        percentPlace = (len(self.progBar) / 2) - len(str(percentDone))
46        percentString = str(percentDone) + "%"
47
48        # slice the percentage into the bar
49        self.progBar = self.progBar[0:percentPlace] + percentString + self.progBar[percentPlace+len(percentString):]
50
51    def update_and_print( self, newAmount = 0, f = sys.stdout ):
52        self.update( newAmount )
53        print >> f, "\r", self,
54        f.flush()
55
56
57    def __str__(self):
58        return str(self.progBar)
59
60if __name__ == "__main__":
61    import time
62
63    bar = ProgressBar( 0, 1000, 80 )
64
65    for i in range(1000):
66        bar.update( i )
67        print "\r", bar,
68        sys.stdout.flush()
69       
70
71    print
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。