ATLAS Offline Software
Loading...
Searching...
No Matches
ProgressBar.py
Go to the documentation of this file.
1# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2
3import sys
4
6 def __init__(self, minValue = 0, maxValue = 10, totalWidth=12, prefix="", suffix=""):
7 self.progBar = "[]" # This holds the progress bar string
8 self.min = minValue
9 self.max = maxValue
10 self.span = maxValue - minValue
11 self.amount = 0 # When amount == max, we are 100% done
12 self.nextUpdate = minValue+float(self.span/100.0)
13 if len(prefix):
14 self.prefix=prefix+" ["
15 else:
16 self.prefix="["
17 if len(suffix):
18 self.suffix="] " + suffix
19 else:
20 self.suffix="]"
21 self.width = totalWidth - len(self.prefix) - len(self.suffix)
22 self.update(0) # Build progress bar string
23
24 def update(self, newAmount = 0):
25 if newAmount < self.min:
26 newAmount = self.min
27 if newAmount > self.max:
28 newAmount = self.max
29
30 #=== only continue if something new to draw
31 if newAmount < self.nextUpdate and newAmount<self.max :
32 return
33 self.nextUpdate = self.nextUpdate+float(self.span/100.0)
34 self.amount = newAmount
35
36 # Figure out the new percent done, round to an integer
37 diffFromMin = float(self.amount - self.min)
38 percentDone = (diffFromMin / float(self.span)) * 100.0 if self.span>0 else 100.
39 percentDone = round(percentDone)
40 percentDone = int(percentDone)
41
42 # Figure out how many hash bars the percentage should be
43 numHashes = (percentDone / 100.0) * self.width
44 numHashes = int(round(numHashes))
45
46 # build a progress bar with hashes and spaces
47 self.progBar = self.prefix + '#'*numHashes + ' '*(self.width-numHashes) + self.suffix
48
49 # figure out where to put the percentage, roughly centered
50 percentPlace = (len(self.progBar) // 2) - len(str(percentDone))
51 percentString = str(percentDone) + "%"
52
53 # slice the percentage into the bar
54 self.progBar = self.progBar[0:percentPlace] + percentString + self.progBar[percentPlace+len(percentString):]
55
56 sys.stdout.write('\r' + self.progBar)
57 sys.stdout.flush()# force updating of screen
58
59 def done(self):
60 self.update(self.max)
61 sys.stdout.write('\r' + self.progBar + '\n')
62 sys.stdout.flush()# force updating of screen
63
64 def __str__(self):
65 return str(self.progBar)
__init__(self, minValue=0, maxValue=10, totalWidth=12, prefix="", suffix="")
Definition ProgressBar.py:6
update(self, newAmount=0)