Skip to content

Commit f54ca99

Browse files
Formatting code with ruff
1 parent d8a7d03 commit f54ca99

File tree

10 files changed

+237
-203
lines changed

10 files changed

+237
-203
lines changed

src/thread/__init__.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,27 @@
1919

2020

2121
# Export Core
22-
from .thread import (
23-
Thread,
24-
ParallelProcessing
25-
)
22+
from .thread import Thread, ParallelProcessing
2623

2724

28-
from . import (
29-
_types as types,
30-
exceptions
31-
)
25+
from . import _types as types, exceptions
3226

3327

3428
# Export decorators
35-
from .decorators import (
36-
threaded,
37-
processor
38-
)
29+
from .decorators import threaded, processor
3930

4031

4132
# Configuration
4233
from .utils import Settings
34+
35+
36+
# Wildcard Export
37+
__all__ = [
38+
'Thread',
39+
'ParallelProcessing',
40+
'threaded',
41+
'processor',
42+
'types',
43+
'exceptions',
44+
'Settings',
45+
]

src/thread/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
21
# To make CLI accessible with py/python/python3 -m thread ...
32
from .cli import app
43

54
if __name__ == '__main__':
6-
app(prog_name = 'thread')
5+
app(prog_name='thread')

src/thread/_types.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
# Variable Types
1818
ThreadStatus = Literal[
19-
'Idle',
20-
'Running',
21-
'Invoking hooks',
22-
'Completed',
23-
24-
'Errored',
25-
'Kill Scheduled',
26-
'Killed'
19+
'Idle',
20+
'Running',
21+
'Invoking hooks',
22+
'Completed',
23+
'Errored',
24+
'Kill Scheduled',
25+
'Killed',
2726
]
2827

2928

src/thread/cli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
21
try:
32
import importlib
3+
44
thread_cli = importlib.import_module('thread-cli')
55
app = thread_cli.app
66
except ModuleNotFoundError:
7-
def app(prog_name = 'thread'):
7+
8+
def app(prog_name='thread'):
89
print('thread-cli not found, please install it with `pip install thread-cli`')
9-
exit(1)
10+
exit(1)

src/thread/decorators/_processor.py

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,26 @@
1818
TargetFunction = Callable[Concatenate[_DataT, _TargetP], _TargetT]
1919

2020

21-
NoParamReturn = Callable[Concatenate[Sequence[_DataT], _TargetP], ParallelProcessing[_TargetP, _TargetT, _DataT]]
22-
WithParamReturn = Callable[[TargetFunction[_DataT, _TargetP, _TargetT]], NoParamReturn[_DataT, _TargetP, _TargetT]]
23-
FullParamReturn = Callable[Concatenate[Sequence[_DataT], _TargetP], ParallelProcessing[_TargetP, _TargetT, _DataT]]
24-
25-
21+
NoParamReturn = Callable[
22+
Concatenate[Sequence[_DataT], _TargetP],
23+
ParallelProcessing[_TargetP, _TargetT, _DataT],
24+
]
25+
WithParamReturn = Callable[
26+
[TargetFunction[_DataT, _TargetP, _TargetT]],
27+
NoParamReturn[_DataT, _TargetP, _TargetT],
28+
]
29+
FullParamReturn = Callable[
30+
Concatenate[Sequence[_DataT], _TargetP],
31+
ParallelProcessing[_TargetP, _TargetT, _DataT],
32+
]
2633

2734

2835
@overload
29-
def processor(__function: TargetFunction[_DataT, _TargetP, _TargetT]) -> NoParamReturn[_DataT, _TargetP, _TargetT]: ...
36+
def processor(
37+
__function: TargetFunction[_DataT, _TargetP, _TargetT],
38+
) -> NoParamReturn[_DataT, _TargetP, _TargetT]:
39+
...
40+
3041

3142
@overload
3243
def processor(
@@ -35,8 +46,10 @@ def processor(
3546
kwargs: Mapping[str, Data_In] = {},
3647
ignore_errors: Sequence[type[Exception]] = (),
3748
suppress_errors: bool = False,
38-
**overflow_kwargs: Overflow_In
39-
) -> WithParamReturn[_DataT, _TargetP, _TargetT]: ...
49+
**overflow_kwargs: Overflow_In,
50+
) -> WithParamReturn[_DataT, _TargetP, _TargetT]:
51+
...
52+
4053

