Enable PLR2004: replace magic values with named constants

- Added constants for HTTP status codes (using http.HTTPStatus)
- Added validation limit constants in screen_locker
- Added centipawn loss threshold constants in chess analysis
- Added various other domain-specific constants across 9 files
This commit is contained in:
Krzysztof Rudnicki 2025-11-30 15:01:14 +01:00
parent a5406b71b0
commit 3f793d2d17
10 changed files with 96 additions and 45 deletions

View File

@ -12,8 +12,10 @@ import requests
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
MAX_REQUESTS = 90
requests_send = 0 requests_send = 0
while requests_send < 90: while requests_send < MAX_REQUESTS:
res = requests.get("https://api.thecatapi.com/v1/images/search?limit=100&api_key=") res = requests.get("https://api.thecatapi.com/v1/images/search?limit=100&api_key=")
requests_send += 1 requests_send += 1
response = json.loads(res.text) response = json.loads(res.text)

View File

@ -27,6 +27,7 @@ KEY_SELECTED_COLOR = (150, 150, 200)
KEY_AVAILABLE_COLOR = (100, 150, 100) KEY_AVAILABLE_COLOR = (100, 150, 100)
TEXT_COLOR = (255, 255, 255) TEXT_COLOR = (255, 255, 255)
PLAYER_COLORS = [(255, 100, 100), (100, 100, 255)] PLAYER_COLORS = [(255, 100, 100), (100, 100, 255)]
MIN_WORD_LENGTH = 3
# Keyboard layout # Keyboard layout
KEYBOARD_LAYOUT = [ KEYBOARD_LAYOUT = [
@ -286,7 +287,7 @@ class KeyboardCoopGame:
def calculate_score(self, word_length): def calculate_score(self, word_length):
"""Calculate score exponentially based on word length.""" """Calculate score exponentially based on word length."""
if word_length < 3: if word_length < MIN_WORD_LENGTH:
return 0 return 0
return 2 ** (word_length - 2) return 2 ** (word_length - 2)
@ -317,7 +318,9 @@ class KeyboardCoopGame:
def submit_word(self): def submit_word(self):
"""Submit the current word and check if it's valid.""" """Submit the current word and check if it's valid."""
if len(self.current_word) >= 3 and self.is_valid_word(self.current_word): if len(self.current_word) >= MIN_WORD_LENGTH and self.is_valid_word(
self.current_word
):
points = self.calculate_score(len(self.current_word)) points = self.calculate_score(len(self.current_word))
self.score += points self.score += points
self.message = ( self.message = (
@ -329,8 +332,11 @@ class KeyboardCoopGame:
self.generate_random_keyboard() self.generate_random_keyboard()
self.key_positions = self.calculate_key_positions() self.key_positions = self.calculate_key_positions()
elif len(self.current_word) < 3: elif len(self.current_word) < MIN_WORD_LENGTH:
self.message = f"'{self.current_word}' is too short! (minimum 3 letters)" self.message = (
f"'{self.current_word}' is too short! "
f"(minimum {MIN_WORD_LENGTH} letters)"
)
else: else:
self.message = f"'{self.current_word}' is not a valid word!" self.message = f"'{self.current_word}' is not a valid word!"

View File

@ -2,6 +2,7 @@
from collections.abc import Generator from collections.abc import Generator
import contextlib import contextlib
from http import HTTPStatus
import json import json
import logging import logging
import time import time
@ -45,7 +46,7 @@ class LichessAPI:
raise raise
elapsed = time.monotonic() - t0 elapsed = time.monotonic() - t0
status = r.status_code status = r.status_code
if status >= 400: if status >= HTTPStatus.BAD_REQUEST:
# Log a brief error body snippet if available # Log a brief error body snippet if available
snippet = None snippet = None
try: try:
@ -88,7 +89,7 @@ class LichessAPI:
logging.debug(f"Skipping non-JSON line: {line}") logging.debug(f"Skipping non-JSON line: {line}")
except requests.HTTPError as e: except requests.HTTPError as e:
status = getattr(e.response, "status_code", None) status = getattr(e.response, "status_code", None)
if status == 429: if status == HTTPStatus.TOO_MANY_REQUESTS:
logging.warning("Event stream hit 429; backing off") logging.warning("Event stream hit 429; backing off")
time.sleep(backoff) time.sleep(backoff)
backoff = min(8.0, backoff * 2) backoff = min(8.0, backoff * 2)
@ -160,11 +161,11 @@ class LichessAPI:
"""Submit a move to an active game.""" """Submit a move to an active game."""
url = f"{LICHESS_API}/api/board/game/{game_id}/move/{move.uci()}" url = f"{LICHESS_API}/api/board/game/{game_id}/move/{move.uci()}"
r = self._request("POST", url, timeout=30) r = self._request("POST", url, timeout=30)
if r.status_code in (400, 409): if r.status_code in (HTTPStatus.BAD_REQUEST, HTTPStatus.CONFLICT):
# Likely not our turn or move already played; do not retry to avoid spam # Likely not our turn or move already played; do not retry to avoid spam
r.raise_for_status() r.raise_for_status()
return return
if r.status_code == 429: if r.status_code == HTTPStatus.TOO_MANY_REQUESTS:
logging.warning(f"HTTP POST {url} -> 429; retrying once after 0.5s") logging.warning(f"HTTP POST {url} -> 429; retrying once after 0.5s")
time.sleep(0.5) time.sleep(0.5)
r = self._request("POST", url, timeout=30) r = self._request("POST", url, timeout=30)
@ -178,6 +179,6 @@ class LichessAPI:
"""Fetch the authenticated user's ID.""" """Fetch the authenticated user's ID."""
url = f"{LICHESS_API}/api/account" url = f"{LICHESS_API}/api/account"
r = self._request("GET", url, timeout=30) r = self._request("GET", url, timeout=30)
if r.status_code == 200: if r.status_code == HTTPStatus.OK:
return r.json().get("id") return r.json().get("id")
return None return None

View File

@ -45,6 +45,10 @@ import chess.pgn
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
# Expected columns in the log file:
# ply, side, move, played_eval, best_eval, loss, class, best_suggestion
EXPECTED_COLUMNS = 8
@dataclass @dataclass
class Blunder: class Blunder:
@ -79,7 +83,7 @@ def parse_columns_for_blunders(text: str) -> list[Blunder]:
parts = re.split(r"\s{2,}", ln.strip()) parts = re.split(r"\s{2,}", ln.strip())
# Expected columns: # Expected columns:
# ply, side, move, played_eval, best_eval, loss, class, best_suggestion # ply, side, move, played_eval, best_eval, loss, class, best_suggestion
if len(parts) < 8: if len(parts) < EXPECTED_COLUMNS:
continue continue
try: try:
ply = int(parts[0]) ply = int(parts[0])

View File

@ -10,6 +10,8 @@ from PIL import Image
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
MAX_IMAGE_SIZE = 1000
def generate_bloated_jpeg( def generate_bloated_jpeg(
size, color_list, block_size, output_path, quality, image_index, folder size, color_list, block_size, output_path, quality, image_index, folder
@ -26,9 +28,11 @@ def generate_bloated_jpeg(
image_index (int): Index of the image for unique naming. image_index (int): Index of the image for unique naming.
folder (str): Folder to save the image. folder (str): Folder to save the image.
""" """
# Ensure size is divisible by block_size and does not exceed 1000 pixels # Ensure size is divisible by block_size and does not exceed MAX_IMAGE_SIZE
if size > 1000 or size % block_size != 0: if size > MAX_IMAGE_SIZE or size % block_size != 0:
msg = "Size must be 1000 pixels or less and divisible by block_size" msg = (
f"Size must be {MAX_IMAGE_SIZE} pixels or less and divisible by block_size"
)
raise ValueError(msg) raise ValueError(msg)
# Create a new image # Create a new image

View File

@ -8,8 +8,15 @@ import sys
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
DEFAULT_MIN_PERCENTAGE = 1
DEFAULT_MAX_PERCENTAGE = 20
def randomize_numbers(numbers, min_percentage=1, max_percentage=20):
def randomize_numbers(
numbers,
min_percentage=DEFAULT_MIN_PERCENTAGE,
max_percentage=DEFAULT_MAX_PERCENTAGE,
):
"""Apply random percentage variation to a list of numbers.""" """Apply random percentage variation to a list of numbers."""
randomized_numbers = [] randomized_numbers = []
for number in numbers: for number in numbers:
@ -43,8 +50,10 @@ def parse_input(input_string):
return numbers, decimal_counts return numbers, decimal_counts
MIN_ARGS = 2
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) < 2: if len(sys.argv) < MIN_ARGS:
logging.info( logging.info(
"Usage: python random_digits.py <number1> <number2> ... " "Usage: python random_digits.py <number1> <number2> ... "
"[min_percentage max_percentage]" "[min_percentage max_percentage]"
@ -54,8 +63,8 @@ if __name__ == "__main__":
try: try:
input_string = " ".join(sys.argv[1:]) input_string = " ".join(sys.argv[1:])
numbers, decimal_counts = parse_input(input_string) numbers, decimal_counts = parse_input(input_string)
min_percentage = 1 min_percentage = DEFAULT_MIN_PERCENTAGE
max_percentage = 20 max_percentage = DEFAULT_MAX_PERCENTAGE
if len(numbers) == 0: if len(numbers) == 0:
msg = "No valid numbers provided." msg = "No valid numbers provided."

View File

@ -13,6 +13,15 @@ import tkinter as tk
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
# Validation limits for workout data
MAX_DISTANCE_KM = 100
MAX_TIME_MINUTES = 600
MAX_PACE_MIN_PER_KM = 20
MIN_EXERCISE_NAME_LEN = 3
MAX_SETS = 20
MAX_REPS = 100
MAX_WEIGHT_KG = 500
class ScreenLocker: class ScreenLocker:
"""Screen locker that requires workout logging to unlock.""" """Screen locker that requires workout logging to unlock."""
@ -294,16 +303,20 @@ class ScreenLocker:
pace = float(self.pace_entry.get()) pace = float(self.pace_entry.get())
# Sanity checks # Sanity checks
if distance <= 0 or distance > 100: if distance <= 0 or distance > MAX_DISTANCE_KM:
self.show_error("Distance seems unrealistic (0-100 km)") self.show_error(f"Distance seems unrealistic (0-{MAX_DISTANCE_KM} km)")
return return
if time_mins <= 0 or time_mins > 600: if time_mins <= 0 or time_mins > MAX_TIME_MINUTES:
self.show_error("Time seems unrealistic (0-600 minutes)") self.show_error(
f"Time seems unrealistic (0-{MAX_TIME_MINUTES} minutes)"
)
return return
if pace <= 0 or pace > 20: if pace <= 0 or pace > MAX_PACE_MIN_PER_KM:
self.show_error("Pace seems unrealistic (0-20 min/km)") self.show_error(
f"Pace seems unrealistic (0-{MAX_PACE_MIN_PER_KM} min/km)"
)
return return
# Calculate expected pace and check if close enough # Calculate expected pace and check if close enough
@ -463,21 +476,21 @@ class ScreenLocker:
return return
# Check for empty or lazy entries # Check for empty or lazy entries
if any(len(ex) < 3 for ex in exercises): if any(len(ex) < MIN_EXERCISE_NAME_LEN for ex in exercises):
self.show_error("Exercise names too short - be specific") self.show_error("Exercise names too short - be specific")
return return
# Sanity checks # Sanity checks
if any(s < 1 or s > 20 for s in sets): if any(s < 1 or s > MAX_SETS for s in sets):
self.show_error("Sets should be between 1-20") self.show_error(f"Sets should be between 1-{MAX_SETS}")
return return
if any(r < 1 or r > 100 for r in reps): if any(r < 1 or r > MAX_REPS for r in reps):
self.show_error("Reps should be between 1-100") self.show_error(f"Reps should be between 1-{MAX_REPS}")
return return
if any(w < 0 or w > 500 for w in weights): if any(w < 0 or w > MAX_WEIGHT_KG for w in weights):
self.show_error("Weights should be between 0-500 kg") self.show_error(f"Weights should be between 0-{MAX_WEIGHT_KG} kg")
return return
# Calculate expected total weight # Calculate expected total weight

View File

@ -44,6 +44,10 @@ except Exception: # pragma: no cover
logging.exception(" pip install -r PYTHON/stockfish_analysis/requirements.txt") logging.exception(" pip install -r PYTHON/stockfish_analysis/requirements.txt")
raise raise
# Memory configuration constants
MEMINFO_PARTS_MIN = 2
HIGH_THREAD_COUNT = 16
def extract_pgn_text(raw: str) -> str | None: def extract_pgn_text(raw: str) -> str | None:
"""Try to extract a PGN block from a possibly noisy file. """Try to extract a PGN block from a possibly noisy file.
@ -97,6 +101,14 @@ def score_to_cp(
return s.score(mate_score=None), None return s.score(mate_score=None), None
# Centipawn loss thresholds for move quality classification (Lichess-like bands)
CP_LOSS_BEST = 10
CP_LOSS_EXCELLENT = 20
CP_LOSS_GOOD = 50
CP_LOSS_INACCURACY = 99
CP_LOSS_MISTAKE = 299
def classify_cp_loss(cp_loss: int | None) -> str: def classify_cp_loss(cp_loss: int | None) -> str:
"""Classify move quality using Lichess-like centipawn loss bands. """Classify move quality using Lichess-like centipawn loss bands.
@ -111,15 +123,15 @@ def classify_cp_loss(cp_loss: int | None) -> str:
""" """
if cp_loss is None: if cp_loss is None:
return "Unknown" return "Unknown"
if cp_loss <= 10: if cp_loss <= CP_LOSS_BEST:
return "Best" return "Best"
if cp_loss <= 20: if cp_loss <= CP_LOSS_EXCELLENT:
return "Excellent" return "Excellent"
if cp_loss <= 50: if cp_loss <= CP_LOSS_GOOD:
return "Good" return "Good"
if cp_loss <= 99: if cp_loss <= CP_LOSS_INACCURACY:
return "Inaccuracy" return "Inaccuracy"
if cp_loss <= 299: if cp_loss <= CP_LOSS_MISTAKE:
return "Mistake" return "Mistake"
return "Blunder" return "Blunder"
@ -172,7 +184,7 @@ def _detect_total_mem_mb() -> int | None:
for line in f: for line in f:
if line.startswith("MemTotal:"): if line.startswith("MemTotal:"):
parts = line.split() parts = line.split()
if len(parts) >= 2 and parts[1].isdigit(): if len(parts) >= MEMINFO_PARTS_MIN and parts[1].isdigit():
# Value is in kB # Value is in kB
kb = int(parts[1]) kb = int(parts[1])
return kb // 1024 return kb // 1024
@ -196,7 +208,7 @@ def _auto_hash_mb(threads_wanted: int, engine_options) -> int:
if isinstance(max_allowed, int): if isinstance(max_allowed, int):
target = min(target, max_allowed) target = min(target, max_allowed)
# Some rough scaling: if very many threads, give a bit more (but not huge) # Some rough scaling: if very many threads, give a bit more (but not huge)
if threads_wanted >= 16: if threads_wanted >= HIGH_THREAD_COUNT:
target = min(target + 1024, (total_mb * 3) // 4) target = min(target + 1024, (total_mb * 3) // 4)
return max(64, int(target)) return max(64, int(target))

View File

@ -1,5 +1,6 @@
"""Integration tests for the articles C server API.""" """Integration tests for the articles C server API."""
from http import HTTPStatus
import json import json
import os import os
from pathlib import Path from pathlib import Path
@ -62,19 +63,19 @@ def test_crud_roundtrip(tmp_path):
"thumb": "data:image/png;base64,xyz", "thumb": "data:image/png;base64,xyz",
}, },
) )
assert code == 201 assert code == HTTPStatus.CREATED
created = json.loads(body) created = json.loads(body)
art_id = created["id"] art_id = created["id"]
# List # List
code, body = _req(base + "/api/articles") code, body = _req(base + "/api/articles")
assert code == 200 assert code == HTTPStatus.OK
items = json.loads(body) items = json.loads(body)
assert any(a["id"] == art_id for a in items) assert any(a["id"] == art_id for a in items)
# Get one # Get one
code, body = _req(base + f"/api/articles/{art_id}") code, body = _req(base + f"/api/articles/{art_id}")
assert code == 200 assert code == HTTPStatus.OK
got = json.loads(body) got = json.loads(body)
assert got["title"] == "T1" assert got["title"] == "T1"
@ -82,13 +83,13 @@ def test_crud_roundtrip(tmp_path):
code, body = _req( code, body = _req(
base + f"/api/articles/{art_id}", method="PUT", data={"title": "T2"} base + f"/api/articles/{art_id}", method="PUT", data={"title": "T2"}
) )
assert code == 200 assert code == HTTPStatus.OK
updated = json.loads(body) updated = json.loads(body)
assert updated["title"] == "T2" assert updated["title"] == "T2"
# Delete # Delete
code, _ = _req(base + f"/api/articles/{art_id}", method="DELETE") code, _ = _req(base + f"/api/articles/{art_id}", method="DELETE")
assert code == 204 assert code == HTTPStatus.NO_CONTENT
# Ensure gone # Ensure gone
try: try:
@ -96,7 +97,7 @@ def test_crud_roundtrip(tmp_path):
msg = "Expected 404" msg = "Expected 404"
raise AssertionError(msg) raise AssertionError(msg)
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e:
assert e.code == 404 assert e.code == HTTPStatus.NOT_FOUND
finally: finally:
srv.terminate() srv.terminate()

View File

@ -34,7 +34,6 @@ ignore = [
"COM812", # Trailing comma missing (conflicts with formatter) "COM812", # Trailing comma missing (conflicts with formatter)
"ISC001", # Implicit string concatenation (conflicts with formatter) "ISC001", # Implicit string concatenation (conflicts with formatter)
# Relaxed for script-heavy repository # Relaxed for script-heavy repository
"PLR2004", # Magic value comparison - common in scripts
"S101", # Use of assert - acceptable in this codebase "S101", # Use of assert - acceptable in this codebase
"ANN001", # Missing type annotation for function argument "ANN001", # Missing type annotation for function argument
"ANN002", # Missing type annotation for *args "ANN002", # Missing type annotation for *args