Skip to content

Commit 804a8ec

Browse files
committed
PyLoadBar v0.0.9 development started.
- Overhauled `PyLoadBar` class. - Replaced "load" method with "start" method. - Messages and labels are now set when initializing `PyLoadBar` class. - 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". Signed-off-by: schlopp96 <71921821+schlopp96@users.noreply.github.com>
1 parent eaf3040 commit 804a8ec

4 files changed

Lines changed: 141 additions & 71 deletions

File tree

PyLoadBar/main.py

Lines changed: 122 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,143 @@
11
#!/usr/bin/env python3
22

3-
from time import sleep as s
3+
import sys
4+
from os import chdir
5+
from os.path import dirname
6+
from random import uniform
7+
from time import sleep
48

59
import tqdm
610

7-
__version__ = '0.0.8'
11+
sys.path.insert(0, dirname(
12+
dirname(__file__))) # Ensure module can be found by Python.
13+
14+
chdir(dirname(__file__)) # Change working directory to main module.
15+
16+
__version__ = '0.0.9'
817

918

1019
class PyLoadBar:
11-
"""Create simple loading sequences with ability to customize start/finish messages, toggle visual progress meter, and set time to completion."""
20+
"""Generate loading sequences with ability to customize start/finish messages, toggle visual progress meter, and set time to completion.
1221
13-
def __init__(self):
14-
pass
22+
---
1523
16-
def load(self,
17-
msg_loading: str = 'Loading',
18-
msg_complete: str = 'Done!',
19-
time: int = 5,
20-
label: str = None,
21-
enable_display: bool = True) -> None:
22-
"""
23-
Start a loading sequence which includes start/completion messages, and an optional progress meter.
24+
Settings:
25+
26+
- Set custom start/completion messages by passing string to `msg_loading` and `msg_complete` respectively.
27+
28+
- Toggle visual progress meter using `enable_display`.
29+
30+
- Apply label to progress meter by passing string to `label: str | None` (set to `None` by default)
31+
- Note that `enable_display: bool` must be set to `True` for this to take effect.
32+
33+
- When calling :func:`start`:
34+
35+
- Optionally set the total number of iterations to run using `iter_total: int`
2436
25-
- User may set custom start/completion messages using the parameters `msg_loading: str` and `msg_complete: str` respectively.
26-
- Set the sequence length in milliseconds using the `time: int` parameter.
27-
- Each unit of time = 0.1 seconds.
28-
- Display of the progress meter is activated by default, but may be disabled using the `enable_display` parameter.
29-
- The progress meter may be labeled using the `label: str` parameter (set to `None` by default).
30-
- Note that `enable_display` must be set to `True` for this to take effect.
37+
- Optionally set the minimum/maximum iteration length in seconds using the `min_iter: float` and `max_iter: float` parameters respectively
38+
- Default values are `min_iter: 0.1` seconds and `max_iter: 1.0` seconds
3139
32-
Examples:
40+
"""
3341

34-
- Load sequence WITH progress meter (default):
42+
def __init__(self,
43+
msg_loading: str | None = 'Loading...',
44+
msg_complete: str | None = 'Done!',
45+
label: str | None = None,
46+
enable_display: bool = True):
47+
"""Initialize loading sequence with set configuration.
3548
36-
>>> bar_ON = PyLoadBar() # < Create class instance.
37-
>>> bar_ON.load(msg_loading='Printing', msg_complete='Complete!', time=50) # <- Will take 5 seconds to complete loading sequence.
49+
---
50+
51+
:param msg_loading: initial loading message string, defaults to 'Loading...'
52+
:type msg_loading: :class:`str` | None, optional
53+
:param msg_complete: final message string displayed upon completion, defaults to 'Done!'
54+
:type msg_complete: :class:`str` | None, optional
55+
:param label: label displayed alongside progress bar, defaults to None
56+
:type label: :class:`str` | None, optional
57+
:param enable_display: toggle visible progress meter, defaults to True
58+
:type enable_display: :class:`bool`, optional
59+
"""
60+
61+
self.msg_loading: str | None = msg_loading
62+
self.msg_complete: str | None = msg_complete
63+
self.label: str | None = label
64+
self.enable_display: bool = enable_display
3865

39-
- Load sequence WITHOUT progress meter:
66+
def start(
67+
self,
68+
iter_total: int = 5,
69+
min_iter: float = 0.1,
70+
max_iter: float = 1.0,
71+
) -> None:
72+
"""Start loading sequence.
4073
41-
>>> bar_OFF = PyLoadBar() # < Create class instance.
42-
>>> bar_OFF.load('Processing Information', 'Finished!', time=20, enable_display=False) # <- Will take 2 seconds to complete loading sequence.
74+
---
75+
76+
- Set the total number of iterations using the `iter_total: int` parameter
77+
78+
- Set the minimum/maximum iteration length in seconds using the `min_iter: float` and `max_iter: float` parameters respectively
79+
- Default values are `min_iter: 0.1` seconds and `max_iter: 1.0` seconds
4380
4481
---
4582
46-
:param msg_loading: message to display during loading process, defaults to `"Loading"`.
47-
:type msg_loading: str, optional
48-
:param msg_complete: message to display upon load completion, defaults to `"Done!"`.
49-
:type msg_complete: str, optional
50-
:param time: time for loading sequence to complete measured in units of 0.1 seconds, defaults to `5`.
51-
:type time: int, optional
52-
:param label: label preceding progress bar, defaults to `None`.
53-
:type label: str, optional
54-
:param enable_display: toggle display of progress bar during loading process, defaults to `True`.
55-
:type enable_display: bool, optional
56-
:return: loading sequence.
57-
:rtype: None
83+
:param iter_total: number of iter_total until completion, defaults to 5
84+
:type iter_total: :class:`int`, optional
85+
:param min_iter: minimum possible time to complete an iteration, defaults to 1
86+
:type min_iter: :class:`int`, optional
87+
:param max_iter: maximum possible time to complete an iteration, defaults to 5
88+
:type max_iter: :class:`int`, optional
89+
"""
90+
91+
if self.enable_display:
92+
return self.__loadseq_A(iter_total, min_iter, max_iter)
93+
94+
else:
95+
return self.__loadseq_B(iter_total)
96+
97+
def __loadseq_A(self, iter_total: int, min_iter: float,
98+
max_iter: float) -> None:
99+
"""
100+
Run loading sequence and display progress with graphical progress bar.
101+
102+
:param iter_total: total number of iter_total to run
103+
:type iter_total: :class:`int`
104+
:param min_iter: minimum possible time (in seconds) an iteration can take
105+
:type min_iter: :class:`float`
106+
:param max_iter: maximum possible time (in seconds) an iteration can take
107+
:type max_iter: :class:`float`
108+
"""
109+
110+
print(f'\n{self.msg_loading}\n')
111+
112+
for iter in tqdm.trange(iter_total, desc=self.label):
113+
sleep(uniform(min_iter, max_iter))
114+
iter -= 1
115+
116+
print(f'\n{self.msg_complete}\n')
117+
118+
# Add a small delay to allow user to see completion message.
119+
sleep(0.5)
120+
121+
def __loadseq_B(self, iter_total: int) -> None:
122+
"""
123+
Run non-graphical loading sequence.
124+
125+
:param iter_total: Amount of iter_total to run loading sequence
126+
:type iter_total: :class:`int`
127+
:return: nonvisual loading sequence
128+
58129
"""
59130

60-
match enable_display:
61-
case True:
62-
print(f'{msg_loading}...\n')
63-
for iter in tqdm.trange(time, desc=label):
64-
s(0.1)
65-
iter -= 1
66-
print(f'\n{msg_complete}')
67-
68-
case False:
69-
print(f'{msg_loading}', end='')
70-
for iter in range(time):
71-
print('.', end='')
72-
s(0.1)
73-
iter -= 1
74-
print(f'\n\n{msg_complete}')
75-
s(0.5) # Add a small delay to allow user to see completion message.
131+
print(f'\n{self.msg_loading}', end='')
132+
133+
for iter in range(iter_total):
134+
for _ in range(3):
135+
print('.', end='')
136+
sleep(0.3)
137+
print('\b\b\b', end='')
138+
iter -= 1
139+
140+
print(f'\n\n{self.msg_complete}\n')
141+
142+
# Add a small delay to allow user to see completion message.
143+
sleep(0.5)

PyLoadBar/tests/PyLoadBar_test.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
from PyLoadBar.main import PyLoadBar
1+
from ..main import PyLoadBar
2+
23

34

45
def test_loadA():
56
bar = PyLoadBar()
6-
assert bar.load(time=1, enable_display=False) is None
7+
assert bar.start() is None
78

89

910
def test_loadB():
10-
bar = PyLoadBar()
11-
assert bar.load("Taking sweet time",
12-
"Finally...\n\nTook long enough.") is None
11+
bar = PyLoadBar("Taking sweet time",
12+
"Finally...\n\nTook long enough.", enable_display=False)
13+
assert bar.start(iter_total=3) is None
1314

1415

1516
def test_loadC():
16-
bar = PyLoadBar()
17-
assert bar.load('Exiting', 'Finished!!', enable_display=False) is None
17+
bar = PyLoadBar('Exiting', 'Finished!!')
18+
assert bar.start() is None
1819

1920

2021
def test_loadD():
21-
bar = PyLoadBar()
22-
assert bar.load(4, 3463463.4, time=3, enable_display=False) is None
22+
bar = PyLoadBar(
23+
4,
24+
3463463.4)
25+
assert bar.start(min_iter=0.3, max_iter=0.05) is None

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
setuptools==62.1.0
2-
tqdm==4.64.0
1+
setuptools>=62.1.0
2+
tqdm>=4.64.0

setup.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
setup(
99
name='PyLoadBar',
10-
version="0.0.8",
11-
description='Simple, easy-to-use loading sequence/progress bar module.',
10+
version="0.0.9",
11+
description=
12+
'Easy-to-use loading sequence/progress bar generator, enabling users to customize start/finish messages, toggle visual progress meter, set amount of iterations, among other features.',
1213
url='https://github.com/schlopp96/PyLoadBar',
1314
author='schlopp96',
1415
author_email='schloppdaddy@gmail.com',
@@ -20,16 +21,14 @@
2021
install_requires=[reqs],
2122
extras_require={"dev": ["pytest>=6.2.5"]},
2223
classifiers=[
23-
"Development Status :: 3 - Alpha",
24-
"Intended Audience :: Developers",
24+
"Development Status :: 3 - Alpha", "Intended Audience :: Developers",
2525
"Intended Audience :: End Users/Desktop",
2626
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
2727
"Natural Language :: English",
2828
"Operating System :: Microsoft :: Windows",
2929
"Programming Language :: Python :: 3",
3030
"Programming Language :: Python :: 3.9",
31-
"Programming Language :: Python :: 3.10",
32-
"Topic :: Utilities"
31+
"Programming Language :: Python :: 3.10", "Topic :: Utilities"
3332
],
3433
keywords=[
3534
'python, load, bar, loading, sequence, progress, simple, easy, utilities, package, module, tqdm, pytest, pyloadbar, developer, tool, tqdm'

0 commit comments

Comments
 (0)