4154
@overload
4255
def processor(
@@ -46,8 +59,9 @@ def processor(
4659
kwargs: Mapping[str, Data_In] = {},
4760
ignore_errors: Sequence[type[Exception]] = (),
4861
suppress_errors: bool = False,
49-
**overflow_kwargs: Overflow_In
50-
) -> FullParamReturn[_DataT, _TargetP, _TargetT]: ...
62+
**overflow_kwargs: Overflow_In,
63+
) -> FullParamReturn[_DataT, _TargetP, _TargetT]:
64+
...
5165

5266

5367
def processor(
@@ -57,8 +71,12 @@ def processor(
5771
kwargs: Mapping[str, Data_In] = {},
5872
ignore_errors: Sequence[type[Exception]] = (),
5973
suppress_errors: bool = False,
60-
**overflow_kwargs: Overflow_In
61-
) -> Union[NoParamReturn[_DataT, _TargetP, _TargetT], WithParamReturn[_DataT, _TargetP, _TargetT], FullParamReturn[_DataT, _TargetP, _TargetT]]:
74+
**overflow_kwargs: Overflow_In,
75+
) -> Union[
76+
NoParamReturn[_DataT, _TargetP, _TargetT],
77+
WithParamReturn[_DataT, _TargetP, _TargetT],
78+
FullParamReturn[_DataT, _TargetP, _TargetT],
79+
]:
6280
"""
6381
Decorate a function to run it in a thread
6482
@@ -88,7 +106,8 @@ def processor(
88106
89107
You can also pass keyword arguments to change the thread behaviour, it otherwise follows the defaults of `thread.Thread`
90108
>>> @thread.threaded(daemon = True)
91-
>>> def myfunction(): ...
109+
>>> def myfunction():
110+
... ...
92111
93112
Args will be ordered infront of function-parsed args parsed into `thread.Thread.args`
94113
>>> @thread.threaded(args = (1))
@@ -100,39 +119,46 @@ def processor(
100119
"""
101120

102121
if not callable(__function):
103-
def wrapper(func: TargetFunction[_DataT, _TargetP, _TargetT]) -> FullParamReturn[_DataT, _TargetP, _TargetT]:
122+
123+
def wrapper(
124+
func: TargetFunction[_DataT, _TargetP, _TargetT],
125+
) -> FullParamReturn[_DataT, _TargetP, _TargetT]:
104126
return processor(
105127
func,
106-
args = args,
107-
kwargs = kwargs,
108-
ignore_errors = ignore_errors,
109-
suppress_errors = suppress_errors,
110-
**overflow_kwargs
128+
args=args,
129+
kwargs=kwargs,
130+
ignore_errors=ignore_errors,
131+
suppress_errors=suppress_errors,
132+
**overflow_kwargs,
111133
)
134+
112135
return wrapper
113136

114-
overflow_kwargs.update({
115-
'ignore_errors': ignore_errors,
116-
'suppress_errors': suppress_errors
117-
})
137+
overflow_kwargs.update(
138+
{'ignore_errors': ignore_errors, 'suppress_errors': suppress_errors}
139+
)
118140

119141
kwargs = dict(kwargs)
120-
142+
121143
@wraps(__function)
122-
def wrapped(data: Sequence[_DataT], *parsed_args: _TargetP.args, **parsed_kwargs: _TargetP.kwargs) -> ParallelProcessing[_TargetP, _TargetT, _DataT]:
144+
def wrapped(
145+
data: Sequence[_DataT],
146+
*parsed_args: _TargetP.args,
147+
**parsed_kwargs: _TargetP.kwargs,
148+
) -> ParallelProcessing[_TargetP, _TargetT, _DataT]:
123149
kwargs.update(parsed_kwargs)
124150

125-
processed_args = ( *args, *parsed_args )
126-
processed_kwargs = { i:v for i,v in kwargs.items() if i not in ['args', 'kwargs'] }
151+
processed_args = (*args, *parsed_args)
152+
processed_kwargs = {i: v for i, v in kwargs.items() if i not in ['args', 'kwargs']}
127153

128154
job = ParallelProcessing(
129-
function = __function,
130-
dataset = data,
131-
args = processed_args,
132-
kwargs = processed_kwargs,
133-
**overflow_kwargs
155+
function=__function,
156+
dataset=data,
157+
args=processed_args,
158+
kwargs=processed_kwargs,
159+
**overflow_kwargs,
134160
)
135161
job.start()
136162
return job
137-
163+
138164
return wrapped

