Skip to content

Commit f1a3493

Browse files
author
Olivier Brunel
committed
Add README content and fix typos
1 parent 396699e commit f1a3493

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

ProgressBar.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class ProgressBar:
77
def __init__(self,
8-
pretext="", # Test to print before the bar
8+
pretext="", # Text to print before the bar
99
progresschar="-", # Character to show progress
1010
loadingchars=r"\|/", # Last character of bar moving as bar loads (moves even if no progress)
1111
startendchar="[]", # Characters going around the bar
@@ -32,33 +32,35 @@ def __init__(self,
3232

3333
# Role : print the progress bar as an independent thread
3434
# Arguments:
35-
# number: multiprocessing.Value
36-
# total: value to reach
35+
# number: progress value (type multiprocessing.Value of int)
36+
# max: value to reach (int)
3737
# updateperiod: refresh period of progress bar in seconds
3838
# Example:
3939
# number = multiprocessing.Value("i", 0)
40-
# total = 30
40+
# max = 30
4141
# progressbar = ProgressBar()
42-
#-> multiprocessing.Process(target=progressbar.inThread, args=(number,total,0.1))
43-
# for i in range(0,total)
42+
#-> multiprocessing.Process(target=progressbar.inThread, args=(number,max,0.1))
43+
# for i in range(0,max)
4444
# number.value = i
45-
def inThread(self, number, total, updateperiod=0.1):
46-
while(number.value != total):
47-
self.print(number.value,total)
45+
# time.sleep(1)
46+
def inThread(self, number, max, updateperiod=0.1):
47+
while(number.value != max):
48+
self.print(number.value,max)
4849
time.sleep(float(updateperiod))
49-
self.print(total,total)
50+
self.print(max,max)
5051

5152

5253
# Role : print the progress bar
5354
# Arguments:
54-
# number: progress value
55-
# total: maximum value
55+
# number: progress value (int)
56+
# max: maximum value (int)
5657
# Example:
57-
# total = 30
58+
# max = 30
5859
# progressbar = ProgressBar()
59-
# for i in range(0,total):
60-
#-> progressbar.print(i, total)
61-
def print(self,number,total):
60+
# for i in range(0,max):
61+
#-> progressbar.print(i, max)
62+
# time.sleep(1)
63+
def print(self,number,max):
6264
barstring = ""
6365

6466
# No carriage return on first print
@@ -75,12 +77,12 @@ def print(self,number,total):
7577
if self.startendchar:
7678
barstring += self.startendchar[0]
7779
#Current state of affairs
78-
sofarbar = int( (number/total)*self.barwidth )
80+
sofarbar = int( (number/max)*self.barwidth )
7981
remainingbar = self.barwidth - sofarbar
8082
#Add progress chars
8183
barstring += sofarbar*self.progresschar
8284
#If loading chars, print loading chars and go to next one (unless 100%)
83-
if self.loadingchars != "" and number != total:
85+
if self.loadingchars != "" and number != max:
8486
barstring += self.loadingchars[self.loadingcharsindex]
8587
self.loadingcharsindex = (self.loadingcharsindex+1) % len(self.loadingchars)
8688
remainingbar -= 1
@@ -95,9 +97,9 @@ def print(self,number,total):
9597

9698
# Post progress bar
9799
if self.displaypercentage:
98-
barstring += " %d%%" % int(number*100/total)
100+
barstring += " %d%%" % int(number*100/max)
99101
if self.displaycount:
100-
barstring += " (%d/%d)" % (number,total)
102+
barstring += " (%d/%d)" % (number,max)
101103

102104
# Print the bar out
103105
sys.stdout.write(barstring)

README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
1-
# python-progress-bar
2-
Prints a progress bar. It can be the thread function or a normal function call.
1+
# Python Progress Bar
2+
Prints an all customisable **easy-to-use** progress bar. It can be the thread target or a normal function call.
3+
4+
5+
## Import in your code
6+
```python
7+
from *folderclone*.ProgressBar import ProgressBar
8+
```
9+
Now use as class ProgressBar.
10+
/!\ folderclone should not contain character "-". Make sure not to clone in default git clone dir for this repo.
11+
12+
13+
## Use
14+
### As a simple function call
15+
```python
16+
max = 30
17+
progressbar = ProgressBar()
18+
for i in range(0,max):
19+
progressbar.print(i,max)
20+
time.sleep(1) # To see progress
21+
```
22+
23+
### As a separate thread target
24+
```python
25+
max = 30
26+
progressbar = ProgressBar()
27+
number = multiprocessing.Value("i", 0)
28+
multiprocessing.Process(target=progressbar.inThread, args=(number,max))
29+
for i in range(0,max)
30+
number.value = i
31+
time.sleep(1) # To see progress
32+
```
33+
34+
35+
## Go further: customise your bar
36+
### Style parameters
37+
You can give the following optional arguments to the ProgressBar constructor:
38+
- pretext: Text to print before the bar (default "")
39+
- progresschar: Character to show progress (default '-')
40+
- loadingchars: Last character of bar moving as bar loads (moves even if no progress) (default "\|/")
41+
- startendchar: Characters going around the bar (default "[]")
42+
- barwidth: Length of the bar in characters (does not include optionally printed pretext, progresschar, percentage and count) (default terminal width/2)
43+
- displaypercentage: Show percentage as well or not (default False)
44+
- displaycount: Show count as well or not (default False)
45+
46+
### Thread parameters
47+
If you use the thread version, you can customise the refresh time of your progress bar.
48+
To do this, give an extra argument (type float) representing the refreshing period of the bar.
49+
```python
50+
multiprocessing.Process(target=progressbar.inThread, args=(number,max,0.2))
51+
```

0 commit comments

Comments
 (0)