Skip to content

Commit 02a2237

Browse files
committed
blacked the code
1 parent f36c1db commit 02a2237

17 files changed

Lines changed: 162 additions & 115 deletions

_python_utils_tests/test_decorators.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
@pytest.fixture
99
def random(monkeypatch):
1010
mock = MagicMock()
11-
monkeypatch.setattr("python_utils.decorators.random.random", mock, raising=True)
11+
monkeypatch.setattr(
12+
"python_utils.decorators.random.random", mock, raising=True
13+
)
1214
return mock
1315

1416

1517
def test_sample_called(random):
1618
demo_function = MagicMock()
17-
decorated = sample(0.5)(demo_function)
19+
decorated = sample(0.5)(demo_function)
1820
random.return_value = 0.4
1921
decorated()
2022
random.return_value = 0.0
@@ -33,4 +35,4 @@ def test_sample_not_called(random):
3335
decorated()
3436
random.return_value = 1.0
3537
decorated()
36-
assert demo_function.call_count == 0
38+
assert demo_function.call_count == 0

_python_utils_tests/test_generators.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ async def test_abatcher():
1616
async def test_abatcher_timed():
1717
batches = []
1818
async for batch in python_utils.abatcher(
19-
python_utils.acount(stop=10, delay=0.08),
20-
interval=0.2
19+
python_utils.acount(stop=10, delay=0.08), interval=0.2
2120
):
2221
batches.append(batch)
2322

_python_utils_tests/test_import.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ def test_import_globals_without_inspection():
1717
locals_ = {}
1818
globals_ = {'__name__': __name__}
1919
import_.import_global(
20-
'python_utils.formatters', locals_=locals_, globals_=globals_)
20+
'python_utils.formatters', locals_=locals_, globals_=globals_
21+
)
2122
assert 'camel_to_underscore' in globals_
2223

2324

2425
def test_import_globals_single_method():
2526
locals_ = {}
2627
globals_ = {'__name__': __name__}
2728
import_.import_global(
28-
'python_utils.formatters', ['camel_to_underscore'], locals_=locals_,
29-
globals_=globals_)
29+
'python_utils.formatters',
30+
['camel_to_underscore'],
31+
locals_=locals_,
32+
globals_=globals_,
33+
)
3034
assert 'camel_to_underscore' in globals_
3135

3236

@@ -37,12 +41,13 @@ def test_import_globals_with_inspection():
3741

3842
def test_import_globals_missing_module():
3943
import_.import_global(
40-
'python_utils.spam', exceptions=ImportError, locals_=locals())
44+
'python_utils.spam', exceptions=ImportError, locals_=locals()
45+
)
4146
assert 'camel_to_underscore' in globals()
4247

4348

4449
def test_import_locals_missing_module():
4550
import_.import_global(
46-
'python_utils.spam', exceptions=ImportError, globals_=globals())
51+
'python_utils.spam', exceptions=ImportError, globals_=globals()
52+
)
4753
assert 'camel_to_underscore' in globals()
48-

_python_utils_tests/test_python_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ def test_definitions():
77
assert __about__.__author__
88
assert __about__.__author_email__
99
assert __about__.__description__
10-

_python_utils_tests/test_time.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,54 @@
88

99

1010
@pytest.mark.parametrize(
11-
'timeout,interval,interval_multiplier,maximum_interval,iterable,result', [
11+
'timeout,interval,interval_multiplier,maximum_interval,iterable,result',
12+
[
1213
(0.2, 0.1, 0.4, 0.2, python_utils.acount, 2),
1314
(0.3, 0.1, 0.4, 0.2, python_utils.acount(), 3),
1415
(0.3, 0.06, 1.0, None, python_utils.acount, 5),
15-
(timedelta(seconds=0.1), timedelta(seconds=0.06),
16-
2.0, timedelta(seconds=0.1), python_utils.acount, 2),
17-
])
16+
(
17+
timedelta(seconds=0.1),
18+
timedelta(seconds=0.06),
19+
2.0,
20+
timedelta(seconds=0.1),
21+
python_utils.acount,
22+
2,
23+
),
24+
],
25+
)
1826
@pytest.mark.asyncio
19-
async def test_aio_timeout_generator(timeout, interval, interval_multiplier,
20-
maximum_interval, iterable, result):
27+
async def test_aio_timeout_generator(
28+
timeout, interval, interval_multiplier, maximum_interval, iterable, result
29+
):
2130
i = None
2231
async for i in python_utils.aio_timeout_generator(
23-
timeout, interval, iterable,
24-
maximum_interval=maximum_interval
32+
timeout, interval, iterable, maximum_interval=maximum_interval
2533
):
2634
pass
2735

2836
assert i == result
2937

3038