src/thread/decorators/_threaded.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
FullParamReturn = Callable[P, Thread[P, T]]
2323

2424

25-
26-
2725
@overload
28-
def threaded(__function: TargetFunction[P, T]) -> NoParamReturn[P, T]: ...
26+
def threaded(__function: TargetFunction[P, T]) -> NoParamReturn[P, T]:
27+
...
28+
2929

3030
@overload
3131
def threaded(
@@ -34,8 +34,10 @@ def threaded(
3434
kwargs: Mapping[str, Data_In] = {},
3535
ignore_errors: Sequence[type[Exception]] = (),
3636
suppress_errors: bool = False,
37-
**overflow_kwargs: Overflow_In
38-
) -> WithParamReturn[P, T]: ...
37+
**overflow_kwargs: Overflow_In,
38+
) -> WithParamReturn[P, T]:
39+
...
40+
3941

4042
@overload
4143
def threaded(
@@ -45,8 +47,9 @@ def threaded(
4547
kwargs: Mapping[str, Data_In] = {},
4648
ignore_errors: Sequence[type[Exception]] = (),
4749
suppress_errors: bool = False,
48-
**overflow_kwargs: Overflow_In
49-
) -> FullParamReturn[P, T]: ...
50+
**overflow_kwargs: Overflow_In,
51+
) -> FullParamReturn[P, T]:
52+
...
5053

5154

5255
def threaded(
@@ -56,8 +59,8 @@ def threaded(
5659
kwargs: Mapping[str, Data_In] = {},
5760
ignore_errors: Sequence[type[Exception]] = (),
5861
suppress_errors: bool = False,
59-
**overflow_kwargs: Overflow_In
60-
) -> Union[NoParamReturn[P, T], WithParamReturn[P, T], FullParamReturn[P, T]]:
62+
**overflow_kwargs: Overflow_In,
63+
) -> Union[NoParamReturn[P, T], WithParamReturn[P, T], FullParamReturn[P, T]]:
6164
"""
6265
Decorate a function to run it in a thread
6366
@@ -87,7 +90,8 @@ def threaded(
8790
8891
You can also pass keyword arguments to change the thread behaviour, it otherwise follows the defaults of `thread.Thread`
8992
>>> @thread.threaded(daemon = True)
90-
>>> def myfunction(): ...
93+
>>> def myfunction():
94+
... ...
9195
9296
Args will be ordered infront of function-parsed args parsed into `thread.Thread.args`
9397
>>> @thread.threaded(args = (1))
@@ -99,38 +103,38 @@ def threaded(
99103
"""
100104

101105
if not callable(__function):
106+
102107
def wrapper(func: TargetFunction[P, T]) -> FullParamReturn[P, T]:
103108
return threaded(
104109
func,
105-
args = args,
106-
kwargs = kwargs,
107-
ignore_errors = ignore_errors,
108-
suppress_errors = suppress_errors,
109-
**overflow_kwargs
110+
args=args,
111+
kwargs=kwargs,
112+
ignore_errors=ignore_errors,
113+
suppress_errors=suppress_errors,
114+
**overflow_kwargs,
110115
)
116+
111117
return wrapper
112118

113-
overflow_kwargs.update({
114-
'ignore_errors': ignore_errors,
115-
'suppress_errors': suppress_errors
116-
})
119+
overflow_kwargs.update(
120+
{'ignore_errors': ignore_errors, 'suppress_errors': suppress_errors}
121+
)
117122

118123
kwargs = dict(kwargs)
119-
124+
120125
@wraps(__function)
121126
def wrapped(*parsed_args: P.args, **parsed_kwargs: P.kwargs) -> Thread[P, T]:
122127
kwargs.update(parsed_kwargs)
123128

124-
processed_args = ( *args, *parsed_args )
125-
processed_kwargs = { i:v for i,v in parsed_kwargs.items() if i not in ['args', 'kwargs'] }
129+
processed_args = (*args, *parsed_args)
130+
processed_kwargs = {
131+
i: v for i, v in parsed_kwargs.items() if i not in ['args', 'kwargs']
132+
}
126133

127134
job = Thread(
128-
target = __function,
129-
args = processed_args,
130-
kwargs = processed_kwargs,
131-
**overflow_kwargs
135+
target=__function, args=processed_args, kwargs=processed_kwargs, **overflow_kwargs
132136
)
133137
job.start()
134138
return job
135-
139+
136140
return wrapped

0 commit comments

Comments
 (0)