Skip to content

Commit 28fea74

Browse files
committed
PyLoadBar v0.1.0 Update
- Overhauled `PyLoadBar` class. - Replaced "load" method with "start" method. - Sequence type is now set upon initialization of `PyLoadBar` object. - Implemented customizeable iteration length of text-sequence. - Total iterations can now be set. - Iterations now take random amount of time to complete to add realism. - Min/Max time to complete iteration is set upon calling "start()". - Re-wrote tests. - Updated requirements.txt to be less strict. - Updated "setup.py". - Expanded upon source code docstrings. - Updated README.md sections and added more content. - Appropriately renamed several parameters. Signed-off-by: schlopp96 <71921821+schlopp96@users.noreply.github.com>
1 parent ae38aae commit 28fea74

4 files changed

Lines changed: 107 additions & 79 deletions

File tree

PyLoadBar/main.py

Lines changed: 81 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
chdir(dirname(__file__)) # Change working directory to main module.
1515

16-
__version__ = '0.0.9.2'
16+
__version__ = '0.1.0'
1717

1818

1919
class PyLoadBar:
@@ -23,50 +23,41 @@ class PyLoadBar:
2323
2424
Settings:
2525
26-
- When instantiating a new :class:`PyLoadBar` object, you can set the following parameters:
27-
- Set custom start/completion messages by passing string to :param:`msg_loading` and :param:`msg_complete` respectively.
28-
- Toggle visual progress meter using :param:`enable_bar`.
29-
- Apply label to progress meter by passing string to :param:`label` (set to `None` by default)
30-
- Note that :param:`enable_bar` must be set to `True` for this to take effect.
26+
- When instantiating a new :class:`PyLoadBar` object, you can set the sequence type using :param:`bar_sequence`.
3127
32-
- When calling :func:`start(self, iter_total, min_iter, max_iter, txt_seq_speed)`:
28+
- When calling :func:`start(self, msg_loading, msg_complete, label, iter_total, min_iter, max_iter, txt_seq_speed)`:
29+
- Set custom start/completion messages by passing string to :param:`msg_loading` and :param:`msg_complete` respectively.
3330
- Optionally set the total number of iterations to run using :param:`iter_total`, defaults to 5.
34-
- Optionally set the minimum/maximum iteration length in seconds using the :param:`min_iter` and :param:`max_iter` parameters respectively
35-
- Default values are `min_iter: 0.1` seconds and `max_iter: 1.0` seconds
31+
32+
- If using bar-sequence:
33+
- Set the minimum/maximum iteration length in seconds using the :param:`min_iter` and :param:`max_iter` parameters respectively.
34+
- Time taken by each individual iteration is randomized within range of :param:`min_iter` and :param:`max_iter`.
35+
- Apply label to progress meter by passing string to :param:`label` (set to `None` by default).
36+
37+
- If using text-sequence:
38+
- Set number of seconds to complete a single text-sequence iteration using :param:`txt_seq_speed`.
39+
- Defaults to `0.5` seconds per iteration/animation cycle.
3640
"""
3741

38-
def __init__(self,
39-
msg_loading: str | None = 'Loading',
40-
msg_complete: str | None = 'Done!',
41-
label: str | None = None,
42-
enable_bar: bool = True) -> None:
42+
def __init__(self, bar_sequence: bool = True) -> None:
4343
"""Initialize loading sequence with set configuration.
4444
4545
---
4646
47-
:param msg_loading: initial loading message, defaults to 'Loading'
48-
:type msg_loading: :class:`str` | None, optional
49-
:param msg_complete: final message displayed upon completion, defaults to 'Done!'
50-
:type msg_complete: :class:`str` | None, optional
51-
:param label: label displayed alongside progress bar, defaults to None
52-
:type label: :class:`str` | None, optional
53-
:param enable_bar: toggle visible progress bar, defaults to `True`
54-
:type enable_bar: :class:`bool`, optional
47+
:param bar_sequence: toggle progress-bar loading sequence, defaults to `True`
48+
:type bar_sequence: :class:`bool`, optional
5549
:return: :class:`PyLoadBar` object
5650
:rtype: None
5751
"""
5852

59-
self.msg_loading: str | None = msg_loading
60-
self.msg_complete: str | None = msg_complete
61-
self.label: str | None = label
62-
self.enable_bar: bool = enable_bar
63-
64-
if self.enable_bar and self.msg_loading == 'Loading':
65-
self.msg_loading = f'{msg_loading}...' # Add ellipses to default progress-bar starting message.
53+
self.bar_sequence: bool = bar_sequence
6654

6755
def start(
6856
self,
69-
iter_total: int = 10,
57+
msg_loading: str | None = 'Loading',
58+
msg_complete: str | None = 'Done!',
59+
label: str | None = None,
60+
iter_total: int = 5,
7061
min_iter: float = 0.01,
7162
max_iter: float = 0.5,
7263
txt_seq_speed: float = 0.5,
@@ -77,34 +68,53 @@ def start(
7768
7869
Settings:
7970
80-
- Set the total number of iterations to complete using the :param:`iter_total` parameter.
71+
- Set custom start/completion messages by passing string to :param:`msg_loading` and :param:`msg_complete` respectively.
72+
- Note that :param:`bar_sequence` must be set to `True` for this to take effect.
73+
74+
- Set the total number of iterations to complete using the :param:`iter_total`.
75+
- Defaults to 5 iterations.
8176
82-
- Set the minimum/maximum iteration length in seconds using the :param:`min_iter` and :param:`max_iter` parameters respectively.
83-
- Time taken by each individual iteration is randomized within range of :param:`min_iter` and :param:`max_iter`.
77+
- If using bar-sequence:
78+
- Set the minimum/maximum iteration length in seconds using the :param:`min_iter` and :param:`max_iter` parameters respectively.
79+
- Time taken by each individual iteration is randomized within range of :param:`min_iter` and :param:`max_iter`.
80+
- :param:`min_iter` defaults to 0.01 seconds, :param:`max_iter` defaults to 0.5 seconds.
81+
- Apply label to progress meter by passing string to :param:`label` (set to `None` by default).
8482
85-
- Set number of seconds to complete a single text-sequence iteration using :param:`txt_seq_speed`.
86-
- Defaults to `0.5` seconds per iteration/animation cycle.
83+
- If using text-sequence:
84+
- Set number of seconds to complete a single text-sequence iteration using :param:`txt_seq_speed`.
85+
- Defaults to `0.5` seconds per iteration/animation cycle.
8786
8887
---
8988
89+
:param msg_loading: initial loading message, defaults to 'Loading'
90+
:type msg_loading: :class:`str` | None, optional
91+
:param msg_complete: final message displayed upon completion, defaults to 'Done!'
92+
:type msg_complete: :class:`str` | None, optional
93+
:param label: label displayed alongside progress bar, defaults to None
94+
:type label: :class:`str` | None, optional
9095
:param iter_total: total amount of iterations to run, defaults to 5
9196
:type iter_total: :class:`int`, optional
92-
:param min_iter: minimum possible time to complete an iteration, defaults to 0.05 seconds
97+
:param min_iter: minimum possible time to complete an iteration, defaults to 0.01 seconds
9398
:type min_iter: :class:`float`, optional
94-
:param max_iter: maximum possible time to complete an iteration, defaults to 0.25 seconds
99+
:param max_iter: maximum possible time to complete an iteration, defaults to 0.5 seconds
95100
:type max_iter: :class:`float`, optional
96101
:param txt_seq_speed: number of seconds to complete a single text-sequence iteration, defaults to 0.5 seconds
97102
:type txt_seq_speed: :class:`float`, optional
98-
:return: enable loading sequence
103+
:return: loading sequence
99104
:rtype: None
100105
"""
101106

