Skip to content

Commit 55e536a

Browse files
committed
ruff
1 parent 49d5a94 commit 55e536a

6 files changed

Lines changed: 41 additions & 29 deletions

File tree

dictdatabase/io_bytes.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from __future__ import annotations
2+
13
import os
24
import zlib
35

46
from . import config, utils
57

68

7-
def read(db_name: str, *, start: int = None, end: int = None) -> bytes:
9+
def read(db_name: str, *, start: int | None = None, end: int | None = None) -> bytes:
810
"""
911
Read the content of a file as bytes. Reading works even when the config
1012
changes, so a compressed ddb file can also be read if compression is
@@ -33,7 +35,8 @@ def read(db_name: str, *, start: int = None, end: int = None) -> bytes:
3335

3436
if json_exists:
3537
if ddb_exists:
36-
raise FileExistsError(f'Inconsistent: "{db_name}" exists as .json and .ddb.' "Please remove one of them.")
38+
msg = f'Inconsistent: "{db_name}" exists as .json and .ddb.Please remove one of them.'
39+
raise FileExistsError(msg)
3740
with open(json_path, "rb") as f:
3841
if start is None and end is None:
3942
return f.read()
@@ -43,7 +46,8 @@ def read(db_name: str, *, start: int = None, end: int = None) -> bytes:
4346
return f.read()
4447
return f.read(end - start)
4548
if not ddb_exists:
46-
raise FileNotFoundError(f'No database file exists for "{db_name}"')
49+
msg = f'No database file exists for "{db_name}"'
50+
raise FileNotFoundError(msg)
4751
with open(ddb_path, "rb") as f:
4852
json_bytes = zlib.decompress(f.read())
4953
if start is None and end is None:
@@ -53,7 +57,7 @@ def read(db_name: str, *, start: int = None, end: int = None) -> bytes:
5357
return json_bytes[start:end]
5458

5559

56-
def write(db_name: str, dump: bytes, *, start: int = None) -> None:
60+
def write(db_name: str, dump: bytes, *, start: int | None = None) -> None:
5761
"""
5862
Write the bytes to the file of the db_path. If the db was compressed but no
5963
compression is enabled, remove the compressed file, and vice versa.
@@ -72,7 +76,8 @@ def write(db_name: str, dump: bytes, *, start: int = None) -> None:
7276
remove_file = None
7377
if config.use_compression:
7478
if start is not None:
75-
raise RuntimeError("Cannot write to compressed file at a specific index")
79+
msg = "Cannot write to compressed file at a specific index"
80+
raise RuntimeError(msg)
7681
write_file = ddb_path
7782
if json_exists:
7883
remove_file = json_path

dictdatabase/locking.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LockFileMeta:
4040
Metadata representation for a lock file.
4141
"""
4242

43-
__slots__ = ("ddb_dir", "name", "id", "time_ns", "stage", "mode", "path")
43+
__slots__ = ("ddb_dir", "id", "mode", "name", "path", "stage", "time_ns")
4444

4545
ddb_dir: str
4646
name: str
@@ -142,7 +142,7 @@ class AbstractLock:
142142
provides a blueprint for derived classes to implement.
143143
"""
144144

145-
__slots__ = ("db_name", "need_lock", "has_lock", "snapshot", "mode", "is_alive" "keep_alive_thread")
145+
__slots__ = ("db_name", "need_lock", "has_lock", "snapshot", "mode", "is_alivekeep_alive_thread")
146146

147147
db_name: str
148148
need_lock: LockFileMeta
@@ -155,7 +155,7 @@ class AbstractLock:
155155
def __init__(self, db_name: str) -> None:
156156
# Normalize db_name to avoid file naming conflicts
157157
self.db_name = db_name.replace("/", "___").replace(".", "____")
158-
time_ns = time.time_ns()
158+
time_ns = str(time.time_ns())
159159
t_id = f"{threading.get_native_id()}" # ID that's unique across processes and threads.
160160
dir = os.path.join(config.storage_directory, ".ddb")
161161

dictdatabase/models.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def dir_where(self) -> bool:
6161
return self.dir and self.where and not self.key
6262

6363

64-
def at(*path, key: str = None, where: Callable[[Any, Any], bool] = None) -> DDBMethodChooser:
64+
def at(*path, key: str | None = None, where: Callable[[Any, Any], bool] | None = None) -> DDBMethodChooser:
6565
"""
6666
Select a file or folder to perform an operation on.
6767
If you want to select a specific key in a file, use the `key` parameter,
@@ -98,8 +98,8 @@ class DDBMethodChooser:
9898
def __init__(
9999
self,
100100
path: tuple,
101-
key: str = None,
102-
where: Callable[[Any, Any], bool] = None,
101+
key: str | None = None,
102+
where: Callable[[Any, Any], bool] | None = None,
103103
) -> None:
104104
# Convert path to a list of strings
105105
pc = []
@@ -146,13 +146,13 @@ def create(self, data: dict | None = None, force_overwrite: bool = False) -> Non
146146
exists, defaults to False (optional).
147147
"""
148148
if self.where is not None or self.key is not None:
149-
raise RuntimeError("DDB.at().create() cannot be used with the where or key parameters")
149+
msg = "DDB.at().create() cannot be used with the where or key parameters"
150+
raise RuntimeError(msg)
150151

