Skip to content

Commit 53e15d2

Browse files
style: Formatting with ruff
Conforming to PEP8
1 parent 3e303c1 commit 53e15d2

File tree

15 files changed

+1048
-1054
lines changed

15 files changed

+1048
-1054
lines changed

src/thread/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838

3939
# Wildcard Export
4040
__all__ = [
41-
'Thread',
42-
'ParallelProcessing',
43-
'threaded',
44-
'processor',
45-
'types',
46-
'exceptions',
47-
'Settings',
48-
'__version__',
41+
'Thread',
42+
'ParallelProcessing',
43+
'threaded',
44+
'processor',
45+
'types',
46+
'exceptions',
47+
'Settings',
48+
'__version__',
4949
]

src/thread/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
from .cli import app
33

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

src/thread/_types.py

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

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

2828

src/thread/cli.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
try:
2-
import importlib
2+
import importlib
33

4-
thread_cli = importlib.import_module('thread-cli')
5-
app = thread_cli.app
4+
thread_cli = importlib.import_module('thread-cli')
5+
app = thread_cli.app
66
except ModuleNotFoundError:
77

8-
def app(prog_name='thread'):
9-
print('thread-cli not found, please install it with `pip install thread-cli`')
10-
exit(1)
8+
def app(prog_name='thread'):
9+
print('thread-cli not found, please install it with `pip install thread-cli`')
10+
exit(1)

src/thread/decorators/_processor.py

Lines changed: 122 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -19,146 +19,148 @@
1919

2020

2121
NoParamReturn = Callable[
22-
Concatenate[Sequence[_DataT], _TargetP],
23-
ParallelProcessing[_TargetP, _TargetT, _DataT],
22+
Concatenate[Sequence[_DataT], _TargetP],
23+
ParallelProcessing[_TargetP, _TargetT, _DataT],
2424
]
2525
WithParamReturn = Callable[
26-
[TargetFunction[_DataT, _TargetP, _TargetT]],
27-
NoParamReturn[_DataT, _TargetP, _TargetT],
26+
[TargetFunction[_DataT, _TargetP, _TargetT]],
27+
NoParamReturn[_DataT, _TargetP, _TargetT],
2828
]
2929
FullParamReturn = Callable[
30-
Concatenate[Sequence[_DataT], _TargetP],
31-
ParallelProcessing[_TargetP, _TargetT, _DataT],
30+
Concatenate[Sequence[_DataT], _TargetP],
31+
ParallelProcessing[_TargetP, _TargetT, _DataT],
3232
]
3333

3434

3535
@overload
3636
def processor(
37-
__function: TargetFunction[_DataT, _TargetP, _TargetT],
38-
) -> NoParamReturn[_DataT, _TargetP, _TargetT]:
39-
...
37+
__function: TargetFunction[_DataT, _TargetP, _TargetT],
38+
) -> NoParamReturn[_DataT, _TargetP, _TargetT]:
39+
...
4040

4141

4242
@overload
4343
def processor(
44-
*,
45-
args: Sequence[Data_In] = (),
46-
kwargs: Mapping[str, Data_In] = {},
47-
ignore_errors: Sequence[type[Exception]] = (),
48-
suppress_errors: bool = False,
49-
**overflow_kwargs: Overflow_In,
50-
) -> WithParamReturn[_DataT, _TargetP, _TargetT]:
51-
...
44+
*,
45+
args: Sequence[Data_In] = (),
46+
kwargs: Mapping[str, Data_In] = {},
47+
ignore_errors: Sequence[type[Exception]] = (),
48+
suppress_errors: bool = False,
49+
**overflow_kwargs: Overflow_In,
50+
) -> WithParamReturn[_DataT, _TargetP, _TargetT]:
51+
...
5252

5353