107+
if self.bar_sequence and msg_loading == 'Loading':
108+
msg_loading = f'{msg_loading}...' # Add ellipses to default progress-bar starting message.
109+
102110
try:
103-
if self.enable_bar:
104-
self.__bar_seq(iter_total, min_iter, max_iter)
111+
if self.bar_sequence:
112+
self.__bar_seq(msg_loading, msg_complete, label, iter_total,
113+
min_iter, max_iter)
105114

106115
else:
107-
self.__text_seq(iter_total, txt_seq_speed)
116+
self.__text_seq(msg_loading, msg_complete, iter_total,
117+
txt_seq_speed)
108118

109119
sleep(
110120
0.5
@@ -113,7 +123,9 @@ def start(
113123
except Exception as e:
114124
print(f'Error: {e}')
115125

116-
def __bar_seq(self, iter_total: int, min_iter: float,
126+
@staticmethod
127+
def __bar_seq(msg_loading: str | None, msg_complete: str | None,
128+
label: str | None, iter_total: int, min_iter: float,
117129
max_iter: float) -> None:
118130
"""
119131
Run loading sequence with progress bar.
@@ -123,8 +135,8 @@ def __bar_seq(self, iter_total: int, min_iter: float,
123135
Example:
124136
125137
```python
126-
>>> bar = PyLoadBar(msg_loading='Loading', msg_complete='Done!', label='Progress') # Initialize loading sequence.
127-
>>> bar.start(iter_total=5, min_iter=0.1, max_iter=1.0) # Start loading sequence.
138+
>>> bar = PyLoadBar() # Initialize loading sequence.
139+
>>> bar.start(msg_loading='Loading', msg_complete='Done!', label='Progress', iter_total=5, min_iter=0.1, max_iter=1.0) # Start loading sequence.
128140
129141
Loading...
130142
@@ -136,6 +148,12 @@ def __bar_seq(self, iter_total: int, min_iter: float,
136148
137149
---
138150
151+
:param msg_loading: initial loading message
152+
:type msg_loading: :class:`str` | None
153+
:param msg_complete: final message displayed upon completion
154+
:type msg_complete: :class:`str` | None
155+
:param label: label displayed alongside progress bar
156+
:type label: :class:`str` | None
139157
:param iter_total: total amount of iterations to run
140158
:type iter_total: :class:`int`
141159
:param min_iter: minimum possible time (in seconds) an iteration can take
@@ -146,56 +164,60 @@ def __bar_seq(self, iter_total: int, min_iter: float,
146164
:rtype: None
147165
"""
148166

149-
print(f'{self.msg_loading}\n')
167+
print(f'{msg_loading}\n')
150168

151169
# Start progress-bar iteration.
152170
for iter in tqdm.trange(
153171
iter_total,
154-
desc=self.label,
172+
desc=label,
155173
bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt}]'):
156174
sleep(uniform(min_iter, max_iter))
157175
iter -= 1
158176

159-
print(f'{self.msg_complete}\n') # Print completion message.
177+
print(f'\n{msg_complete}\n') # Print completion message.
160178

161-
def __text_seq(self, iter_total: int, iter_speed: float) -> None:
179+
@staticmethod
180+
def __text_seq(msg_loading: str | None, msg_complete: str | None,
181+
iter_total: int, iter_speed: float) -> None:
162182
"""Run text-based loading sequence.
163183
164184
---
165185
166186
Example:
167187
168188
```python
169-
>>> bar = PyLoadBar(msg_loading='Loading', msg_complete='Done!', enable_bar=False) # Initialize loading sequence.
170-
>>> bar.start(iter_total=1) # Start loading sequence.
189+
>>> bar = PyLoadBar(bar_sequence=False) # Initialize loading sequence.
190+
>>> bar.start(msg_loading='Loading', msg_complete='Done!', iter_total=1, txt_seq_speed=1) # Start loading sequence.
171191
>>> # Note that during actual use case, text is printed to same line followed by an animated '.' character sequence:
172192
173193
# Would look like:
174-
It(1) line 1: \r`bar.msg_loading`
175-
It(2) line 1: \r`bar.msg_loading`.
176-
It(3) line 1: \r`bar.msg_loading`..
177-
It(4) line 1: \r`bar.msg_loading`...
194+
It(1) line 1: \r"Loading"
195+
It(2) line 1: \r"Loading."
196+
It(3) line 1: \r"Loading.."
197+
It(4) line 1: \r"Loading..."
178198
179199
Repeat(...)
180200
181201
182-
Line 3: `bar.msg_complete`
202+
Line 3: "Done!"
183203
184204
```
185205
186206
---
187207
188-
:param iter_total: Amount of iterations to run loading sequence
208+
:param msg_loading: initial loading message
209+
:type msg_loading: :class:`str` | None
210+
:param iter_total: number of iterations to run during loading sequence
189211
:type iter_total: :class:`int`
190-
:param iter_speed: Seconds per text-sequence iteration
212+
:param iter_speed: number of seconds to complete a single text-sequence iteration
191213
:type iter_speed: :class:`float`
192214
:return: animated text-based progress sequence
193215
:rtype: None
194216
"""
195217

196218
# Start text animation.
197219
for iter in range(iter_total):
198-
print(f'{self.msg_loading}', end='', flush=True)
220+
print(f'{msg_loading}', end='', flush=True)
199221
sleep(iter_speed / 4)
200222

201223
for _ in range(3): # Add 3 dots to `msg_loading` message.
@@ -206,4 +228,4 @@ def __text_seq(self, iter_total: int, iter_speed: float) -> None:
206228

207229
iter -= 1
208230

209-
print(f'\n{self.msg_complete}\n') # Print completion message.
231+
print(f'\n{msg_complete}\n') # Print completion message.

PyLoadBar/tests/PyLoadBar_test.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,28 @@ def test_seqA():
77

88

99
def test_seqB():
10-
seq = PyLoadBar(enable_bar=False)
10+
seq = PyLoadBar(bar_sequence=False)
1111
assert seq.start() is None
1212

1313

1414
def test_seqC():
15-
seq = PyLoadBar(msg_loading='TEST',
15+
seq = PyLoadBar()
16+
assert seq.start(msg_loading='TEST',
1617
msg_complete='COMPLETE',
17-
label="LOADING")
18-
assert seq.start(iter_total=25, min_iter=0.001, max_iter=0.25) is None
18+
label="LOADING", iter_total=25, min_iter=0.001, max_iter=0.25) is None
1919

2020

2121
def test_seqD():
22-
seq = PyLoadBar(enable_bar=False,
23-
msg_loading='TEST',
24-
msg_complete='COMPLETE')
25-
assert seq.start(iter_total=5, txt_seq_speed=0.25) is None
22+
seq = PyLoadBar(bar_sequence=False,
23+
)
24+
assert seq.start(msg_loading='TEST',
25+
msg_complete='COMPLETE',
26+
iter_total=5,
27+
txt_seq_speed=0.25) is None
2628

2729

2830
def test_seqE():
29-
seq = PyLoadBar(4, 3463463.4, label=123)
30-
assert seq.start(iter_total=5, min_iter=0.001, max_iter=0.25) is None
31+
seq = PyLoadBar()
32+
assert seq.start(
33+
4, 3463463.4, label=123, iter_total=5, min_iter=0.001,
34+
max_iter=0.25) is None

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@
1313
- **A.** _Progress-bar_ style loading sequence
1414
- **B.** _Animated-text_ style loading sequence
1515

16-
- When instantiating a `PyLoadBar` object, messages can be customized by passing custom strings to the `msg_loading: str` and `msg_complete: str` parameters respectively.
16+
- When instantiating a `PyLoadBar` object, you may set the type of loading sequence using the `bar_sequence: bool` parameter.
17+
18+
- Once initialized, run the loading sequence using the `start()` method, and set sequence configuration using parameters.
19+
20+
- Messages can be customized by passing custom strings to the `msg_loading: str` and `msg_complete: str` parameters respectively.
1721

1822
- The loading message defaults to `"Loading..."`
1923
- The completion message defaults to `"Done!"`
2024

2125
- You may apply a label to the progress bar using the `label: str` parameter (defaults to `None`).
2226

23-
- **NOTE:** `enable_bar: bool` must be set to `True` for a label to be assigned to the progress bar.
24-
25-
- The desired loading sequence **can be toggled** using the `enable_bar: bool` parameter.
27+
- **NOTE:** `bar_sequence: bool` must be set to `True` for a label to be assigned to the progress bar.
2628

27-
- If `enable_bar: bool` is `False`, the progress-bar sequence will not be used, and the animated text-based loading sequence will be used instead.
29+
- If `bar_sequence: bool` is `False`, the _progress-bar sequence_ will **not** be used, and the _animated text-based_ loading sequence **will** be used instead.
2830

2931
- When calling the `start()` method and using the _progress-bar_ sequence, the time taken to complete each iteration can be determined using the `min_iter: float` and `max_iter: float` parameters.
3032

@@ -90,9 +92,9 @@ gh repo clone schlopp96/PyLoadBar
9092
```python
9193
>>> from PyLoadBar import PyLoadBar
9294

93-
>>> important_bar = PyLoadBar(msg_loading='Important Stuff Happening', msg_complete='Day Saved!', label='Saving Day') # Initialize a new `PyLoadBar` instance.
95+
>>> important_bar = PyLoadBar() # Initialize a new `PyLoadBar` instance.
9496

95-
>>> important_bar.start(min_iter=0.05, max_iter=1.0, iter_total=10) # Call `start` method to begin loading sequence.
97+
>>> important_bar.start(msg_loading='Important Stuff Happening', msg_complete='Day Saved!', label='Saving Day', min_iter=0.05, max_iter=1.0, iter_total=10) # Call `start` method to begin loading sequence.
9698

9799
Important Stuff Happening...
98100

@@ -106,9 +108,9 @@ gh repo clone schlopp96/PyLoadBar
106108
```python
107109
>>> from PyLoadBar import PyLoadBar
108110

109-
>>> bar = PyLoadBar(msg_loading='Loading', msg_complete='Done!', enable_bar=False) # Initialize loading sequence.
111+
>>> bar = PyLoadBar(bar_sequence=False) # Initialize loading sequence.
110112

111-
>>> bar.start(iter_total=1, txt_iter_speed=1) # Start animated-text loading sequence.
113+
>>> bar.start(msg_loading='Loading', msg_complete='Done!', iter_total=1, txt_iter_speed=1) # Start animated-text loading sequence.
112114

113115
# Note that during actual use case, text is printed to same line followed by incrementing dots:
114116

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='PyLoadBar',
10-
version="0.0.9.2",
10+
version="0.1.0",
1111
description=
1212
'Customizeable loading sequence/progress bar generator, enabling users to customize start/finish messages, toggle sequence type, and set total iterations among other features.',
1313
url='https://github.com/schlopp96/PyLoadBar',

0 commit comments

Comments
 (0)