151152
# Except if db exists and force_overwrite is False
152153
if not force_overwrite and self.exists():
153-
raise FileExistsError(
154-
f"Database {self.path} already exists in {config.storage_directory}. Pass force_overwrite=True to overwrite."
155-
)
154+
msg = f"Database {self.path} already exists in {config.storage_directory}. Pass force_overwrite=True to overwrite."
155+
raise FileExistsError(msg)
156156
# Write db to file
157157
if data is None:
158158
data = {}
@@ -166,7 +166,7 @@ def delete(self) -> None:
166166
raise RuntimeError("DDB.at().delete() cannot be used with the where or key parameters")
167167
io_safe.delete(self.path)
168168

169-
def read(self, as_type: Type[T] = None) -> dict | T | None:
169+
def read(self, as_type: Type[T] | None = None) -> dict | T | None:
170170
"""
171171
Reads a file or folder depending on previous `.at(...)` selection.
172172
@@ -209,7 +209,7 @@ def type_cast(value):
209209
return type_cast(data)
210210

211211
def session(
212-
self, as_type: Type[T] = None
212+
self, as_type: Type[T] | None = None
213213
) -> SessionFileFull[T] | SessionFileKey[T] | SessionFileWhere[T] | SessionDirFull[T] | SessionDirWhere[T]:
214214
"""
215215
Opens a session to the selected file(s) or folder, depending on previous
@@ -228,6 +228,7 @@ def session(
228228
Returns:
229229
- Tuple of (session_object, data)
230230
"""
231+
231232
if self.op_type.file_normal:
232233
return SessionFileFull(self.path, as_type)
233234
if self.op_type.file_key:
@@ -238,3 +239,6 @@ def session(
238239
return SessionDirFull(self.path, as_type)
239240
if self.op_type.dir_where:
240241
return SessionDirWhere(self.path, self.where, as_type)
242+
243+
msg = "Invalid operation type"
244+
raise RuntimeError(msg)

tests/benchmark/locking.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
# 25.11.22: 4156ms
1616
with profiler.Profiler() as p:
1717
for _ in range(25_000):
18-
l = locking.ReadLock("db")
19-
l._lock()
20-
l._unlock()
18+
read_lock = locking.ReadLock("db")
19+
read_lock._lock() # noqa: SLF001
20+
read_lock._unlock() # noqa: SLF001
2121
p.open_in_browser()
2222

2323

2424
# 05.11.22: 4884ms
2525
# 25.11.22: 4159ms
2626
with profiler.Profiler() as p:
2727
for _ in range(25_000):
28-
l = locking.WriteLock("db")
29-
l._lock()
30-
l._unlock()
28+
write_lock = locking.WriteLock("db")
29+
write_lock._lock() # noqa: SLF001
30+
write_lock._unlock() # noqa: SLF001
3131
p.open_in_browser()
3232

3333

34-
l = locking.WriteLock("db/test.some")
35-
l._lock()
34+
write_lock = locking.WriteLock("db/test.some")
35+
write_lock._lock() # noqa: SLF001
3636

3737

3838
shutil.rmtree(DDB.config.storage_directory)

tests/system_checks/test_monotonic_over_threads.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import queue
22
import threading
33
import time
4+
from typing import Callable
45

56
# Number of threads
67
NUM_THREADS = 64
@@ -19,14 +20,14 @@
1920
timestamps = queue.Queue()
2021

2122

22-
def capture_time(i, clock_func: callable) -> None:
23+
def capture_time(i, clock_func: Callable) -> None:
2324
# Capture time using the given clock function and put it in the queue
2425
for _ in range(1000):
2526
# print(f"Thread {i} capturing time")
2627
timestamps.put(clock_func())
2728

2829

29-
def check_monotonicity_for_clock(clock_name: str, clock_func: callable) -> None:
30+
def check_monotonicity_for_clock(clock_name: str, clock_func: Callable) -> None:
3031
# Clear the queue for the next clock
3132
while not timestamps.empty():
3233
timestamps.get()

tests/system_checks/test_tick_rate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import time
2+
from typing import Callable
23

34

4-
def get_tick_rate(clock_func: callable) -> float:
5+
def get_tick_rate(clock_func: Callable) -> float:
56
start_time = time.time()
67
measurements = [clock_func() for _ in range(2_000_000)]
78
end_time = time.time()
@@ -10,7 +11,8 @@ def get_tick_rate(clock_func: callable) -> float:
1011
prev_value = measurements[0]
1112
for current_value in measurements[1:]:
1213
if current_value < prev_value:
13-
raise RuntimeError("Clock function is not monotonic")
14+
msg = "Clock function is not monotonic"
15+
raise RuntimeError(msg)
1416
if current_value != prev_value:
1517
ticks += 1
1618
prev_value = current_value

0 commit comments

Comments
 (0)