Skip to content

Commit 35ba6a8

Browse files
Added additional_variables argument to log21.ProgressBar class. You can use it in order to add additional variables to the progress bar.
1 parent 672ecdb commit 35ba6a8

7 files changed

Lines changed: 104 additions & 36 deletions

File tree

.github/workflows/automatic-release.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ jobs:
2323
- name: Build
2424
run: |
2525
python setup.py sdist bdist_wheel
26+
pip install dist/*.whl
27+
export PACKAGE_VERSION=$(python -c "import log21; print(log21.__version__)")
2628
- uses: "marvinpinto/action-automatic-releases@latest"
2729
with:
2830
repo_token: "${{ secrets.GITHUB_TOKEN }}"
29-
automatic_release_tag: "latest"
31+
automatic_release_tag: PACKAGE_VERSION
3032
title: "Auto Build"
3133
files: |
3234
dist/*

CHANGES-LOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ Help this project by [Donation](DONATE.md)
66
Changes log
77
-----------
88

9+
### 2.3.2
10+
11+
Added `additional_variables` argument to `log21.ProgressBar` class. You can use it in order to add additional variables
12+
to the progress bar:
13+
14+
```python3
15+
import log21, time
16+
17+
progress_bar = log21.ProgressBar(format_='Iteration: {i} {prefix}{bar}{suffix} {percentage}%', style='{',
18+
additional_variables={"i": 0})
19+
20+
for i in range(100):
21+
progress_bar(i + 1, 100, i=i)
22+
time.sleep(0.1)
23+
# Iteration: 99 |██████████████████████████████████████████████████████████████████████████████| 100%
24+
```
25+
926
### 2.3.1
1027

1128
Added `formatter` argument to `StreamHandler` and `FileHandler`. You can use it to set the formatter of the handler when

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,22 @@ python setup.py install
5353
Changes
5454
-------
5555

56-
### 2.3.1
56+
### 2.3.2
5757

58-
Added `formatter` argument to `StreamHandler` and `FileHandler`. You can use it to set the formatter of the handler when
59-
you create it. Added `handlers` argument to `Logger`. You can use it to add handlers to the logger when you create it.
58+
Added `additional_variables` argument to `log21.ProgressBar` class. You can use it in order to add additional variables
59+
to the progress bar:
60+
61+
```python3
62+
import log21, time
63+
64+
progress_bar = log21.ProgressBar(format_='Iteration: {i} {prefix}{bar}{suffix} {percentage}%', style='{',
65+
additional_variables={"i": 0})
66+
67+
for i in range(100):
68+
progress_bar(i + 1, 100, i=i)
69+
time.sleep(0.1)
70+
# Iteration: 99 |██████████████████████████████████████████████████████████████████████████████| 100%
71+
```
6072

6173
[Full Changes Log](https://github.com/MPCodeWriter21/log21/blob/master/CHANGES-LOG.md)
6274

log21/Logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ def getpass(self, *msg, args: tuple = (), end='', **kwargs):
175175
self._log(self.level if self.level >= NOTSET else NOTSET, msg, args, **kwargs)
176176
return _getpass('')
177177

178-
def print_progress(self, progress: float, total: float):
178+
def print_progress(self, progress: float, total: float, **kwargs):
179179
"""
180180
Log progress.
181181
"""
182-
self.progress_bar(progress, total)
182+
self.progress_bar(progress, total, **kwargs)
183183

184184
@property
185185
def progress_bar(self):

log21/ProgressBar.py

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import os as _os
55

6+
from typing import Dict as _Dict, Any as _Any
7+
68
import log21 as _log21
79
from log21.Logger import Logger as _Logger
810
from log21.StreamHandler import ColorizingStreamHandler as _ColorizingStreamHandler
@@ -39,7 +41,8 @@ class ProgressBar:
3941

4042
def __init__(self, *args, width: int = None, show_percentage: bool = True, prefix: str = '|', suffix: str = '|',
4143
fill: str = '█', empty: str = ' ', format_: str = None, style: str = '%',
42-
new_line_when_complete: bool = True, colors: dict = None, logger: '_log21.Logger' = _logger):
44+
new_line_when_complete: bool = True, colors: dict = None, logger: '_log21.Logger' = _logger,
45+
additional_variables: _Dict[str, _Any] = None):
4346
"""
4447
:param args: Prevents the use of positional arguments
4548
:param width: The width of the progress bar
@@ -53,6 +56,7 @@ def __init__(self, *args, width: int = None, show_percentage: bool = True, prefi
5356
:param new_line_when_complete: Whether to print a new line when the progress is complete or failed
5457
:param colors: The colors of the progress bar
5558
:param logger: The logger to use
59+
:param additional_variables: Additional variables to use in the format and their default values
5660
"""
5761
self.width = width if width else _os.get_terminal_size().columns - 1
5862
if self.width < 3:
@@ -71,6 +75,16 @@ def __init__(self, *args, width: int = None, show_percentage: bool = True, prefi
7175
raise ValueError('`empty` must be a single character')
7276
if style not in ['%', '{']:
7377
raise ValueError('`style` must be either `%` or `{`')
78+
if additional_variables:
79+
if not isinstance(additional_variables, dict):
80+
raise TypeError('`additional_variables` must be a dictionary')
81+
for key, value in additional_variables.items():
82+
if not isinstance(key, str):
83+
raise TypeError('`additional_variables` keys must be strings')
84+
if not isinstance(value, str):
85+
additional_variables[key] = str(value)
86+
else:
87+
additional_variables = dict()
7488

7589
self.colors = {
7690
'progress in-progress': _gc('LightYellow'),
@@ -105,24 +119,30 @@ def __init__(self, *args, width: int = None, show_percentage: bool = True, prefi
105119
for key, value in colors.items():
106120
self.colors[key] = value
107121
self.logger = logger
122+
self.additional_variables = additional_variables
108123
self.i = 0
109124

110-
def get_bar(self, progress: float, total: float):
125+
def get_bar(self, progress: float, total: float, **kwargs) -> str:
111126
if progress == total:
112-
return self.progress_complete()
127+
return self.progress_complete(**kwargs)
113128
elif progress > total or progress < 0:
114-
return self.progress_failed(progress, total)
129+
return self.progress_failed(progress, total, **kwargs)
115130
else:
116-
return self.progress_in_progress(progress, total)
131+
return self.progress_in_progress(progress, total, **kwargs)
117132

118-
def progress_in_progress(self, progress: float, total: float):
133+
def progress_in_progress(self, progress: float, total: float, **kwargs):
119134
percentage = str(round(progress / total * 100, 2))
120135
progress_dict = {
121136
'prefix': self.prefix,
122137
'bar': '',
123138
'suffix': self.suffix,
124-
'percentage': percentage
139+
'percentage': percentage,
140+
**self.additional_variables
125141
}
142+
for key, value in kwargs.items():
143+
if key in ['prefix', 'bar', 'suffix', 'percentage']:
144+
raise ValueError(f'`{key}` is a reserved keyword')
145+
progress_dict[key] = value
126146

127147
if self.style == '%':
128148
used_characters = len(self.format % progress_dict)
@@ -142,28 +162,34 @@ def progress_in_progress(self, progress: float, total: float):
142162

143163
progress_dict = {
144164
'prefix': self.colors['prefix-color in-progress'] + self.prefix + self.colors['reset-color'],
145-
'bar': self.colors['progress in-progress'] +
146-
(self.fill * fill_length + spinner_char + self.empty * empty_length) + self.colors['reset-color'],
165+
'bar': self.colors['progress in-progress'] + (self.fill * fill_length + spinner_char +
166+
self.empty * empty_length) + self.colors['reset-color'],
147167
'suffix': self.colors['suffix-color in-progress'] + self.suffix + self.colors['reset-color'],
148-
'percentage': self.colors["percentage in-progress"] + str(percentage) + self.colors['reset-color']
168+
'percentage': self.colors["percentage in-progress"] + str(percentage) + self.colors['reset-color'],
169+
**self.additional_variables
149170
}
171+
for key, value in kwargs.items():
172+
progress_dict[key] = value
150173

151174
if self.style == '%':
152-
bar = self.format % progress_dict
175+
return '\r' + self.format % progress_dict + self.colors['reset-color']
153176
elif self.style == '{':
154-
bar = self.format.format(**progress_dict)
177+
return '\r' + self.format.format(**progress_dict) + self.colors['reset-color']
155178
else:
156179
raise ValueError('`style` must be either `%` or `{`')
157180

158-
return '\r' + bar + self.colors['reset-color']
159-
160-
def progress_complete(self):
181+
def progress_complete(self, **kwargs):
161182
progress_dict = {
162183
'prefix': self.prefix,
163184
'bar': '',
164185
'suffix': self.suffix,
165-
'percentage': '100'
186+
'percentage': '100',
187+
**self.additional_variables
166188
}
189+
for key, value in kwargs.items():
190+
if key in ['prefix', 'bar', 'suffix', 'percentage']:
191+
raise ValueError(f'`{key}` is a reserved keyword')
192+
progress_dict[key] = value
167193

168194
if self.style == '%':
169195
bar_length = self.width - len(self.format % progress_dict)
@@ -176,25 +202,33 @@ def progress_complete(self):
176202
'prefix': self.colors['prefix-color complete'] + self.prefix + self.colors['reset-color'],
177203
'bar': self.colors['progress complete'] + (self.fill * bar_length) + self.colors['reset-color'],
178204
'suffix': self.colors['suffix-color complete'] + self.suffix + self.colors['reset-color'],
179-
'percentage': self.colors["percentage complete"] + '100' + self.colors['reset-color']
205+
'percentage': self.colors["percentage complete"] + '100' + self.colors['reset-color'],
206+
**self.additional_variables
180207
}
208+
for key, value in kwargs.items():
209+
progress_dict[key] = value
181210

182211
if self.style == '%':
183-
bar = self.format % progress_dict
212+
return '\r' + self.format % progress_dict + self.colors['reset-color'] + \
213+
('\n' if self.new_line_when_complete else '')
184214
elif self.style == '{':
185-
bar = self.format.format(**progress_dict)
215+
return '\r' + self.format.format(**progress_dict) + self.colors['reset-color'] + \
216+
('\n' if self.new_line_when_complete else '')
186217
else:
187218
raise ValueError('`style` must be either `%` or `{`')
188219

189-
return '\r' + bar + self.colors['reset-color'] + ('\n' if self.new_line_when_complete else '')
190-
191-
def progress_failed(self, progress: float, total: float):
220+
def progress_failed(self, progress: float, total: float, **kwargs):
192221
progress_dict = {
193222
'prefix': self.prefix,
194223
'bar': '',
195224
'suffix': self.suffix,
196-
'percentage': str(round(progress / total * 100, 2))
225+
'percentage': str(round(progress / total * 100, 2)),
226+
**self.additional_variables
197227
}
228+
for key, value in kwargs.items():
229+
if key in ['prefix', 'bar', 'suffix', 'percentage']:
230+
raise ValueError(f'`{key}` is a reserved keyword')
231+
progress_dict[key] = value
198232

199233
if self.style == '%':
200234
bar_length = self.width - len(self.format % progress_dict)
@@ -212,8 +246,11 @@ def progress_failed(self, progress: float, total: float):
212246
'prefix': self.colors['prefix-color failed'] + self.prefix + self.colors['reset-color'],
213247
'bar': self.colors['progress failed'] + (bar_char * bar_length) + self.colors['reset-color'],
214248
'suffix': self.colors['suffix-color failed'] + self.suffix + self.colors['reset-color'],
215-
'percentage': self.colors["percentage failed"] + progress_dict['percentage'] + self.colors['reset-color']
249+
'percentage': self.colors["percentage failed"] + progress_dict['percentage'] + self.colors['reset-color'],
250+
**self.additional_variables
216251
}
252+
for key, value in kwargs.items():
253+
progress_dict[key] = value
217254

218255
if self.style == '%':
219256
bar = self.format % progress_dict
@@ -224,11 +261,11 @@ def progress_failed(self, progress: float, total: float):
224261

225262
return '\r' + bar + self.colors['reset-color'] + ('\n' if self.new_line_when_complete else '')
226263

227-
def __call__(self, progress: float, total: float, logger: '_log21.Logger' = None):
264+
def __call__(self, progress: float, total: float, logger: '_log21.Logger' = None, **kwargs):
228265
if not logger:
229266
logger = self.logger
230267

231-
logger.print(self.get_bar(progress, total), end='')
268+
logger.print(self.get_bar(progress, total, **kwargs), end='')
232269

233-
def update(self, progress: float, total: float, logger: '_log21.Logger' = None):
234-
self(progress, total, logger)
270+
def update(self, progress: float, total: float, logger: '_log21.Logger' = None, **kwargs):
271+
self(progress, total, logger, **kwargs)

log21/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from log21.Formatters import ColorizingFormatter, DecolorizingFormatter
2222
from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color
2323

24-
__version__ = "2.3.1"
24+
__version__ = "2.3.2"
2525
__author__ = "CodeWriter21 (Mehrad Pooryoussof)"
2626
__github__ = "Https://GitHub.com/MPCodeWriter21/log21"
2727
__all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter',

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
long_description = file.read()
88

99
DESCRIPTION = 'A simple logging package that helps you log colorized messages in Windows console.'
10-
VERSION = '2.3.1'
10+
VERSION = '2.3.2'
1111

1212
setup(
1313
name='log21',

0 commit comments

Comments
 (0)