Skip to content

Commit 671b723

Browse files
committed
fixed ruff issues
1 parent 16c4fce commit 671b723

File tree

11 files changed

+329
-308
lines changed

11 files changed

+329
-308
lines changed

progressbar/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
from datetime import date
22

33
from .__about__ import __author__, __version__
4+
from .algorithms import (
5+
DoubleExponentialMovingAverage,
6+
ExponentialMovingAverage,
7+
SmoothingAlgorithm,
8+
)
49
from .bar import DataTransferBar, NullBar, ProgressBar
510
from .base import UnknownLength
611
from .multi import MultiBar, SortKey
712
from .shortcuts import progressbar
813
from .terminal.stream import LineOffsetStreamWrapper
914
from .utils import len_color, streams
10-
from .algorithms import ExponentialMovingAverage, SmoothingAlgorithm, DoubleExponentialMovingAverage
1115
from .widgets import (
1216
ETA,
1317
AbsoluteETA,
1418
AdaptiveETA,
15-
SmoothingETA,
1619
AdaptiveTransferSpeed,
1720
AnimatedMarker,
1821
Bar,
@@ -34,11 +37,11 @@
3437
ReverseBar,
3538
RotatingMarker,
3639
SimpleProgress,
40+
SmoothingETA,
3741
Timer,
3842
Variable,
3943
VariableMixin,
4044
)
41-
from .algorithms import ExponentialMovingAverage, SmoothingAlgorithm
4245