5454
@overload
5555
def processor(
56-
__function: TargetFunction[_DataT, _TargetP, _TargetT],
57-
*,
58-
args: Sequence[Data_In] = (),
59-
kwargs: Mapping[str, Data_In] = {},
60-
ignore_errors: Sequence[type[Exception]] = (),
61-
suppress_errors: bool = False,
62-
**overflow_kwargs: Overflow_In,
63-
) -> FullParamReturn[_DataT, _TargetP, _TargetT]:
64-
...
56+
__function: TargetFunction[_DataT, _TargetP, _TargetT],
57+
*,
58+
args: Sequence[Data_In] = (),
59+
kwargs: Mapping[str, Data_In] = {},
60+
ignore_errors: Sequence[type[Exception]] = (),
61+
suppress_errors: bool = False,
62+
**overflow_kwargs: Overflow_In,
63+
) -> FullParamReturn[_DataT, _TargetP, _TargetT]:
64+
...
6565

6666

6767
def processor(
68-
__function: Optional[TargetFunction[_DataT, _TargetP, _TargetT]] = None,
69-
*,
70-
args: Sequence[Data_In] = (),
71-
kwargs: Mapping[str, Data_In] = {},
72-
ignore_errors: Sequence[type[Exception]] = (),
73-
suppress_errors: bool = False,
74-
**overflow_kwargs: Overflow_In,
75-
) -> Union[
76-
NoParamReturn[_DataT, _TargetP, _TargetT],
77-
WithParamReturn[_DataT, _TargetP, _TargetT],
78-
FullParamReturn[_DataT, _TargetP, _TargetT],
68+
__function: Optional[TargetFunction[_DataT, _TargetP, _TargetT]] = None,
69+
*,
70+
args: Sequence[Data_In] = (),
71+
kwargs: Mapping[str, Data_In] = {},
72+
ignore_errors: Sequence[type[Exception]] = (),
73+
suppress_errors: bool = False,
74+
**overflow_kwargs: Overflow_In,
75+
) -> Union[
76+
NoParamReturn[_DataT, _TargetP, _TargetT],
77+
WithParamReturn[_DataT, _TargetP, _TargetT],
78+
FullParamReturn[_DataT, _TargetP, _TargetT],
7979
]:
80-
"""
81-
Decorate a function to run it in a thread
82-
83-
Parameters
84-
----------
85-
:param __function: The function to run in a thread
86-
:param args: Keyword-Only arguments to pass into `thread.Thread`
87-
:param kwargs: Keyword-Only keyword arguments to pass into `thread.Thread`
88-
:param ignore_errors: Keyword-Only arguments to pass into `thread.Thread`
89-
:param suppress_errors: Keyword-Only arguments to pass into `thread.Thread`
90-
:param **: Keyword-Only arguments to pass into `thread.Thread`
91-
92-
Returns
93-
-------
94-
:return decorator:
95-
96-
Use Case
97-
--------
98-
Now whenever `myfunction` is invoked, it will be executed in a thread and the `Thread` object will be returned
99-
100-
>>> @thread.threaded
101-
>>> def myfunction(*args, **kwargs): ...
102-
103-
>>> myJob = myfunction(1, 2)
104-
>>> type(myjob)
105-
> Thread
106-
107-
You can also pass keyword arguments to change the thread behaviour, it otherwise follows the defaults of `thread.Thread`
108-
>>> @thread.threaded(daemon = True)
109-
>>> def myfunction():
110-
... ...
111-
112-
Args will be ordered infront of function-parsed args parsed into `thread.Thread.args`
113-
>>> @thread.threaded(args = (1))
114-
>>> def myfunction(*args):
115-
>>> print(args)
116-
>>>
117-
>>> myfunction(4, 6).get_return_value()
118-
1, 4, 6
119-
"""
120-
121-
if not callable(__function):
122-
123-
def wrapper(
124-
func: TargetFunction[_DataT, _TargetP, _TargetT],
125-
) -> FullParamReturn[_DataT, _TargetP, _TargetT]:
126-
return processor(
127-
func,
128-
args=args,
129-
kwargs=kwargs,
130-
ignore_errors=ignore_errors,
131-
suppress_errors=suppress_errors,
132-
**overflow_kwargs,
133-
)
134-
135-
return wrapper
136-
137-
overflow_kwargs.update(
138-
{'ignore_errors': ignore_errors, 'suppress_errors': suppress_errors}
139-
)
140-
141-
kwargs = dict(kwargs)
142-
143-
@wraps(__function)
144-
def wrapped(
145-
data: Sequence[_DataT],
146-
*parsed_args: _TargetP.args,
147-
**parsed_kwargs: _TargetP.kwargs,
148-
) -> ParallelProcessing[_TargetP, _TargetT, _DataT]:
149-
kwargs.update(parsed_kwargs)
150-
151-
processed_args = (*args, *parsed_args)
152-
processed_kwargs = {i: v for i, v in kwargs.items() if i not in ['args', 'kwargs']}
153-
154-
job = ParallelProcessing(
155-
function=__function,
156-
dataset=data,
157-
args=processed_args,
158-
kwargs=processed_kwargs,
159-
**overflow_kwargs,
80+
"""
81+
Decorate a function to run it in a thread
82+
83+
Parameters
84+
----------
85+
:param __function: The function to run in a thread
86+
:param args: Keyword-Only arguments to pass into `thread.Thread`
87+
:param kwargs: Keyword-Only keyword arguments to pass into `thread.Thread`
88+
:param ignore_errors: Keyword-Only arguments to pass into `thread.Thread`
89+
:param suppress_errors: Keyword-Only arguments to pass into `thread.Thread`
90+
:param **: Keyword-Only arguments to pass into `thread.Thread`
91+
92+
Returns
93+
-------
94+
:return decorator:
95+
96+
Use Case
97+
--------
98+
Now whenever `myfunction` is invoked, it will be executed in a thread and the `Thread` object will be returned
99+
100+
>>> @thread.threaded
101+
>>> def myfunction(*args, **kwargs): ...
102+
103+
>>> myJob = myfunction(1, 2)
104+
>>> type(myjob)
105+
> Thread
106+
107+
You can also pass keyword arguments to change the thread behaviour, it otherwise follows the defaults of `thread.Thread`
108+
>>> @thread.threaded(daemon = True)
109+
>>> def myfunction():
110+
... ...
111+
112+
Args will be ordered infront of function-parsed args parsed into `thread.Thread.args`
113+
>>> @thread.threaded(args = (1))
114+
>>> def myfunction(*args):
115+
>>> print(args)
116+
>>>
117+
>>> myfunction(4, 6).get_return_value()
118+
1, 4, 6
119+
"""
120+
121+
if not callable(__function):
122+
123+
def wrapper(
124+
func: TargetFunction[_DataT, _TargetP, _TargetT],
125+
) -> FullParamReturn[_DataT, _TargetP, _TargetT]:
126+
return processor(
127+
func,
128+
args=args,
129+
kwargs=kwargs,
130+
ignore_errors=ignore_errors,
131+
suppress_errors=suppress_errors,
132+
**overflow_kwargs,
133+
)
134+
135+
return wrapper
136+
137+
overflow_kwargs.update(
138+
{'ignore_errors': ignore_errors, 'suppress_errors': suppress_errors}
160139
)
161-
job.start()
162-
return job
163140

164-
return wrapped
141+
kwargs = dict(kwargs)
142+
143+
@wraps(__function)
144+
def wrapped(
145+
data: Sequence[_DataT],
146+
*parsed_args: _TargetP.args,
147+
**parsed_kwargs: _TargetP.kwargs,
148+
) -> ParallelProcessing[_TargetP, _TargetT, _DataT]:
149+
kwargs.update(parsed_kwargs)
150+
151+
processed_args = (*args, *parsed_args)
152+
processed_kwargs = {
153+
i: v for i, v in kwargs.items() if i not in ['args', 'kwargs']
154+
}
155+
156+
job = ParallelProcessing(
157+
function=__function,
158+
dataset=data,
159+
args=processed_args,
160+
kwargs=processed_kwargs,
161+
**overflow_kwargs,
162+
)
163+
job.start()
164+
return job
165+
166+
return wrapped

0 commit comments

Comments
 (0)