3139
@pytest.mark.parametrize(
32-
'timeout,interval,interval_multiplier,maximum_interval,iterable,result', [
40+
'timeout,interval,interval_multiplier,maximum_interval,iterable,result',
41+
[
3342
(0.01, 0.006, 0.5, 0.01, 'abc', 'c'),
3443
(0.01, 0.006, 0.5, 0.01, itertools.count, 2),
3544
(0.01, 0.006, 0.5, 0.01, itertools.count(), 2),
3645
(0.01, 0.006, 1.0, None, 'abc', 'c'),
37-
(timedelta(seconds=0.01),
38-
timedelta(seconds=0.006),
39-
2.0, timedelta(seconds=0.01),
40-
itertools.count, 2),
41-
])
42-
def test_timeout_generator(timeout, interval, interval_multiplier,
43-
maximum_interval, iterable, result):
46+
(
47+
timedelta(seconds=0.01),
48+
timedelta(seconds=0.006),
49+
2.0,
50+
timedelta(seconds=0.01),
51+
itertools.count,
52+
2,
53+
),
54+
],
55+
)
56+
def test_timeout_generator(
57+
timeout, interval, interval_multiplier, maximum_interval, iterable, result
58+
):
4459
i = None
4560
for i in python_utils.timeout_generator(
4661
timeout=timeout,
@@ -104,8 +119,7 @@ async def generator():
104119

105120
# Test regular timeout with clean exit
106121
@python_utils.aio_generator_timeout_detector_decorator(
107-
timeout=0.05,
108-
on_timeout=None
122+
timeout=0.05, on_timeout=None
109123
)
110124
async def generator():
111125
for i in range(10):
@@ -130,8 +144,7 @@ async def generator():
130144

131145
# Test total timeout with clean exit
132146
@python_utils.aio_generator_timeout_detector_decorator(
133-
total_timeout=0.1,
134-
on_timeout=None
147+
total_timeout=0.1, on_timeout=None
135148
)
136149
async def generator():
137150
for i in range(10):

python_utils/__about__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
__author_email__: str = 'Wolph@wol.ph'
44
__description__: str = (
55
'Python Utils is a module with some convenient utilities not included '
6-
'with the standard Python install')
6+
'with the standard Python install'
7+
)
78
__url__: str = 'https://github.com/WoLpH/python-utils'
89
# Omit type info due to automatic versioning script
910
__version__ = '3.3.1'

python_utils/containers.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,17 @@ class CastedDictBase(types.Dict[KT, VT], abc.ABC):
2828
_value_cast: VT_cast
2929

3030
def __init__(
31-
self,
32-
key_cast: KT_cast = None,
33-
value_cast: VT_cast = None,
34-
*args,
35-
**kwargs
31+
self,
32+
key_cast: KT_cast = None,
33+
value_cast: VT_cast = None,
34+
*args,
35+
**kwargs,
3636
) -> None:
3737
self._value_cast = value_cast
3838
self._key_cast = key_cast
3939
self.update(*args, **kwargs)
4040

41-
def update(
42-
self,
43-
*args: DictUpdateArgs,
44-
**kwargs: VT
45-
) -> None:
41+
def update(self, *args: DictUpdateArgs, **kwargs: VT) -> None:
4642
if args:
4743
kwargs.update(*args)
4844

python_utils/converters.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88