4346
__date__ = str(date.today())
4447
__all__ = [

progressbar/algorithms.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
class SmoothingAlgorithm(abc.ABC):
8-
98
@abc.abstractmethod
109
def __init__(self, **kwargs):
1110
raise NotImplementedError
@@ -41,7 +40,7 @@ class DoubleExponentialMovingAverage(SmoothingAlgorithm):
4140
It's more responsive to recent changes in data.
4241
'''
4342

44-
def __init__(self, alpha: float=0.5) -> None:
43+
def __init__(self, alpha: float = 0.5) -> None:
4544
self.alpha = alpha
4645
self.ema1 = 0
4746
self.ema2 = 0
@@ -50,4 +49,3 @@ def update(self, new_value: float, elapsed: timedelta) -> float:
5049
self.ema1 = self.alpha * new_value + (1 - self.alpha) * self.ema1
5150
self.ema2 = self.alpha * self.ema1 + (1 - self.alpha) * self.ema2
5251
return 2 * self.ema1 - self.ema2
53-

progressbar/env.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from . import base
1010

1111

12-
1312
@typing.overload
1413
def env_flag(name: str, default: bool) -> bool:
1514
...
@@ -68,7 +67,7 @@ def from_env(cls):
6867
)
6968

7069
if os.environ.get('JUPYTER_COLUMNS') or os.environ.get(
71-
'JUPYTER_LINES',
70+
'JUPYTER_LINES',
7271
):
7372
# Jupyter notebook always supports true color.
7473
return cls.XTERM_TRUECOLOR
@@ -77,9 +76,10 @@ def from_env(cls):
7776
# will assume it is supported if the console is configured to
7877
# support it.
7978
from .terminal.os_specific import windows
79+
8080
if (
81-
windows.get_console_mode() &
82-
windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
81+
windows.get_console_mode()
82+
& windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
8383
):
8484
return cls.XTERM_TRUECOLOR
8585
else:
@@ -103,8 +103,8 @@ def from_env(cls):
103103

104104

105105
def is_ansi_terminal(
106-
fd: base.IO,
107-
is_terminal: bool | None = None,
106+
fd: base.IO,
107+
is_terminal: bool | None = None,
108108
) -> bool | None: # pragma: no cover
109109
if is_terminal is None:
110110
# Jupyter Notebooks define this variable and support progress bars
@@ -113,7 +113,7 @@ def is_ansi_terminal(
113113
# This works for newer versions of pycharm only. With older versions
114114
# there is no way to check.
115115
elif os.environ.get('PYCHARM_HOSTED') == '1' and not os.environ.get(
116-
'PYTEST_CURRENT_TEST',
116+
'PYTEST_CURRENT_TEST',
117117
):
118118
is_terminal = True
119119

@@ -133,9 +133,10 @@ def is_ansi_terminal(
133133
is_terminal = True
134134
elif os.name == 'nt':
135135
from .terminal.os_specific import windows
136+
136137
return bool(
137-
windows.get_console_mode() &
138-
windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
138+
windows.get_console_mode()
139+
& windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT,
139140
)
140141
else:
141142
is_terminal = None

progressbar/terminal/base.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
import enum
77
import threading
88
from collections import defaultdict
9+
910
# Ruff is being stupid and doesn't understand `ClassVar` if it comes from the
1011
# `types` module
1112
from typing import ClassVar
1213

1314
from python_utils import converters, types
1415

15-
from .os_specific import getch
1616
from .. import (
1717
base as pbase,
1818
env,
1919
)
20+
from .os_specific import getch
2021

2122
ESC = '\x1B'
2223

@@ -178,7 +179,6 @@ def column(self, stream):
178179
return column
179180

180181

181-
182182
class WindowsColors(enum.Enum):
183183
BLACK = 0, 0, 0
184184
BLUE = 0, 0, 128
@@ -235,20 +235,23 @@ class WindowsColor:
235235
>>> WindowsColor(WindowsColors.RED)('test')
236236
'test'
237237
'''
238-
__slots__ = 'color',
238+
239+
__slots__ = ('color',)
239240

240241
def __init__(self, color: Color):
241242
self.color = color
242243

243244
def __call__(self, text):
244245
return text
245-
# In the future we might want to use this, but it requires direct printing to stdout and all of our surrounding functions expect buffered output so it's not feasible right now.
246-
# Additionally, recent Windows versions all support ANSI codes without issue so there is little need.
246+
## In the future we might want to use this, but it requires direct
247+
## printing to stdout and all of our surrounding functions expect
248+
## buffered output so it's not feasible right now. Additionally,
249+
## recent Windows versions all support ANSI codes without issue so
250+
## there is little need.
247251
# from progressbar.terminal.os_specific import windows
248252
# windows.print_color(text, WindowsColors.from_rgb(self.color.rgb))
249253

250254

251-
252255
class RGB(collections.namedtuple('RGB', ['red', 'green', 'blue'])):
253256
__slots__ = ()
254257

@@ -387,14 +390,14 @@ def underline(self):
387390
@property
388391
def ansi(self) -> types.Optional[str]:
389392
if (
390-
env.COLOR_SUPPORT is env.ColorSupport.XTERM_TRUECOLOR
393+
env.COLOR_SUPPORT is env.ColorSupport.XTERM_TRUECOLOR
391394
): # pragma: no branch
392395
return f'2;{self.rgb.red};{self.rgb.green};{self.rgb.blue}'
393396

394397
if self.xterm: # pragma: no branch
395398
color = self.xterm
396399
elif (
397-
env.COLOR_SUPPORT is env.ColorSupport.XTERM_256
400+
env.COLOR_SUPPORT is env.ColorSupport.XTERM_256
398401
): # pragma: no branch
399402
color = self.rgb.to_ansi_256
400403
elif env.COLOR_SUPPORT is env.ColorSupport.XTERM: # pragma: no branch
@@ -442,11 +445,11 @@ class Colors:
442445

443446
@classmethod
444447
def register(
445-
cls,
446-
rgb: RGB,
447-
hls: types.Optional[HSL] = None,
448-
name: types.Optional[str] = None,
449-
xterm: types.Optional[int] = None,
448+
cls,
449+
rgb: RGB,
450+
hls: types.Optional[HSL] = None,
451+
name: types.Optional[str] = None,
452+
xterm: types.Optional[int] = None,
450453
) -> Color:
451454
color = Color(rgb, hls, name, xterm)
452455

@@ -483,9 +486,9 @@ def __call__(self, value: float) -> Color:
483486
def get_color(self, value: float) -> Color:
484487
'Map a value from 0 to 1 to a color.'
485488
if (
486-
value == pbase.Undefined
487-
or value == pbase.UnknownLength
488-
or value <= 0
489+
value == pbase.Undefined
490+
or value == pbase.UnknownLength
491+
or value <= 0
489492
):
490493
return self.colors[0]
491494
elif value >= 1:
@@ -531,14 +534,14 @@ def get_color(value: float, color: OptionalColor) -> Color | None:
531534

532535

533536
def apply_colors(
534-
text: str,
535-
percentage: float | None = None,
536-
*,
537-
fg: OptionalColor = None,
538-
bg: OptionalColor = None,
539-
fg_none: Color | None = None,
540-
bg_none: Color | None = None,
541-
**kwargs: types.Any,
537+
text: str,
538+
percentage: float | None = None,
539+
*,
540+
fg: OptionalColor = None,
541+
bg: OptionalColor = None,
542+
fg_none: Color | None = None,
543+
bg_none: Color | None = None,
544+
**kwargs: types.Any,
542545
) -> str:
543546
'''Apply colors/gradients to a string depending on the given percentage.
544547

progressbar/terminal/os_specific/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
if sys.platform.startswith('win'):
44
from .windows import (
5+
get_console_mode as _get_console_mode,
56
getch as _getch,
67
reset_console_mode as _reset_console_mode,
78
set_console_mode as _set_console_mode,
8-
get_console_mode as _get_console_mode,
99
)
1010

1111
else:

progressbar/terminal/os_specific/windows.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,16 @@ def reset_console_mode():
126126

127127

128128
def set_console_mode() -> bool:
129-
mode = _input_mode.value | WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_INPUT
129+
mode = (
130+
_input_mode.value
131+
| WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_INPUT
132+
)
130133
_SetConsoleMode(_HANDLE(_h_console_input), _DWORD(mode))
131134

132135
mode = (
133-
_output_mode.value
134-
| WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
135-
| WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_PROCESSING
136+
_output_mode.value
137+
| WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
138+
| WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_PROCESSING
136139
)
137140
return bool(_SetConsoleMode(_HANDLE(_h_console_output), _DWORD(mode)))
138141

@@ -147,7 +150,7 @@ def set_text_color(color):
147150

148151
def print_color(text, color):
149152
set_text_color(color)
150-
print(text)
153+
print(text) # noqa: T201
151154
set_text_color(7) # Reset to default color, grey
152155

153156

0 commit comments

Comments
 (0)