Skip to content

Commit d05dde1

Browse files
committed
Ruff format
1 parent b3d4694 commit d05dde1

File tree

7 files changed

+29
-27
lines changed

7 files changed

+29
-27
lines changed

_prepare_dev_data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
Script for building test DAWGs.
44
"""
5+
56
import struct
67

78
import dawg
@@ -40,7 +41,6 @@ def create_int_completion_dawg():
4041

4142

4243
def build_test_data() -> None:
43-
4444
dawg.CompletionDAWG(["f", "bar", "foo", "foobar"]).save("dev_data/small/completion.dawg")
4545
dawg.CompletionDAWG([]).save("dev_data/small/completion-empty.dawg")
4646

bench/speed.py

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def load_int_dawg():
6969

7070

7171
def benchmark() -> None:
72-
7372
tests = [
7473
("__getitem__ (hits)", "for word in WORDS100k: data[word]", "M ops/sec", 0.1, 3),
7574
("get() (hits)", "for word in WORDS100k: data.get(word)", "M ops/sec", 0.1, 3),

dawg_python/dawgs.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class DAWG:
2020
"""
2121
Base DAWG wrapper.
2222
"""
23+
2324
dct: wrapper.Dictionary | None
2425

2526
def __init__(self) -> None:
@@ -41,7 +42,6 @@ def _has_value(self, index: int) -> bool:
4142
return self.dct.has_value(index)
4243

4344
def _similar_keys(self, current_prefix: str, key: str, index: int, replace_chars: CompiledReplaces) -> list[str]:
44-
4545
res = []
4646
start_pos = len(current_prefix)
4747
end_pos = len(key)
@@ -51,7 +51,7 @@ def _similar_keys(self, current_prefix: str, key: str, index: int, replace_chars
5151
b_step = key[word_pos].encode("utf8")
5252

5353
if b_step in replace_chars:
54-
for (b_replace_char, u_replace_char) in replace_chars[b_step]:
54+
for b_replace_char, u_replace_char in replace_chars[b_step]:
5555
next_index = index
5656

5757
next_index = self.dct.follow_bytes(b_replace_char, next_index)
@@ -89,22 +89,18 @@ def similar_keys(self, key: str, replaces: CompiledReplaces):
8989

9090
@classmethod
9191
def compile_replaces(cls, replaces: Replaces) -> CompiledReplaces:
92-
93-
for k,v in replaces.items():
92+
for k, v in replaces.items():
9493
if len(k) != 1:
9594
msg = "Keys must be single-char unicode strings."
9695
raise ValueError(msg)
97-
if (isinstance(v, str) and len(v) != 1):
96+
if isinstance(v, str) and len(v) != 1:
9897
msg = "Values must be single-char unicode strings or non-empty lists of such."
9998
raise ValueError(msg)
10099
if isinstance(v, list) and (any(len(v_entry) != 1 for v_entry in v) or len(v) < 1):
101100
msg = "Values must be single-char unicode strings or non-empty lists of such."
102101
raise ValueError(msg)
103102

104-
return {
105-
k.encode("utf8"): [(v_entry.encode("utf8"), v_entry) for v_entry in v]
106-
for k, v in replaces.items()
107-
}
103+
return {k.encode("utf8"): [(v_entry.encode("utf8"), v_entry) for v_entry in v] for k, v in replaces.items()}
108104

109105
def prefixes(self, key: str | bytes) -> list[str]:
110106
"""
@@ -133,6 +129,7 @@ class CompletionDAWG(DAWG):
133129
"""
134130
DAWG with key completion support.
135131
"""
132+
136133
dct: wrapper.Dictionary
137134
guide: wrapper.Guide | None
138135