99
def to_int(
10-
input_: typing.Optional[str] = None,
11-
default: int = 0,
12-
exception: types.ExceptionsType = (ValueError, TypeError),
13-
regexp: types.O[types.Pattern] = None,
10+
input_: typing.Optional[str] = None,
11+
default: int = 0,
12+
exception: types.ExceptionsType = (ValueError, TypeError),
13+
regexp: types.O[types.Pattern] = None,
1414
) -> int:
1515
r'''
1616
Convert the given input to an integer or return default
@@ -97,10 +97,10 @@ def to_int(
9797

9898

9999
def to_float(
100-
input_: str,
101-
default: int = 0,
102-
exception: types.ExceptionsType = (ValueError, TypeError),
103-
regexp: types.O[types.Pattern] = None,
100+
input_: str,
101+
default: int = 0,
102+
exception: types.ExceptionsType = (ValueError, TypeError),
103+
regexp: types.O[types.Pattern] = None,
104104
) -> types.Number:
105105
r'''
106106
Convert the given `input_` to an integer or return default
@@ -174,9 +174,9 @@ def to_float(
174174

175175

176176
def to_unicode(
177-
input_: types.StringTypes,
178-
encoding: str = 'utf-8',
179-
errors: str = 'replace',
177+
input_: types.StringTypes,
178+
encoding: str = 'utf-8',
179+
errors: str = 'replace',
180180
) -> str:
181181
'''Convert objects to unicode, if needed decodes string with the given
182182
encoding and errors settings.
@@ -203,9 +203,9 @@ def to_unicode(
203203

204204

205205
def to_str(
206-
input_: types.StringTypes,
207-
encoding: str = 'utf-8',
208-
errors: str = 'replace',
206+
input_: types.StringTypes,
207+
encoding: str = 'utf-8',
208+
errors: str = 'replace',
209209
) -> bytes:
210210
'''Convert objects to string, encodes to the given encoding
211211
@@ -234,7 +234,8 @@ def to_str(
234234

235235

236236
def scale_1024(
237-
x: types.Number, n_prefixes: int,
237+
x: types.Number,
238+
n_prefixes: int,
238239
) -> types.Tuple[types.Number, types.Number]:
239240
'''Scale a number down to a suitable size, based on powers of 1024.
240241
@@ -262,9 +263,11 @@ def scale_1024(
262263

263264

264265
def remap(
265-
value: types.DecimalNumber,
266-
old_min: types.DecimalNumber, old_max: types.DecimalNumber,
267-
new_min: types.DecimalNumber, new_max: types.DecimalNumber,
266+
value: types.DecimalNumber,
267+
old_min: types.DecimalNumber,
268+
old_max: types.DecimalNumber,
269+
new_min: types.DecimalNumber,
270+
new_max: types.DecimalNumber,
268271
) -> types.DecimalNumber:
269272
'''
270273
remap a value from one range into another.
@@ -340,19 +343,19 @@ def remap(
340343
'''
341344
type_: types.Type[types.DecimalNumber]
342345
if (
343-
isinstance(value, decimal.Decimal) or
344-
isinstance(old_min, decimal.Decimal) or
345-
isinstance(old_max, decimal.Decimal) or
346-
isinstance(new_min, decimal.Decimal) or
347-
isinstance(new_max, decimal.Decimal)
346+
isinstance(value, decimal.Decimal)
347+
or isinstance(old_min, decimal.Decimal)
348+
or isinstance(old_max, decimal.Decimal)
349+
or isinstance(new_min, decimal.Decimal)
350+
or isinstance(new_max, decimal.Decimal)
348351
):
349352
type_ = decimal.Decimal
350353
elif (
351-
isinstance(value, float) or
352-
isinstance(old_min, float) or
353-
isinstance(old_max, float) or
354-
isinstance(new_min, float) or
355-
isinstance(new_max, float)
354+
isinstance(value, float)
355+
or isinstance(old_min, float)
356+
or isinstance(old_max, float)
357+
or isinstance(new_min, float)
358+
or isinstance(new_max, float)
356359
):
357360
type_ = float
358361

@@ -369,12 +372,14 @@ def remap(
369372
new_range = new_max - new_min # type: ignore
370373

371374
if old_range == 0:
372-
raise ValueError('Input range ({}-{}) is empty'.format(
373-
old_min, old_max))
375+
raise ValueError(
376+
'Input range ({}-{}) is empty'.format(old_min, old_max)
377+
)
374378

375379
if new_range == 0:
376-
raise ValueError('Output range ({}-{}) is empty'.format(
377-
new_min, new_max))
380+
raise ValueError(
381+
'Output range ({}-{}) is empty'.format(new_min, new_max)
382+
)
378383

379384
new_value = (value - old_min) * new_range # type: ignore
380385

python_utils/decorators.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,20 @@ def sample(sample_rate: float):
111111
Calls to *demo_function* will be limited to 50% approximatly.
112112
113113
'''
114+
114115
def _sample(function):
115116
@functools.wraps(function)
116117
def __sample(*args, **kwargs):
117118
if random.random() < sample_rate:
118119
return function(*args, **kwargs)
119120
else:
120-
logging.debug('Skipped execution of %r(%r, %r) due to sampling', function, args, kwargs) # noqa: E501
121+
logging.debug(
122+
'Skipped execution of %r(%r, %r) due to sampling',
123+
function,
124+
args,
125+
kwargs,
126+
) # noqa: E501
121127

122128
return __sample
129+
123130
return _sample

python_utils/formatters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def camel_to_underscore(name: str) -> str:
3232
elif i > 3 and not c.isupper():
3333
# Will return the last 3 letters to check if we are changing
3434
# case
35-
previous = name[i - 3:i]
35+
previous = name[i - 3 : i]
3636
if previous.isalpha() and previous.isupper():
3737
output.insert(len(output) - 1, '_')
3838

@@ -77,7 +77,7 @@ def apply_recursive(
7777

7878
def timesince(
7979
dt: types.Union[datetime.datetime, datetime.timedelta],
80-
default: str = 'just now'
80+
default: str = 'just now',
8181
) -> str:
8282
'''
8383
Returns string representing 'time since' e.g.

0 commit comments

Comments
 (0)