Skip to content

Commit a40e030

Browse files
committed
chore: cleanup
1 parent 18f08dc commit a40e030

File tree

5 files changed

+22
-41
lines changed

5 files changed

+22
-41
lines changed

prettyqt/core/mimedata.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
from typing import TYPE_CHECKING, Any, Self
44

5+
import anyenv
6+
57
from prettyqt import core
6-
from prettyqt.utils import datatypes, helpers
78

89

910
if TYPE_CHECKING:
1011
from collections.abc import Iterable, Iterator
1112

13+
from prettyqt.utils import datatypes
14+
1215

1316
DB = core.MimeDatabase()
1417

@@ -36,14 +39,15 @@ def set_data(self, mime_type: str, data: str):
3639
self.setData(mime_type, core.QByteArray(data.encode()))
3740

3841
def set_json_data(self, mime_type: str, data: datatypes.JSONType):
39-
self.setData(mime_type, core.QByteArray(helpers.dump_json(data)))
42+
bytes_ = anyenv.dump_json(data).encode()
43+
self.setData(mime_type, core.QByteArray(bytes_))
4044

4145
def get_data(self, mime_type: str) -> str:
4246
return bytes(self.data(mime_type)).decode()
4347

4448
def get_json_data(self, mime_type: str) -> datatypes.JSONType:
4549
data = self.data(mime_type)
46-
return helpers.load_json(bytes(data))
50+
return anyenv.load_json(bytes(data))
4751

4852
def keys(self) -> list[str]:
4953
return self.formats()

prettyqt/itemmodels/proxies/columnorderproxymodel.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
from __future__ import annotations
22

3+
from typing import TYPE_CHECKING
4+
35
from prettyqt import constants, core
46

57

8+
if TYPE_CHECKING:
9+
from collections.abc import Sequence
10+
11+
612
class ColumnOrderProxyModel(core.IdentityProxyModel):
713
"""Proxy model which reorders the columns of the source model.
814
@@ -27,10 +33,10 @@ class ColumnOrderProxyModel(core.IdentityProxyModel):
2733
ID = "column_order"
2834
ICON = "mdi.reorder-vertical"
2935

30-
def __init__(self, order: list[int | str], **kwargs):
31-
self._column_order = order
36+
def __init__(self, order: Sequence[int | str], **kwargs):
37+
self._column_order = list(order)
3238
super().__init__(**kwargs)
33-
self.set_column_order(order)
39+
self.set_column_order(self._column_order)
3440

3541
def get_column_order(self) -> list[int]:
3642
return self._column_order

prettyqt/utils/asyncrunner.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import os
2121
import threading
2222
import time
23-
from typing import Any, ParamSpec, TypeVar, cast
23+
from typing import Any, ParamSpec, Self, TypeVar, cast
2424

2525
from prettyqt import core
2626

@@ -31,14 +31,6 @@
3131
T = TypeVar("T")
3232

3333

34-
def chunked_iter(src: Iterable, size: int):
35-
if not src:
36-
return
37-
src_iter = iter(src)
38-
while cur_chunk := list(itertools.islice(src_iter, size)):
39-
yield cur_chunk
40-
41-
4234
class AsyncRunner:
4335
"""A runner which runs long-lasting functions using a thread pool."""
4436

@@ -59,7 +51,7 @@ def __init__(self, max_threads: int | None = None):
5951
self._signaller = _FutureDoneSignaller()
6052
self._signaller.future_done_signal.connect(self._resume_coroutine)
6153

62-
def __enter__(self: T) -> T:
54+
def __enter__(self) -> Self:
6355
return self
6456

6557
def __exit__(self, *exc_info: object):
@@ -108,7 +100,7 @@ def close(self):
108100

109101
async def run(
110102
self, func: Callable[Params, T], *args: Params.args, **kwargs: Params.kwargs
111-
) -> T:
103+
) -> T | None:
112104
"""Run the given function in a thread.
113105
114106
While it is running, yields
@@ -149,7 +141,7 @@ async def run_parallel( # type:ignore[override]
149141
# that try to submit functions to execute in threads would only be resumed
150142
# much later, causing a noticeable slow down in the application.
151143
batch_size = max(self._max_threads // 2, 1)
152-
for function_batch in chunked_iter(funcs, batch_size):
144+
for function_batch in itertools.batched(funcs, batch_size):
153145
# Submit all functions from the current batch to the thread pool,
154146
# using the _AsyncTask to track the futures and await when they finish.
155147
task = _AsyncTask({self._pool.submit(f) for f in function_batch})

prettyqt/utils/helpers.py

-23
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,6 @@ def add_connections(item):
104104
return items, connections
105105

106106

107-
def dump_json(data: str):
108-
try:
109-
import orjson
110-
111-
opts = orjson.OPT_NAIVE_UTC | orjson.OPT_SERIALIZE_NUMPY
112-
return orjson.dumps(data, option=opts)
113-
except ImportError:
114-
import json
115-
116-
return json.dumps(data).encode()
117-
118-
119-
def load_json(data):
120-
try:
121-
import orjson
122-
123-
return orjson.loads(data)
124-
except ImportError:
125-
import json
126-
127-
return json.loads(data)
128-
129-
130107
def parse_time(time_str: str) -> int:
131108
"""Parse given string and return duration in seconds.
132109

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies = [
3030
"qstylizer",
3131
"typing-extensions",
3232
"sublime-search>=0.1.2",
33+
"anyenv>=0.4.6",
3334
]
3435
# dynamic = ["version"]
3536

@@ -207,6 +208,7 @@ ignore = [
207208
# "PLR2004", # Magic values instead of named consts
208209
"SLF001", # Private member accessed
209210
"TRY003", # Avoid specifying long messages outside the exception class
211+
"TC006",
210212
]
211213

212214
[tool.ruff.lint.flake8-quotes]

0 commit comments

Comments
 (0)