@@ -331,8 +328,13 @@ def iteritems(self, prefix: str | bytes = "") -> Generator[tuple[str, bytes], No
331328
def _has_value(self, index: int) -> int | None:
332329
return self.dct.follow_bytes(PAYLOAD_SEPARATOR, index)
333330

334-
def _similar_items(self, current_prefix: str, key: str, index: int, replace_chars: CompiledReplaces) -> list[tuple[str, bytes]]:
335-
331+
def _similar_items(
332+
self,
333+
current_prefix: str,
334+
key: str,
335+
index: int,
336+
replace_chars: CompiledReplaces,
337+
) -> list[tuple[str, bytes]]:
336338
res = []
337339
start_pos = len(current_prefix)
338340
end_pos = len(key)
@@ -342,7 +344,7 @@ def _similar_items(self, current_prefix: str, key: str, index: int, replace_char
342344
b_step = key[word_pos].encode("utf8")
343345

344346
if b_step in replace_chars:
345-
for (b_replace_char, u_replace_char) in replace_chars[b_step]:
347+
for b_replace_char, u_replace_char in replace_chars[b_step]:
346348
next_index = index
347349

348350
next_index = self.dct.follow_bytes(b_replace_char, next_index)
@@ -378,7 +380,13 @@ def similar_items(self, key: str, replaces: CompiledReplaces) -> list[tuple[str,
378380
"""
379381
return self._similar_items("", key, self.dct.ROOT, replaces)
380382

381-
def _similar_item_values(self, start_pos: int, key: str, index: int, replace_chars: CompiledReplaces) -> list[bytes]:
383+
def _similar_item_values(
384+
self,
385+
start_pos: int,
386+
key: str,
387+
index: int,
388+
replace_chars: CompiledReplaces,
389+
) -> list[bytes]:
382390
res = []
383391
end_pos = len(key)
384392
word_pos = start_pos
@@ -387,7 +395,7 @@ def _similar_item_values(self, start_pos: int, key: str, index: int, replace_cha
387395
b_step = key[word_pos].encode("utf8")
388396

389397
if b_step in replace_chars:
390-
for (b_replace_char, _u_replace_char) in replace_chars[b_step]:
398+
for b_replace_char, _u_replace_char in replace_chars[b_step]:
391399
next_index = index
392400

393401
next_index = self.dct.follow_bytes(b_replace_char, next_index)

dawg_python/units.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Unit of a dictionary
33
"""
4+
45
PRECISION_MASK = 0xFFFFFFFF
56

67
OFFSET_MAX = 1 << 21

dawg_python/wrapper.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def load(cls, path) -> Self:
8282

8383

8484
class Guide:
85-
8685
ROOT = 0
8786

8887
def __init__(self) -> None:
@@ -106,14 +105,14 @@ class Completer:
106105
_dic: Dictionary | None
107106
_guide: Guide | None
108107

109-
def __init__(self, dic: Dictionary | None =None, guide: Guide | None=None) -> None:
108+
def __init__(self, dic: Dictionary | None = None, guide: Guide | None = None) -> None:
110109
self._dic = dic
111110
self._guide = guide
112111

113112
def value(self) -> int:
114113
return self._dic.value(self._last_index)
115114

116-
def start(self, index: int, prefix: bytes=b"") -> None:
115+
def start(self, index: int, prefix: bytes = b"") -> None:
117116
self.key = bytearray(prefix)
118117

119118
if self._guide.size():
@@ -131,7 +130,6 @@ def next(self) -> bool:
131130
index = self._index_stack[-1]
132131

133132
if self._last_index != self._dic.ROOT:
134-
135133
child_label = self._guide.child(index) # UCharType
136134

137135
if child_label:

tests/test_payload_dawg.py

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
class TestBytesDAWG:
9-
109
DATA = (
1110
("foo", b"data1"),
1211
("bar", b"data2"),
@@ -82,7 +81,6 @@ def test_prefixes(self):
8281

8382

8483
class TestRecordDAWG:
85-
8684
STRUCTURED_DATA = (
8785
("foo", (3, 2, 256)),
8886
("bar", (3, 1, 0)),

tests/test_prediction.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def encode(w):
1313

1414

1515
class TestPrediction:
16-
1716
REPLACES = dawg_python.DAWG.compile_replaces({"Е": "Ё"})
1817

1918
DATA = ["ЁЖИК", "ЁЖИКЕ", "ЁЖ", "ДЕРЕВНЯ", "ДЕРЁВНЯ", "ЕМ", "ОЗЕРА", "ОЗЁРА", "ОЗЕРО"]
@@ -73,7 +72,6 @@ def test_record_dawg_items_values(self, word, prediction):
7372

7473

7574
class TestMultiValuedPrediction:
76-
7775
REPLACES = dawg_python.DAWG.compile_replaces({"е": ["ё", "ѣ"], "и": "і"})
7876

7977
DATA = "хлѣб ёлка ель лѣс лѣсное всё всѣ бѣлёная изобрѣтён лев лёв лѣв вѣнскій".split(" ")
@@ -101,9 +99,9 @@ class TestMultiValuedPrediction:
10199

102100
SUITE_ITEMS = [
103101
(
104-
it[0], # key
102+
it[0], # key
105103
[
106-
(w, [encode(w)]) # item, value pair
104+
(w, [encode(w)]) # item, value pair
107105
for w in it[1]
108106
],
109107
)
@@ -112,7 +110,7 @@ class TestMultiValuedPrediction:
112110

113111
SUITE_VALUES = [
114112
(
115-
it[0], # key
113+
it[0], # key
116114
[[encode(w)] for w in it[1]],
117115
)
118116
for it in SUITE

0 commit comments

Comments
 (0)