fix: correct shebang and executable permissions

- Add +x to Python scripts with shebangs (3 files)
- Remove -x from non-script files like .cpp, .txt, makefile (23 files)
- Move shebang to first line in C/imageViewer/lint.sh
This commit is contained in:
Krzysztof Rudnicki 2025-11-30 13:42:16 +01:00
parent 2dc97437dd
commit 0489085c68
120 changed files with 3731 additions and 2110 deletions

View File

@ -2,19 +2,19 @@ name: Python tests
on:
push:
branches: [ main ]
branches: [main]
paths:
- 'PYTHON/lichess_bot/**'
- 'PYTHON/**'
- 'tests/**'
- 'requirements.txt'
- "PYTHON/lichess_bot/**"
- "PYTHON/**"
- "tests/**"
- "requirements.txt"
pull_request:
branches: [ main ]
branches: [main]
paths:
- 'PYTHON/lichess_bot/**'
- 'PYTHON/**'
- 'tests/**'
- 'requirements.txt'
- "PYTHON/lichess_bot/**"
- "PYTHON/**"
- "tests/**"
- "requirements.txt"
jobs:
test:
@ -23,7 +23,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip

268
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,268 @@
# ==============================================================================
# Pre-commit Configuration - AGGRESSIVE Python Linting & Formatting
# ==============================================================================
# Install: pre-commit install
# Run all: pre-commit run --all-files
# Update hooks: pre-commit autoupdate
# ==============================================================================
# Global settings
default_language_version:
python: python3
# Fail fast on first error (set to false to see all errors)
fail_fast: false
# Configuration
ci:
autofix_commit_msg: "style: auto-fix by pre-commit hooks"
autoupdate_commit_msg: "chore: update pre-commit hooks"
repos:
# ===========================================================================
# GENERAL HOOKS - File formatting and validation
# ===========================================================================
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- id: end-of-file-fixer
- id: check-yaml
args: [--unsafe]
- id: check-json
- id: check-toml
- id: check-xml
- id: check-added-large-files
args: [--maxkb=1000]
- id: check-merge-conflict
- id: check-case-conflict
- id: check-symlinks
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- id: detect-private-key
- id: debug-statements
- id: name-tests-test
args: [--pytest-test-first]
- id: check-ast
- id: check-builtin-literals
- id: check-docstring-first
- id: fix-byte-order-marker
- id: mixed-line-ending
args: [--fix=lf]
- id: requirements-txt-fixer
# ===========================================================================
# RUFF - Fast Python linter and formatter (replaces black, isort, flake8, etc.)
# ===========================================================================
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.1
hooks:
# Linter - run first to catch issues
- id: ruff
args:
- --fix
- --exit-non-zero-on-fix
- --show-fixes
types_or: [python, pyi]
# Formatter - run after linting
- id: ruff-format
types_or: [python, pyi]
# ===========================================================================
# MYPY - Static type checking (strict mode)
# ===========================================================================
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.13.0
hooks:
- id: mypy
args:
- --strict
- --ignore-missing-imports
- --show-error-codes
- --no-error-summary
additional_dependencies:
- types-requests
- types-PyYAML
- types-python-dateutil
exclude: ^(Bash/|\.venv/)
# ===========================================================================
# PYLINT - Comprehensive Python linter
# ===========================================================================
- repo: https://github.com/pylint-dev/pylint
rev: v3.3.2
hooks:
- id: pylint
args:
- --rcfile=pyproject.toml
- --fail-under=5.0
- --jobs=0
additional_dependencies:
- python-chess
- requests
- pygame
exclude: ^(Bash/|\.venv/)
# ===========================================================================
# BANDIT - Security linter
# ===========================================================================
- repo: https://github.com/PyCQA/bandit
rev: 1.7.10
hooks:
- id: bandit
args:
- -c
- pyproject.toml
- --severity-level=low
- --confidence-level=low
additional_dependencies: ["bandit[toml]"]
exclude: ^(Bash/|\.venv/|tests/|.*test.*\.py$)
# ===========================================================================
# VULTURE - Dead code detection
# ===========================================================================
- repo: https://github.com/jendrikseipp/vulture
rev: v2.13
hooks:
- id: vulture
args:
- --min-confidence=80
- --exclude=.venv,Bash,__pycache__
exclude: ^(Bash/|\.venv/)
# ===========================================================================
# PYUPGRADE - Upgrade Python syntax
# ===========================================================================
- repo: https://github.com/asottile/pyupgrade
rev: v3.19.0
hooks:
- id: pyupgrade
args:
- --py310-plus
# ===========================================================================
# CODESPELL - Spell checking in code
# ===========================================================================
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
- id: codespell
args:
- --skip=*.json,*.lock,*.min.js,*.min.css,.git,__pycache__,.venv
- --ignore-words-list=ans,ect,nd,som,sur
exclude: ^(Bash/ffmpeg-build/)
# ===========================================================================
# DOCFORMATTER - Format docstrings (using local hook due to compatibility)
# ===========================================================================
- repo: local
hooks:
- id: docformatter
name: docformatter
entry: docformatter
language: system
types: [python]
args:
- --in-place
- --wrap-summaries=88
- --wrap-descriptions=88
# ===========================================================================
# INTERROGATE - Docstring coverage
# ===========================================================================
- repo: https://github.com/econchick/interrogate
rev: 1.7.0
hooks:
- id: interrogate
args:
- --fail-under=0
- --verbose
- --ignore-init-method
- --ignore-init-module
- --ignore-magic
- --ignore-private
- --ignore-semiprivate
- --exclude=Bash,.venv,__pycache__
pass_filenames: false
# ===========================================================================
# AUTOFLAKE - Remove unused imports/variables
# ===========================================================================
- repo: https://github.com/PyCQA/autoflake
rev: v2.3.1
hooks:
- id: autoflake
args:
- --in-place
- --remove-all-unused-imports
- --remove-unused-variables
- --remove-duplicate-keys
- --expand-star-imports
# ===========================================================================
# SAFETY - Check for security vulnerabilities in dependencies
# ===========================================================================
# Note: Safety requires API key for full functionality, disabled by default
# - repo: https://github.com/Lucas-C/pre-commit-hooks-safety
# rev: v1.3.2
# hooks:
# - id: python-safety-dependencies-check
# files: requirements.*\.txt$
# ===========================================================================
# PYRIGHT - Microsoft's type checker (very strict, optional)
# ===========================================================================
# Uncomment to enable - can be slow and very strict
# - repo: https://github.com/RobertCraiworthy/pyright-action
# rev: v1.1.350
# hooks:
# - id: pyright
# ===========================================================================
# FLAKE8 - Traditional linter (supplementary to ruff)
# ===========================================================================
- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8
args:
- --max-line-length=88
- --extend-ignore=E203,W503
- --max-complexity=10
- --statistics
additional_dependencies:
- flake8-bugbear
- flake8-comprehensions
- flake8-simplify
- flake8-print
exclude: ^(Bash/|\.venv/)
# ===========================================================================
# CHECK JSON/YAML/TOML formatting
# ===========================================================================
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier
types_or: [yaml, json, markdown]
exclude: ^(Bash/|\.venv/|.*\.lock$)
# ===========================================================================
# SHELLCHECK - Shell script linting
# ===========================================================================
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1
hooks:
- id: shellcheck
args: [--severity=warning]
# ===========================================================================
# COMMITIZEN - Conventional commits (optional)
# ===========================================================================
# - repo: https://github.com/commitizen-tools/commitizen
# rev: v3.13.0
# hooks:
# - id: commitizen
# - id: commitizen-branch
# stages: [push]

View File

@ -1,6 +1,6 @@
{
"files.associations": {
"*.py": "python",
"stdio.h": "c"
}
"files.associations": {
"*.py": "python",
"stdio.h": "c"
}
}

122
.vscode/tasks.json vendored
View File

@ -1,64 +1,62 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"problemMatcher": [
"$pytest"
],
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"isBackground": false,
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"isBackground": false,
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
}
]
"version": "2.0.0",
"tasks": [
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"problemMatcher": ["$pytest"],
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"isBackground": false,
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"isBackground": false,
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
}
]
}

@ -0,0 +1 @@
Subproject commit 0bc54cddb1050c3c55bc65adbd3c8aa90d7eb457

View File

@ -14,5 +14,5 @@ WarningsAsErrors: >
cert-err33-c,
cert-err34-c,
cert-fio38-c
HeaderFilterRegex: '.*'
HeaderFilterRegex: ".*"
FormatStyle: none

View File

@ -1,6 +1,7 @@
# Simple OpenGL FPS (C + FreeGLUT)
A tiny first-person demo using legacy OpenGL (compat) and FreeGLUT:
- Move with WASD, hold Tab or Q to sprint
- Aim with mouse (captured by default). Press M to toggle capture
- Shoot with Left Mouse or Space. Hit the red cube to score; it respawns
@ -24,9 +25,11 @@ make -C C/fps run
```
If your distro uses different package names, install the equivalents of:
- libgl1, libglu1, freeglut (dev headers)
## Notes
- This uses old-school fixed-function OpenGL for simplicity and broad compatibility.
- Mouse is confined via glutWarpPointer; press M if you need to release it.
- SDL2 is used only for simple procedurally generated sound effects (shoot, hit, game over).

View File

@ -1,5 +1,5 @@
# Lint script for imageViewer project
#!/bin/bash
# Lint script for imageViewer project
set -e

View File

@ -4,4 +4,3 @@ sudo apt-get install libjpeg-dev \\
Run make \\
make \\
Run ./generate_images

View File

@ -18,9 +18,11 @@ Run:
```
Tips:
- ESC clears selection.
- Press `m` to cycle to a stored mistake position and practice the best move there.
- If you play Black, the board flips so Black is at the bottom.
Notes:
- Rendering avoids TTF dependency; pieces are clear, high-contrast geometric glyphs.

View File

@ -1,3 +1,5 @@
# Textures and Audio used:
## Space Game Starter Set by [hc](https://opengameart.org/users/hc) - https://opengameart.org/content/space-game-starter-set
## 100 seamless textures by [Mitch Featherston](http://pdtextures.blogspot.com/) (Submitted by [Clint Bellanger](https://opengameart.org/users/clint-bellanger) - https://opengameart.org/content/100-seamless-textures

0
CPP/miscelanious/Pi/main.cpp Executable file → Normal file
View File

0
CPP/miscelanious/brydz/brydz.cpp Executable file → Normal file
View File

0
CPP/miscelanious/calculateShotsDarts/basic.cpp Executable file → Normal file
View File

0
CPP/miscelanious/calculateShotsDarts/main.cpp Executable file → Normal file
View File

0
CPP/miscelanious/findIntegerPercentageValue/main.cpp Executable file → Normal file
View File

View File

0
CPP/miscelanious/howOftenDoesCharOccur.cpp Executable file → Normal file
View File

0
CPP/miscelanious/markovChainGenerator/basic.cpp Executable file → Normal file
View File

0
CPP/miscelanious/markovChainGenerator/loremIpsum.txt Executable file → Normal file
View File

0
CPP/miscelanious/markovChainGenerator/main.cpp Executable file → Normal file
View File

View File

0
CPP/miscelanious/quickchallenges.cpp Executable file → Normal file
View File

0
CPP/miscelanious/randomDevice/main.cpp Executable file → Normal file
View File

0
CPP/miscelanious/reverseString.cpp Executable file → Normal file
View File

0
CPP/miscelanious/solveQuadraticEquation.cpp Executable file → Normal file
View File

0
CPP/miscelanious/tictactoe/tictactoe.cpp Executable file → Normal file
View File

View File

0
CPP/miscelanious/xGoesTo0/xgoes.cpp Executable file → Normal file
View File

0
CPP/miscelanious/yousuckatcards/Bernouli/bernouli.cpp Executable file → Normal file
View File

0
CPP/miscelanious/yousuckatcards/Bernouli/test.cpp Executable file → Normal file
View File

0
CPP/miscelanious/yousuckatcards/makefile Executable file → Normal file
View File

0
CPP/miscelanious/yousuckatcards/yousuckatcards.cpp Executable file → Normal file
View File

0
CPP/tests/howCppHandlesDivision.cpp Executable file → Normal file
View File

View File

@ -1,11 +1,12 @@
import requests
import json
import os
from pathlib import Path
import requests
requests_send = 0
while requests_send < 90:
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
response = json.loads(res.text)
urls = []
@ -24,7 +25,7 @@ while requests_send < 90:
image_path = os.path.join("./CATS2/", image_name)
# Save the image to the directory
with open(image_path, 'wb') as file:
with open(image_path, "wb") as file:
file.write(response.content)
print(f"Saved {url} as {image_path}")

View File

@ -1,4 +1,4 @@
requests
json
os
pathlib
requests

111
PYTHON/extractLinks/main.py Normal file → Executable file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3
"""
Extract hosts from href attributes in an HTML file and write them as *host* per line.
"""Extract hosts from href attributes in an HTML file and write them as *host* per line.
Usage:
python main.py INPUT_HTML [OUTPUT_TXT]
@ -12,79 +11,79 @@ alongside the input file.
from __future__ import annotations
import argparse
import os
from html.parser import HTMLParser
from typing import List, Set
import os
from urllib.parse import urlparse
class _HrefParser(HTMLParser):
def __init__(self) -> None:
super().__init__()
self.hrefs: List[str] = []
def __init__(self) -> None:
super().__init__()
self.hrefs: list[str] = []
def handle_starttag(self, tag: str, attrs): # type: ignore[override]
# Collect any href attribute on any tag
for (k, v) in attrs:
if k.lower() == "href" and v is not None:
self.hrefs.append(v)
def handle_starttag(self, tag: str, attrs): # type: ignore[override]
# Collect any href attribute on any tag
for k, v in attrs:
if k.lower() == "href" and v is not None:
self.hrefs.append(v)
def extract_hosts_from_html(html_text: str) -> List[str]:
"""Parse HTML text, extract href values, and return a list of hostnames.
def extract_hosts_from_html(html_text: str) -> list[str]:
"""Parse HTML text, extract href values, and return a list of hostnames.
Rules:
- Only http/https URLs are considered.
- Output is the network location (host[:port]) without scheme or path.
- Duplicates are removed, preserving first-seen order.
"""
parser = _HrefParser()
parser.feed(html_text)
Rules:
- Only http/https URLs are considered.
- Output is the network location (host[:port]) without scheme or path.
- Duplicates are removed, preserving first-seen order.
"""
parser = _HrefParser()
parser.feed(html_text)
seen: Set[str] = set()
hosts: List[str] = []
for href in parser.hrefs:
parsed = urlparse(href)
if parsed.scheme in {"http", "https"} and parsed.netloc:
host = parsed.netloc
if host not in seen:
seen.add(host)
hosts.append(host)
return hosts
seen: set[str] = set()
hosts: list[str] = []
for href in parser.hrefs:
parsed = urlparse(href)
if parsed.scheme in {"http", "https"} and parsed.netloc:
host = parsed.netloc
if host not in seen:
seen.add(host)
hosts.append(host)
return hosts
def main() -> int:
ap = argparse.ArgumentParser(description="Extract hosts from hrefs in an HTML file.")
ap.add_argument("input_html", help="Path to input HTML file")
ap.add_argument(
"output_txt",
nargs="?",
help="Path to output text file (defaults to <input_basename>_links.txt in the same directory)",
)
args = ap.parse_args()
ap = argparse.ArgumentParser(
description="Extract hosts from hrefs in an HTML file."
)
ap.add_argument("input_html", help="Path to input HTML file")
ap.add_argument(
"output_txt",
nargs="?",
help="Path to output text file (defaults to <input_basename>_links.txt in the same directory)",
)
args = ap.parse_args()
input_path = args.input_html
if not os.path.isfile(input_path):
raise SystemExit(f"Input file not found: {input_path}")
input_path = args.input_html
if not os.path.isfile(input_path):
raise SystemExit(f"Input file not found: {input_path}")
out_path = args.output_txt
if not out_path:
base = os.path.splitext(os.path.basename(input_path))[0]
out_path = os.path.join(os.path.dirname(input_path), f"{base}_links.txt")
out_path = args.output_txt
if not out_path:
base = os.path.splitext(os.path.basename(input_path))[0]
out_path = os.path.join(os.path.dirname(input_path), f"{base}_links.txt")
with open(input_path, "r", encoding="utf-8", errors="ignore") as f:
html_text = f.read()
with open(input_path, encoding="utf-8", errors="ignore") as f:
html_text = f.read()
hosts = extract_hosts_from_html(html_text)
hosts = extract_hosts_from_html(html_text)
with open(out_path, "w", encoding="utf-8") as f:
for host in hosts:
f.write(f"*{host}*\n")
with open(out_path, "w", encoding="utf-8") as f:
for host in hosts:
f.write(f"*{host}*\n")
print(f"Wrote {len(hosts)} host(s) to {out_path}")
return 0
print(f"Wrote {len(hosts)} host(s) to {out_path}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
raise SystemExit(main())

View File

@ -1,9 +1,6 @@
import os
from pathlib import Path
import subprocess
import sys
from pathlib import Path
import pytest
# Allow importing from project root when running pytest from this folder
ROOT = Path(__file__).resolve().parents[1]

View File

@ -25,6 +25,7 @@ A fun 2-player cooperative word game where players take turns selecting adjacent
## Keyboard Adjacency
Each key is adjacent to its neighbors (including diagonals). For example:
- 'S' is adjacent to: Q, W, E, A, D, Z, X, C
- 'F' is adjacent to: E, R, T, D, G, C, V, B

View File

@ -1,8 +1,9 @@
import pygame
import sys
import json
import os
import random
import sys
import pygame
# Initialize Pygame
pygame.init()
@ -21,41 +22,42 @@ PLAYER_COLORS = [(255, 100, 100), (100, 100, 255)]
# Keyboard layout
KEYBOARD_LAYOUT = [
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
['z', 'x', 'c', 'v', 'b', 'n', 'm']
["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"],
["a", "s", "d", "f", "g", "h", "j", "k", "l"],
["z", "x", "c", "v", "b", "n", "m"],
]
# Key adjacency mapping
KEY_ADJACENCY = {
'q': ['w', 'a', 's'],
'w': ['q', 'e', 'a', 's', 'd'],
'e': ['w', 'r', 's', 'd', 'f'],
'r': ['e', 't', 'd', 'f', 'g'],
't': ['r', 'y', 'f', 'g', 'h'],
'y': ['t', 'u', 'g', 'h', 'j'],
'u': ['y', 'i', 'h', 'j', 'k'],
'i': ['u', 'o', 'j', 'k', 'l'],
'o': ['i', 'p', 'k', 'l'],
'p': ['o', 'l'],
'a': ['q', 'w', 's', 'z', 'x'],
's': ['q', 'w', 'e', 'a', 'd', 'z', 'x', 'c'],
'd': ['w', 'e', 'r', 's', 'f', 'x', 'c', 'v'],
'f': ['e', 'r', 't', 'd', 'g', 'c', 'v', 'b'],
'g': ['r', 't', 'y', 'f', 'h', 'v', 'b', 'n'],
'h': ['t', 'y', 'u', 'g', 'j', 'b', 'n', 'm'],
'j': ['y', 'u', 'i', 'h', 'k', 'n', 'm'],
'k': ['u', 'i', 'o', 'j', 'l', 'm'],
'l': ['i', 'o', 'p', 'k'],
'z': ['a', 's', 'x'],
'x': ['a', 's', 'd', 'z', 'c'],
'c': ['s', 'd', 'f', 'x', 'v'],
'v': ['d', 'f', 'g', 'c', 'b'],
'b': ['f', 'g', 'h', 'v', 'n'],
'n': ['g', 'h', 'j', 'b', 'm'],
'm': ['h', 'j', 'k', 'n']
"q": ["w", "a", "s"],
"w": ["q", "e", "a", "s", "d"],
"e": ["w", "r", "s", "d", "f"],
"r": ["e", "t", "d", "f", "g"],
"t": ["r", "y", "f", "g", "h"],
"y": ["t", "u", "g", "h", "j"],
"u": ["y", "i", "h", "j", "k"],
"i": ["u", "o", "j", "k", "l"],
"o": ["i", "p", "k", "l"],
"p": ["o", "l"],
"a": ["q", "w", "s", "z", "x"],
"s": ["q", "w", "e", "a", "d", "z", "x", "c"],
"d": ["w", "e", "r", "s", "f", "x", "c", "v"],
"f": ["e", "r", "t", "d", "g", "c", "v", "b"],
"g": ["r", "t", "y", "f", "h", "v", "b", "n"],
"h": ["t", "y", "u", "g", "j", "b", "n", "m"],
"j": ["y", "u", "i", "h", "k", "n", "m"],
"k": ["u", "i", "o", "j", "l", "m"],
"l": ["i", "o", "p", "k"],
"z": ["a", "s", "x"],
"x": ["a", "s", "d", "z", "c"],
"c": ["s", "d", "f", "x", "v"],
"v": ["d", "f", "g", "c", "b"],
"b": ["f", "g", "h", "v", "n"],
"n": ["g", "h", "j", "b", "m"],
"m": ["h", "j", "k", "n"],
}
class KeyboardCoopGame:
def __init__(self):
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
@ -85,8 +87,10 @@ class KeyboardCoopGame:
def load_dictionary(self):
"""Load dictionary from words_dictionary.json file"""
try:
dictionary_path = os.path.join(os.path.dirname(__file__), 'words_dictionary.json')
with open(dictionary_path, 'r', encoding='utf-8') as f:
dictionary_path = os.path.join(
os.path.dirname(__file__), "words_dictionary.json"
)
with open(dictionary_path, encoding="utf-8") as f:
dictionary_data = json.load(f)
# Convert to set for faster lookup (we only need the keys)
return set(dictionary_data.keys())
@ -94,31 +98,103 @@ class KeyboardCoopGame:
print("Warning: words_dictionary.json not found, using fallback dictionary")
# Fallback to a smaller dictionary if file not found
return {
'cat', 'dog', 'car', 'bat', 'rat', 'hat', 'mat', 'sat', 'fat', 'pat',
'the', 'and', 'for', 'are', 'but', 'not', 'you', 'all', 'can', 'had',
'her', 'was', 'one', 'our', 'out', 'day', 'get', 'has', 'him', 'his',
'how', 'man', 'new', 'now', 'old', 'see', 'two', 'way', 'who', 'boy',
'work', 'know', 'place', 'year', 'live', 'me', 'back', 'give', 'good'
"cat",
"dog",
"car",
"bat",
"rat",
"hat",
"mat",
"sat",
"fat",
"pat",
"the",
"and",
"for",
"are",
"but",
"not",
"you",
"all",
"can",
"had",
"her",
"was",
"one",
"our",
"out",
"day",
"get",
"has",
"him",
"his",
"how",
"man",
"new",
"now",
"old",
"see",
"two",
"way",
"who",
"boy",
"work",
"know",
"place",
"year",
"live",
"me",
"back",
"give",
"good",
}
except json.JSONDecodeError:
print("Warning: Error reading words_dictionary.json, using fallback dictionary")
print(
"Warning: Error reading words_dictionary.json, using fallback dictionary"
)
return {
'cat', 'dog', 'car', 'bat', 'rat', 'hat', 'mat', 'sat', 'fat', 'pat',
'the', 'and', 'for', 'are', 'but', 'not', 'you', 'all', 'can', 'had',
'work', 'know', 'place', 'year', 'live', 'me', 'back', 'give', 'good'
"cat",
"dog",
"car",
"bat",
"rat",
"hat",
"mat",
"sat",
"fat",
"pat",
"the",
"and",
"for",
"are",
"but",
"not",
"you",
"all",
"can",
"had",
"work",
"know",
"place",
"year",
"live",
"me",
"back",
"give",
"good",
}
def generate_random_keyboard(self):
"""Generate a random keyboard layout and calculate adjacencies"""
# All 26 letters
all_letters = list('abcdefghijklmnopqrstuvwxyz')
all_letters = list("abcdefghijklmnopqrstuvwxyz")
random.shuffle(all_letters)
# Create random layout with same structure as QWERTY (10-9-7)
self.keyboard_layout = [
all_letters[0:10], # Top row: 10 keys
all_letters[0:10], # Top row: 10 keys
all_letters[10:19], # Middle row: 9 keys
all_letters[19:26] # Bottom row: 7 keys
all_letters[19:26], # Bottom row: 7 keys
]
# Update available letters
@ -137,9 +213,14 @@ class KeyboardCoopGame:
# Check all 8 directions (including diagonals)
directions = [
(-1, -1), (-1, 0), (-1, 1), # Above
(0, -1), (0, 1), # Same row
(1, -1), (1, 0), (1, 1) # Below
(-1, -1),
(-1, 0),
(-1, 1), # Above
(0, -1),
(0, 1), # Same row
(1, -1),
(1, 0),
(1, 1), # Below
]
for dr, dc in directions:
@ -203,13 +284,19 @@ class KeyboardCoopGame:
self.current_word += letter
# Update available letters to include adjacent letters AND the same letter
adjacent_letters = set(self.key_adjacency[letter]) if letter in self.key_adjacency else set()
adjacent_letters = (
set(self.key_adjacency[letter])
if letter in self.key_adjacency
else set()
)
adjacent_letters.add(letter) # Allow selecting the same letter again
self.available_letters = adjacent_letters
# Switch player
self.current_player = 1 - self.current_player
self.message = f"Player {self.current_player + 1}: Choose an adjacent letter!"
self.message = (
f"Player {self.current_player + 1}: Choose an adjacent letter!"
)
# If no valid moves available, force word submission
if not self.available_letters:
@ -280,7 +367,9 @@ class KeyboardCoopGame:
self.screen.blit(title, (30, 20))
# Current word
word_text = self.font.render(f"Current Word: {self.current_word.upper()}", True, TEXT_COLOR)
word_text = self.font.render(
f"Current Word: {self.current_word.upper()}", True, TEXT_COLOR
)
self.screen.blit(word_text, (30, 50))
# Score
@ -289,7 +378,9 @@ class KeyboardCoopGame:
# Current player
player_color = PLAYER_COLORS[self.current_player]
player_text = self.font.render(f"Current Player: {self.current_player + 1}", True, player_color)
player_text = self.font.render(
f"Current Player: {self.current_player + 1}", True, player_color
)
self.screen.blit(player_text, (30, 100))
# Message
@ -305,7 +396,7 @@ class KeyboardCoopGame:
"• Press ENTER to submit word (min 3 letters)",
"• Press R to reset game",
"• Score increases exponentially",
"• Keyboard randomizes after each valid word!"
"• Keyboard randomizes after each valid word!",
]
start_x = 750
@ -383,6 +474,7 @@ class KeyboardCoopGame:
pygame.quit()
sys.exit()
if __name__ == "__main__":
game = KeyboardCoopGame()
game.run()

View File

@ -27,10 +27,10 @@ pip install -r PYTHON/lichess_bot/requirements.txt
## Activate BOT and get a token
1) Create or use an existing Lichess account for your bot.
2) Activate it as a BOT (one-time): https://lichess.org/api#tag/Bot
1. Create or use an existing Lichess account for your bot.
2. Activate it as a BOT (one-time): https://lichess.org/api#tag/Bot
- If not already BOT, you need to convert the account; follow Lichess docs.
3) Create a personal API token: https://lichess.org/account/oauth/token/create
3. Create a personal API token: https://lichess.org/account/oauth/token/create
- Grant scopes: bot:play, challenge:read, challenge:write
Export the token in your shell (recommended):

View File

@ -1,2 +1,3 @@
"""Package marker for lichess_bot."""
__all__ = []

View File

@ -1,15 +1,11 @@
import os
import shutil
import subprocess
import logging
from typing import Optional, Tuple
import chess
class RandomEngine:
"""
Thin wrapper around the C engine in C/lichess_random_engine/random_engine.
"""Thin wrapper around the C engine in C/lichess_random_engine/random_engine.
Contract:
- Given a chess.Board, call the C binary with all legal moves encoded as
@ -20,7 +16,13 @@ class RandomEngine:
- If the binary is missing or returns an invalid/illegal move, raise.
"""
def __init__(self, *, engine_path: Optional[str] = None, max_time_sec: float = 2.0, depth: Optional[int] = None):
def __init__(
self,
*,
engine_path: str | None = None,
max_time_sec: float = 2.0,
depth: int | None = None,
):
self.max_time_sec = max_time_sec
# depth is accepted for compatibility with existing callers but is unused;
# the C engine handles its own scoring/selection.
@ -29,12 +31,17 @@ class RandomEngine:
default_path = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"..", "..",
"C", "lichess_random_engine", "random_engine",
"..",
"..",
"C",
"lichess_random_engine",
"random_engine",
)
)
self.engine_path = engine_path or default_path
if not os.path.isfile(self.engine_path) or not os.access(self.engine_path, os.X_OK):
if not os.path.isfile(self.engine_path) or not os.access(
self.engine_path, os.X_OK
):
raise FileNotFoundError(
f"C engine not found or not executable at '{self.engine_path}'. "
"Build it first (make -C C/lichess_random_engine)."
@ -58,10 +65,14 @@ class RandomEngine:
return out
def choose_move(self, board: chess.Board) -> chess.Move:
mv, _ = self.choose_move_with_explanation(board, time_budget_sec=self.max_time_sec)
mv, _ = self.choose_move_with_explanation(
board, time_budget_sec=self.max_time_sec
)
return mv
def choose_move_with_explanation(self, board: chess.Board, *, time_budget_sec: float) -> Tuple[Optional[chess.Move], str]:
def choose_move_with_explanation(
self, board: chess.Board, *, time_budget_sec: float
) -> tuple[chess.Move | None, str]:
# Collect legal moves and send to engine as plain UCI tokens.
legal = list(board.legal_moves)
if not legal:
@ -78,10 +89,14 @@ class RandomEngine:
try:
move = chess.Move.from_uci(chosen_uci)
except Exception:
raise RuntimeError(f"Engine returned invalid move: '{chosen_uci}' (output: {output!r})")
raise RuntimeError(
f"Engine returned invalid move: '{chosen_uci}' (output: {output!r})"
)
if move not in board.legal_moves:
raise RuntimeError(f"Engine returned illegal move for position: {chosen_uci}")
raise RuntimeError(
f"Engine returned illegal move for position: {chosen_uci}"
)
return move, "from_c_engine"
@ -91,9 +106,8 @@ class RandomEngine:
proposed_move_uci: str,
*,
time_budget_sec: float,
) -> Tuple[float, str, Optional[chess.Move], str]:
"""
Ask the C engine to explain the current move list and analyze a specific candidate.
) -> tuple[float, str, chess.Move | None, str]:
"""Ask the C engine to explain the current move list and analyze a specific candidate.
Returns (candidate_score, candidate_expl, best_move, best_expl)
where explanations are concise JSON snippets from the engine. All logic is
@ -103,13 +117,16 @@ class RandomEngine:
if not legal:
return 0.0, "no_legal_moves", None, "no_best_move"
args = ["--fen", board.fen(), "--explain", "--analyze", proposed_move_uci] + [m.uci() for m in legal]
args = ["--fen", board.fen(), "--explain", "--analyze", proposed_move_uci] + [
m.uci() for m in legal
]
out = self._call_engine(args, timeout=max(0.1, time_budget_sec))
# Try to parse the engine's JSON explanation
import json as _json
cand_score = 0.0
best_move: Optional[chess.Move] = None
best_move: chess.Move | None = None
cand_expl = out
best_expl = out
try:
@ -130,10 +147,13 @@ class RandomEngine:
best_move = None
# Store compact explanations for debugging
cand_expl = _json.dumps(analyze, ensure_ascii=False)
best_expl = _json.dumps({
"chosen_index": data.get("chosen_index"),
"chosen_move": data.get("chosen_move"),
}, ensure_ascii=False)
best_expl = _json.dumps(
{
"chosen_index": data.get("chosen_index"),
"chosen_move": data.get("chosen_move"),
},
ensure_ascii=False,
)
except Exception:
# Leave defaults with raw output text
pass

View File

@ -1,26 +1,29 @@
from collections.abc import Generator
import json
import logging
import time
from typing import Dict, Generator, Optional, Tuple
import requests
import chess
import requests
LICHESS_API = "https://lichess.org"
class LichessAPI:
def __init__(self, token: str, session: Optional[requests.Session] = None):
def __init__(self, token: str, session: requests.Session | None = None):
self.token = token
self.session = session or requests.Session()
self.session.headers.update({
"Authorization": f"Bearer {self.token}",
"Accept": "application/json",
"User-Agent": "minimal-lichess-bot/0.1 (+https://lichess.org)"
})
self.session.headers.update(
{
"Authorization": f"Bearer {self.token}",
"Accept": "application/json",
"User-Agent": "minimal-lichess-bot/0.1 (+https://lichess.org)",
}
)
def _request(self, method: str, url: str, *, raise_for_status: bool = False, **kwargs) -> requests.Response:
def _request(
self, method: str, url: str, *, raise_for_status: bool = False, **kwargs
) -> requests.Response:
"""Wrapper around session.request that logs every request/response.
- Logs start (method+URL) and end (status, elapsed).
@ -32,7 +35,7 @@ class LichessAPI:
try:
r = self.session.request(method, url, **kwargs)
except Exception as e:
logging.error(f"HTTP {method} {url} -> exception: {e}")
logging.exception(f"HTTP {method} {url} -> exception: {e}")
raise
elapsed = time.monotonic() - t0
status = r.status_code
@ -45,7 +48,9 @@ class LichessAPI:
except Exception:
snippet = None
if snippet:
logging.warning(f"HTTP {method} {url} -> {status} in {elapsed:.2f}s body='{snippet}'")
logging.warning(
f"HTTP {method} {url} -> {status} in {elapsed:.2f}s body='{snippet}'"
)
else:
logging.warning(f"HTTP {method} {url} -> {status} in {elapsed:.2f}s")
else:
@ -54,14 +59,16 @@ class LichessAPI:
r.raise_for_status()
return r
def stream_events(self) -> Generator[Dict, None, None]:
def stream_events(self) -> Generator[dict, None, None]:
url = f"{LICHESS_API}/api/stream/event"
backoff = 0.5
while True:
try:
# Use NDJSON Accept and no timeout for long-lived stream
headers = {"Accept": "application/x-ndjson"}
with self._request("GET", url, headers=headers, stream=True, timeout=None) as r:
with self._request(
"GET", url, headers=headers, stream=True, timeout=None
) as r:
r.raise_for_status()
backoff = 0.5 # reset on success
for line in r.iter_lines(decode_unicode=True):
@ -89,7 +96,9 @@ class LichessAPI:
data = {"reason": reason}
self._request("POST", url, data=data, timeout=30, raise_for_status=True)
def join_game_stream(self, game_id: str, my_color: Optional[str]) -> Tuple[chess.Board, str]:
def join_game_stream(
self, game_id: str, my_color: str | None
) -> tuple[chess.Board, str]:
"""Deprecated: use stream_game_events and parse initial state there."""
# Fallback to initial behavior for compatibility
url = f"{LICHESS_API}/api/board/game/stream/{game_id}"
@ -125,7 +134,7 @@ class LichessAPI:
break
return board, color
def stream_game_events(self, game_id: str) -> Generator[Dict, None, None]:
def stream_game_events(self, game_id: str) -> Generator[dict, None, None]:
url = f"{LICHESS_API}/api/board/game/stream/{game_id}"
headers = {"Accept": "application/x-ndjson"}
with self._request("GET", url, headers=headers, stream=True, timeout=None) as r:
@ -151,11 +160,11 @@ class LichessAPI:
r = self._request("POST", url, timeout=30)
r.raise_for_status()
def get_game_state(self, game_id: str) -> Optional[Dict]:
def get_game_state(self, game_id: str) -> dict | None:
"""Deprecated: use stream_game_events in a persistent loop."""
return None
def get_my_user_id(self) -> Optional[str]:
def get_my_user_id(self) -> str | None:
url = f"{LICHESS_API}/api/account"
r = self._request("GET", url, timeout=30)
if r.status_code == 200:

View File

@ -2,14 +2,12 @@ import argparse
import json
import logging
import os
import subprocess
import sys
import threading
import time
from typing import Optional
import chess
import chess.pgn
import subprocess
import sys
from .engine import RandomEngine
from .lichess_api import LichessAPI
@ -35,10 +33,10 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
game_threads = {}
def handle_game(game_id: str, my_color: Optional[str] = None):
def handle_game(game_id: str, my_color: str | None = None):
logging.info(f"Starting game thread for {game_id} [bot v{bot_version}]")
board = chess.Board()
color: Optional[str] = my_color
color: str | None = my_color
# Track how many moves we have already processed; start at -1 so we act on the first state (0 moves)
last_handled_len = -1
# Prepare a per-game log file
@ -54,14 +52,13 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
opp_ms = None
inc_ms = 0
# Meta info for logging/PGN
game_date_iso: Optional[str] = None
white_name: Optional[str] = None
black_name: Optional[str] = None
site_url: Optional[str] = None
game_date_iso: str | None = None
white_name: str | None = None
black_name: str | None = None
site_url: str | None = None
try:
# Only send moves on authoritative gameState events to avoid race
# conditions right after gameFull arrives.
seen_game_full = False
for event in api.stream_game_events(game_id):
et = event.get("type")
if et in ("gameFull", "gameState"):
@ -71,8 +68,16 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
moves = state.get("moves", "")
status = state.get("status")
# clocks are in milliseconds if present
my_ms = state.get("wtime") if color == "white" else state.get("btime")
opp_ms = state.get("btime") if color == "white" else state.get("wtime")
my_ms = (
state.get("wtime")
if color == "white"
else state.get("btime")
)
opp_ms = (
state.get("btime")
if color == "white"
else state.get("wtime")
)
inc_ms = state.get("winc") or state.get("binc") or 0
# Discover my color from gameFull
white_id = event["white"].get("id")
@ -82,10 +87,15 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
# Set site and date if available
try:
# Lichess event may include 'createdAt' ms epoch
created_ms = event.get("createdAt") or event.get("createdAtDate")
created_ms = event.get("createdAt") or event.get(
"createdAtDate"
)
if created_ms:
import datetime
game_date_iso = datetime.datetime.utcfromtimestamp(int(created_ms)/1000).strftime("%Y.%m.%d")
game_date_iso = datetime.datetime.utcfromtimestamp(
int(created_ms) / 1000
).strftime("%Y.%m.%d")
except Exception:
pass
site_url = f"https://lichess.org/{game_id}"
@ -95,7 +105,6 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
elif me == black_id:
color = "black"
logging.info(f"Game {game_id}: joined as {color} (gameFull)")
seen_game_full = True
else:
moves = event.get("moves", "")
status = event.get("status")
@ -115,7 +124,9 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
f"Game {game_id}: event={et}, moves={new_len}, color={color}"
)
if new_len == last_handled_len:
logging.debug(f"Game {game_id}: position unchanged (len={new_len}), skipping")
logging.debug(
f"Game {game_id}: position unchanged (len={new_len}), skipping"
)
continue
# Rebuild board from moves
@ -127,14 +138,18 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
logging.debug(f"Game {game_id}: could not apply move {m}")
if color is None:
logging.info(f"Game {game_id}: color unknown yet; waiting for gameFull")
logging.info(
f"Game {game_id}: color unknown yet; waiting for gameFull"
)
# Do not mark this position handled on gameFull; wait for authoritative gameState
if et == "gameState":
last_handled_len = new_len
continue
is_white_turn = board.turn
my_turn = (is_white_turn and color == "white") or ((not is_white_turn) and color == "black")
my_turn = (is_white_turn and color == "white") or (
(not is_white_turn) and color == "black"
)
logging.info(
f"Game {game_id}: turn={'white' if is_white_turn else 'black'}, my_turn={my_turn}"
)
@ -142,35 +157,54 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
# - Always move on 'gameState' (authoritative)
# - Also allow moving on the initial 'gameFull' when there are zero moves and it's our turn.
# This avoids stalling at game start when Lichess doesn't immediately send a 'gameState' for 0 moves.
allow_move = (et == "gameState") or (et == "gameFull" and new_len == 0)
allow_move = (et == "gameState") or (
et == "gameFull" and new_len == 0
)
if my_turn and allow_move:
# Compute a per-move time budget (seconds) based on remaining time
# Heuristic: use min( max_time_sec, max(0.05, 0.6 * my_time_left/remaining_moves + inc) )
# Estimate remaining moves as 30 - ply/2 bounded to [10, 60]
est_moves_left = max(10, min(60, 30 - board.fullmove_number // 2))
est_moves_left = max(
10, min(60, 30 - board.fullmove_number // 2)
)
time_left_sec = (my_ms or 0) / 1000.0
inc_sec = (inc_ms or 0) / 1000.0
budget = 0.6 * (time_left_sec / max(1, est_moves_left)) + 0.5 * inc_sec
budget = (
0.6 * (time_left_sec / max(1, est_moves_left))
+ 0.5 * inc_sec
)
# Spend more time per move (requested): double the budget
budget *= 2.0
# Keep within reasonable bounds
budget = max(0.05, min(engine.max_time_sec, budget))
move, reason = engine.choose_move_with_explanation(board, time_budget_sec=budget)
move, reason = engine.choose_move_with_explanation(
board, time_budget_sec=budget
)
if move is None:
logging.info(f"Game {game_id}: no legal moves (game likely over)")
logging.info(
f"Game {game_id}: no legal moves (game likely over)"
)
break
try:
# Double-check legality just before sending to avoid 400s when state changed.
if move not in board.legal_moves:
logging.info(f"Game {game_id}: selected move no longer legal; skipping send")
logging.info(
f"Game {game_id}: selected move no longer legal; skipping send"
)
else:
logging.info(f"Game {game_id}: playing {move.uci()} (budget={budget:.2f}s, my_time_left={time_left_sec:.1f}s, inc={inc_sec:.2f}s)")
logging.info(
f"Game {game_id}: playing {move.uci()} (budget={budget:.2f}s, my_time_left={time_left_sec:.1f}s, inc={inc_sec:.2f}s)"
)
if game_log_path:
with open(game_log_path, "a") as lf:
lf.write(f"ply {last_handled_len+1}: {move.uci()}\n{reason}\n\n")
lf.write(
f"ply {last_handled_len+1}: {move.uci()}\n{reason}\n\n"
)
api.make_move(game_id, move)
except Exception as e:
logging.warning(f"Game {game_id}: move {move.uci()} failed: {e}")
logging.warning(
f"Game {game_id}: move {move.uci()} failed: {e}"
)
# Mark this position as handled on authoritative gameState, or after we've
# actually attempted a move (including the first move on gameFull len=0).
if et == "gameState" or (my_turn and allow_move):
@ -178,9 +212,7 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
if status in {"mate", "resign", "stalemate", "timeout", "draw"}:
logging.info(f"Game {game_id} finished: {status}")
break
elif et == "chatLine":
continue
elif et == "opponentGone":
elif et == "chatLine" or et == "opponentGone":
continue
except Exception as e:
logging.exception(f"Game {game_id} thread error: {e}")
@ -204,12 +236,14 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
pass
with open(game_log_path, "a") as lf:
lf.write("\nPGN:\n")
exporter = chess.pgn.StringExporter(headers=True, variations=False, comments=False)
exporter = chess.pgn.StringExporter(
headers=True, variations=False, comments=False
)
lf.write(game.accept(exporter))
lf.write("\n")
# After PGN is written, run analysis and save it to the same file (inserted before PGN)
if game_log_path:
analysis_text: Optional[str] = None
analysis_text: str | None = None
try:
analyze_script = os.path.join(
os.path.dirname(os.path.dirname(__file__)),
@ -245,7 +279,11 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
if m:
# Count as one analyzed ply
analyzed += 1
left = max(0, (total_plies or 0) - analyzed) if total_plies else "?"
left = (
max(0, (total_plies or 0) - analyzed)
if total_plies
else "?"
)
if total_plies:
pct = analyzed / total_plies * 100.0
logging.info(
@ -266,7 +304,7 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
f"Game {game_id}: analysis script exited with code {ret}"
)
if stderr_text:
analysis_text += ("\n[stderr]\n" + stderr_text)
analysis_text += "\n[stderr]\n" + stderr_text
logging.info(f"Game {game_id}: analysis complete")
else:
logging.info(
@ -278,14 +316,18 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
# Insert analysis before the PGN section so future runs can still parse PGN cleanly
if analysis_text:
try:
with open(game_log_path, "r", encoding="utf-8", errors="replace") as f:
with open(
game_log_path, encoding="utf-8", errors="replace"
) as f:
content = f.read()
# Find the start of the 'PGN:' line
insert_idx = 0
p = content.find("\nPGN:\n")
if p != -1:
insert_idx = p + 1 # start of the line after the preceding newline
insert_idx = (
p + 1
) # start of the line after the preceding newline
elif content.startswith("PGN:\n"):
insert_idx = 0
else:
@ -297,21 +339,31 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
if game_date_iso:
meta_lines.append(f"Date: {game_date_iso}")
if white_name or black_name:
meta_lines.append(f"Players: {white_name or '?'} vs {black_name or '?'}")
meta_lines.append(
f"Players: {white_name or '?'} vs {black_name or '?'}"
)
if meta_lines:
meta_block = "\n".join(meta_lines) + "\n"
else:
meta_block = ""
analysis_block = (
(meta_block if meta_block else "") +
"ANALYSIS:\n" + analysis_text.rstrip() + "\n\n"
(meta_block if meta_block else "")
+ "ANALYSIS:\n"
+ analysis_text.rstrip()
+ "\n\n"
)
new_content = (
content[:insert_idx]
+ analysis_block
+ content[insert_idx:]
)
new_content = content[:insert_idx] + analysis_block + content[insert_idx:]
with open(game_log_path, "w", encoding="utf-8") as f:
f.write(new_content)
except Exception as e:
logging.debug(f"Game {game_id}: could not write analysis to log: {e}")
logging.debug(
f"Game {game_id}: could not write analysis to log: {e}"
)
except Exception as e:
logging.debug(f"Game {game_id}: could not write PGN: {e}")
logging.info(f"Ending game thread for {game_id}")
@ -328,19 +380,29 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
variant = challenge.get("variant", {}).get("key", "standard")
speed = challenge.get("speed")
perf_ok = speed in {"bullet", "blitz", "rapid", "classical"}
not_corr = challenge.get("speed") != "correspondence" or not decline_correspondence
not_corr = (
challenge.get("speed") != "correspondence"
or not decline_correspondence
)
if variant == "standard" and perf_ok and not_corr:
logging.info(f"Accepting challenge {ch_id} ({speed})")
api.accept_challenge(ch_id)
else:
logging.info(f"Declining challenge {ch_id} (variant={variant}, speed={speed})")
logging.info(
f"Declining challenge {ch_id} (variant={variant}, speed={speed})"
)
api.decline_challenge(ch_id)
elif event.get("type") == "gameStart":
game_id = event["game"]["id"]
# Spin up a game thread
if game_id not in game_threads or not game_threads[game_id].is_alive():
t = threading.Thread(target=handle_game, args=(game_id,), name=f"game-{game_id}")
if (
game_id not in game_threads
or not game_threads[game_id].is_alive()
):
t = threading.Thread(
target=handle_game, args=(game_id,), name=f"game-{game_id}"
)
t.daemon = True
game_threads[game_id] = t
t.start()
@ -359,8 +421,14 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No
def main():
parser = argparse.ArgumentParser(description="Run a minimal Lichess bot")
parser.add_argument("--log-level", default="INFO", help="Logging level (default: INFO)")
parser.add_argument("--decline-correspondence", action="store_true", help="Decline correspondence challenges")
parser.add_argument(
"--log-level", default="INFO", help="Logging level (default: INFO)"
)
parser.add_argument(
"--decline-correspondence",
action="store_true",
help="Decline correspondence challenges",
)
args = parser.parse_args()
run_bot(args.log_level, args.decline_correspondence)

View File

@ -1,6 +1,6 @@
python-chess==1.999
requests==2.32.3
urllib3==2.2.3
certifi>=2024.2.2
chardet>=5.2.0
idna>=3.7
python-chess==1.999
requests==2.32.3
urllib3==2.2.3

View File

@ -1,6 +1,6 @@
import os
import sys
from pathlib import Path
import sys
# Add repository root to sys.path so 'import PYTHON.*' works when running
# pytest with a subdirectory as rootdir.

View File

@ -1,6 +1,5 @@
import csv
import os
from typing import List, Tuple
import chess
import pytest
@ -8,12 +7,11 @@ import pytest
from PYTHON.lichess_bot.engine import RandomEngine
def _load_top_puzzles(csv_path: str, limit: int = 8) -> List[Tuple[str, str]]:
"""
Return a list of (FEN, solution_moves_str) for the first `limit` rows in the CSV.
def _load_top_puzzles(csv_path: str, limit: int = 8) -> list[tuple[str, str]]:
"""Return a list of (FEN, solution_moves_str) for the first `limit` rows in the CSV.
CSV columns: PuzzleId,FEN,Moves,...
"""
puzzles: List[Tuple[str, str]] = []
puzzles: list[tuple[str, str]] = []
with open(csv_path, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
@ -28,7 +26,9 @@ def _load_top_puzzles(csv_path: str, limit: int = 8) -> List[Tuple[str, str]]:
@pytest.mark.parametrize(
"fen,moves_str",
_load_top_puzzles(os.path.join(os.path.dirname(__file__), "lichess_db_puzzle.csv"), limit=8),
_load_top_puzzles(
os.path.join(os.path.dirname(__file__), "lichess_db_puzzle.csv"), limit=8
),
)
def test_puzzle_engine_follow_solution(fen: str, moves_str: str):
board = chess.Board(fen)
@ -46,7 +46,11 @@ def test_puzzle_engine_follow_solution(fen: str, moves_str: str):
# If engine move differs from solution, fail immediately but provide analysis of the correct move
if mv.uci() != uci:
# Ask the engine to analyze the correct move for debug
score_cp, proposed_expl, best_mv, best_expl = eng.evaluate_proposed_move_with_suggestion(board, uci, time_budget_sec=0.5)
score_cp, proposed_expl, best_mv, best_expl = (
eng.evaluate_proposed_move_with_suggestion(
board, uci, time_budget_sec=0.5
)
)
details = [
f"Puzzle failed at step {step}.",
f"FEN: {fen}",

View File

@ -1,5 +1,3 @@
import builtins
from PYTHON.lichess_bot.utils import backoff_sleep

View File

@ -1,6 +1,3 @@
import os
import tempfile
from PYTHON.lichess_bot.utils import get_and_increment_version
@ -15,5 +12,5 @@ def test_version_file_increments_and_persists(tmp_path, monkeypatch):
assert v2 == 2
# Ensure it persisted
with open(version_file, "r") as f:
with open(version_file) as f:
assert f.read().strip() == "2"

96
PYTHON/lichess_bot/tools/generate_blunder_tests.py Normal file → Executable file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3
"""
Generate pytest cases from one or more lichess analysis logs.
"""Generate pytest cases from one or more lichess analysis logs.
Input: log files that contain a "Columns:" section and a "PGN:" section.
We'll extract each row where class==Blunder, reconstruct the FEN of the
@ -33,12 +32,11 @@ Dependencies: python-chess, pytest (already in requirements.txt)
from __future__ import annotations
from dataclasses import dataclass
import io
import os
import re
import sys
from dataclasses import dataclass
from typing import List, Tuple
import chess
import chess.pgn
@ -48,11 +46,11 @@ import chess.pgn
class Blunder:
ply: int
side: str # 'W' or 'B'
san: str # SAN of the played blunder
san: str # SAN of the played blunder
best_suggestion_san: str # SAN of the best suggestion from log (mandatory)
def parse_columns_for_blunders(text: str) -> List[Blunder]:
def parse_columns_for_blunders(text: str) -> list[Blunder]:
lines = text.splitlines()
# Find start of "Columns:" block
try:
@ -60,9 +58,9 @@ def parse_columns_for_blunders(text: str) -> List[Blunder]:
except StopIteration:
return []
blunders: List[Blunder] = []
blunders: list[Blunder] = []
# Lines after header until a blank line or "PGN:" marker
for ln in lines[idx + 1:]:
for ln in lines[idx + 1 :]:
if not ln.strip():
break
if ln.strip().startswith("PGN:"):
@ -90,13 +88,20 @@ def parse_columns_for_blunders(text: str) -> List[Blunder]:
f"Missing best_suggestion in Columns for blunder row: ply={ply} side={side} move={move_san}.\n"
f"Raw line: '{ln.strip()}'"
)
blunders.append(Blunder(ply=ply, side=side, san=move_san, best_suggestion_san=best_suggestion_san))
blunders.append(
Blunder(
ply=ply,
side=side,
san=move_san,
best_suggestion_san=best_suggestion_san,
)
)
return blunders
def extract_pgn(text: str) -> str | None:
# Extract the PGN block after a line that is exactly 'PGN:' or starts with it
m = re.search(r"^PGN:\s*$", text, flags=re.M)
m = re.search(r"^PGN:\s*$", text, flags=re.MULTILINE)
if not m:
return None
start = m.end()
@ -104,8 +109,8 @@ def extract_pgn(text: str) -> str | None:
return pgn if pgn else None
def san_list_from_game(game: chess.pgn.Game) -> List[str]:
san_moves: List[str] = []
def san_list_from_game(game: chess.pgn.Game) -> list[str]:
san_moves: list[str] = []
node = game
while node.variations:
node = node.variation(0)
@ -113,13 +118,15 @@ def san_list_from_game(game: chess.pgn.Game) -> List[str]:
return san_moves
def fen_and_uci_for_blunders(pgn_text: str, blunders: List[Blunder]) -> List[Tuple[str, str, str, Blunder]]:
def fen_and_uci_for_blunders(
pgn_text: str, blunders: list[Blunder]
) -> list[tuple[str, str, str, Blunder]]:
game = chess.pgn.read_game(io.StringIO(pgn_text))
if game is None:
raise RuntimeError("Failed to parse PGN from log")
main_sans = san_list_from_game(game)
results: List[Tuple[str, str, str, Blunder]] = []
results: list[tuple[str, str, str, Blunder]] = []
for bl in blunders:
# Reconstruct the board before this ply
board = game.board()
@ -198,17 +205,25 @@ def test_engine_avoids_logged_blunder(fen, blunder_uci, label):
)
def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str, str, Blunder]]) -> int:
def append_cases_to_unified_test(
unified_path: str, cases: list[tuple[str, str, str, Blunder]]
) -> int:
"""Append new cases to BLUNDER_CASES in the unified test file, skipping duplicates.
Returns the number of cases actually appended.
"""
ensure_unified_test_file(unified_path)
with open(unified_path, "r", encoding="utf-8") as f:
with open(unified_path, encoding="utf-8") as f:
content = f.read()
# Extract current cases as a set of (fen, uci) to de-duplicate
existing = set(re.findall(r"\(\"(.*?)\",\s*\"(.*?)\",\s*\"ply\d+_[WB]_[^\"]+\"\)\,?", content, flags=re.S))
existing = set(
re.findall(
r"\(\"(.*?)\",\s*\"(.*?)\",\s*\"ply\d+_[WB]_[^\"]+\"\)\,?",
content,
flags=re.DOTALL,
)
)
lines = []
updated_existing = 0
@ -217,7 +232,7 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str,
if key in existing:
# If a best move UCI is available, try to backfill or update it into the label
if best_uci:
side = 'W' if bl.side == 'W' else 'B'
side = "W" if bl.side == "W" else "B"
fen_re = re.escape(fen)
uci_re = re.escape(uci)
base_label = f"ply{bl.ply}_{side}_{uci}"
@ -228,7 +243,9 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str,
if re.search(pattern_no_best, content):
content = re.sub(
pattern_no_best,
lambda m: m.group(0).replace(m.group(1), f"{base_label}_best_{best_uci}"),
lambda m: m.group(0).replace(
m.group(1), f"{base_label}_best_{best_uci}"
),
content,
count=1,
)
@ -236,7 +253,9 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str,
elif re.search(pattern_with_best, content):
content = re.sub(
pattern_with_best,
lambda m: m.group(0).replace(m.group(1), f"{base_label}_best_{best_uci}"),
lambda m: m.group(0).replace(
m.group(1), f"{base_label}_best_{best_uci}"
),
content,
count=1,
)
@ -245,7 +264,7 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str,
label = f"ply{bl.ply}_{'W' if bl.side=='W' else 'B'}_{uci}"
# Encode the best move UCI in the label so tests can extract it without changing tuple shape
label += f"_best_{best_uci}"
lines.append(f" (\"{fen}\", \"{uci}\", \"{label}\"),\n")
lines.append(f' ("{fen}", "{uci}", "{label}"),\n')
if not lines:
return 0
@ -267,7 +286,7 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str,
def _process_single_log(log_path: str) -> int:
"""Process a single log file. Returns 0 on success, non-zero otherwise."""
try:
with open(log_path, "r", encoding="utf-8") as fh:
with open(log_path, encoding="utf-8") as fh:
text = fh.read()
except FileNotFoundError:
print(f"Log file not found: {log_path}")
@ -293,7 +312,9 @@ def _process_single_log(log_path: str) -> int:
print(f"Error converting SAN to UCI in {os.path.basename(log_path)}: {e}")
return 2
if not cases:
print(f"Failed to reconstruct any blunder positions from PGN: {os.path.basename(log_path)}")
print(
f"Failed to reconstruct any blunder positions from PGN: {os.path.basename(log_path)}"
)
return 1
base = os.path.basename(log_path)
@ -301,14 +322,18 @@ def _process_single_log(log_path: str) -> int:
game_id = m.group(1) if m else os.path.splitext(base)[0]
# Always append to the unified test file
unified = os.path.join(os.path.dirname(__file__), "..", "tests", "test_blunders_all.py")
unified = os.path.join(
os.path.dirname(__file__), "..", "tests", "test_blunders_all.py"
)
unified = os.path.abspath(unified)
added = append_cases_to_unified_test(unified, cases)
print(f"Appended {added} new blunder checks to {os.path.relpath(unified)} (game {game_id}).")
print(
f"Appended {added} new blunder checks to {os.path.relpath(unified)} (game {game_id})."
)
return 0
def main(argv: List[str]) -> int:
def main(argv: list[str]) -> int:
script_dir = os.path.dirname(__file__)
past_dir = os.path.abspath(os.path.join(script_dir, "past_games"))
@ -332,7 +357,9 @@ def main(argv: List[str]) -> int:
rc = _process_single_log(lp)
if rc == 0:
ok += 1
print(f"Processed {len(logs)} logs from {past_dir}, succeeded: {ok}, failed: {len(logs)-ok}")
print(
f"Processed {len(logs)} logs from {past_dir}, succeeded: {ok}, failed: {len(logs)-ok}"
)
return 0 if ok > 0 else 1
# One argument: game id or file path
@ -340,15 +367,14 @@ def main(argv: List[str]) -> int:
candidate_path = None
if os.path.isfile(arg):
candidate_path = arg
# Treat as game id, resolve within past_games
elif re.fullmatch(r"[A-Za-z0-9]+", arg):
candidate_path = os.path.join(past_dir, f"lichess_bot_game_{arg}.log")
else:
# Treat as game id, resolve within past_games
if re.fullmatch(r"[A-Za-z0-9]+", arg):
candidate_path = os.path.join(past_dir, f"lichess_bot_game_{arg}.log")
else:
# Fallback: if it's a bare filename, try inside past_games
maybe = os.path.join(past_dir, arg)
if os.path.isfile(maybe):
candidate_path = maybe
# Fallback: if it's a bare filename, try inside past_games
maybe = os.path.join(past_dir, arg)
if os.path.isfile(maybe):
candidate_path = maybe
if not candidate_path:
print("Usage: generate_blunder_tests.py [<game_id>|</path/to/log>]")

View File

@ -22,7 +22,7 @@ def get_and_increment_version() -> int:
path = _version_file_path()
current = 0
try:
with open(path, "r") as f:
with open(path) as f:
raw = f.read().strip()
if raw:
current = int(raw)
@ -54,7 +54,7 @@ def backoff_sleep(current_backoff: int, base: float = 0.5, cap: float = 8.0) ->
- base: base delay in seconds
- cap: maximum delay in seconds
"""
delay = min(cap, base * (2 ** current_backoff))
delay = min(cap, base * (2**current_backoff))
logging.info(f"Backing off for {delay:.1f}s")
time.sleep(delay)
return min(current_backoff + 1, 10)

View File

@ -6,11 +6,10 @@ Install: https://mitmproxy.org/ (install it the way they recommend for your OS,
Run it using `mitmproxy`
Run your webbrowser using `127.0.0.1:8080` as proxy
Run your webbrowser using `127.0.0.1:8080` as proxy
on chromium-based browser it should be enough to do:
`google-chrome --proxy-server=127.0.0.1:8080`
Run `mitmweb`
open 127.0.0.1:808**1**

View File

@ -1,10 +1,11 @@
from mitmproxy import http
def request(flow: http.HTTPFlow) -> None:
# Only intercept traffic to example.com
if "example.com" in flow.request.host:
flow.response = http.Response.make(
502, # Bad Gateway status code
b"Simulated connection failure",
{"Content-Type": "text/plain"}
{"Content-Type": "text/plain"},
)

View File

@ -1,12 +1,15 @@
import random
from PIL import Image
import os
import argparse
from datetime import datetime
import os
import random
def generate_bloated_jpeg(size, color_list, block_size, output_path, quality, image_index, folder):
"""
Generates a random JPEG image with given size, list of colors, and block size.
from PIL import Image
def generate_bloated_jpeg(
size, color_list, block_size, output_path, quality, image_index, folder
):
"""Generates a random JPEG image with given size, list of colors, and block size.
Args:
size (int): Size of the image (both width and height, must be divisible by block_size).
@ -26,7 +29,9 @@ def generate_bloated_jpeg(size, color_list, block_size, output_path, quality, im
pixels = image.load()
# Convert hex colors to RGB
rgb_colors = [tuple(int(color[i:i+2], 16) for i in (1, 3, 5)) for color in color_list]
rgb_colors = [
tuple(int(color[i : i + 2], 16) for i in (1, 3, 5)) for color in color_list
]
# Fill the image with block_size x block_size pixel squares of random colors from the list
for y in range(0, size, block_size):
@ -41,21 +46,63 @@ def generate_bloated_jpeg(size, color_list, block_size, output_path, quality, im
os.makedirs(folder)
# Generate unique output path
unique_output_path = os.path.join(folder, f"{os.path.splitext(output_path)[0]}_{image_index}{os.path.splitext(output_path)[1]}")
unique_output_path = os.path.join(
folder,
f"{os.path.splitext(output_path)[0]}_{image_index}{os.path.splitext(output_path)[1]}",
)
# Save the image with specified quality to maximize file size
image.save(unique_output_path, "JPEG", quality=quality, optimize=False)
return unique_output_path
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate bloated JPEG images with random colors.")
parser.add_argument("-n", "--num_images", type=int, default=1, help="Number of images to generate. Default is 1.")
parser.add_argument("-s", "--size", type=int, default=1000, help="Size of the images (must be 1000 or less and divisible by block size). Default is 1000.")
parser.add_argument("-c", "--colors", nargs='+', default=["#FF5733", "#33FF57", "#3357FF", "#F3FF33", "#FF33F6", "#33FFF6"], help="List of colors in hex format. Default is ['#FF5733', '#33FF57', '#3357FF', '#F3FF33', '#FF33F6', '#33FFF6'].")
parser.add_argument("-b", "--block_size", type=int, default=4, help="Size of the pixel blocks (must divide the image size evenly). Default is 4.")
parser.add_argument("-o", "--output_path", type=str, default="bloated_image.jpeg", help="Base output path for the JPEG images. Default is 'bloated_image.jpeg'.")
parser.add_argument("-q", "--quality", type=int, default=100, help="Quality setting for the JPEG images (0-100). Default is 100.")
parser = argparse.ArgumentParser(
description="Generate bloated JPEG images with random colors."
)
parser.add_argument(
"-n",
"--num_images",
type=int,
default=1,
help="Number of images to generate. Default is 1.",
)
parser.add_argument(
"-s",
"--size",
type=int,
default=1000,
help="Size of the images (must be 1000 or less and divisible by block size). Default is 1000.",
)
parser.add_argument(
"-c",
"--colors",
nargs="+",
default=["#FF5733", "#33FF57", "#3357FF", "#F3FF33", "#FF33F6", "#33FFF6"],
help="List of colors in hex format. Default is ['#FF5733', '#33FF57', '#3357FF', '#F3FF33', '#FF33F6', '#33FFF6'].",
)
parser.add_argument(
"-b",
"--block_size",
type=int,
default=4,
help="Size of the pixel blocks (must divide the image size evenly). Default is 4.",
)
parser.add_argument(
"-o",
"--output_path",
type=str,
default="bloated_image.jpeg",
help="Base output path for the JPEG images. Default is 'bloated_image.jpeg'.",
)
parser.add_argument(
"-q",
"--quality",
type=int,
default=100,
help="Quality setting for the JPEG images (0-100). Default is 100.",
)
args = parser.parse_args()
@ -74,5 +121,13 @@ if __name__ == "__main__":
# Generate the specified number of images
for i in range(1, args.num_images + 1):
output_path = generate_bloated_jpeg(args.size, args.colors, args.block_size, args.output_path, args.quality, i, folder)
output_path = generate_bloated_jpeg(
args.size,
args.colors,
args.block_size,
args.output_path,
args.quality,
i,
folder,
)
print(f"Image {i} saved to {os.path.abspath(output_path)}")

View File

@ -1,6 +1,7 @@
import random
import sys
import re
import sys
def randomize_numbers(numbers, min_percentage=1, max_percentage=20):
randomized_numbers = []
@ -13,9 +14,10 @@ def randomize_numbers(numbers, min_percentage=1, max_percentage=20):
randomized_numbers.append(new_number)
return randomized_numbers
def parse_input(input_string):
# Replace commas with dots and remove non-numeric characters except dots, commas, and digits
cleaned_input = re.sub(r'[^\d.,\s]', '', input_string).replace(',', '.')
cleaned_input = re.sub(r"[^\d.,\s]", "", input_string).replace(",", ".")
# Split the cleaned input into individual numbers
number_strings = cleaned_input.split()
# Convert the number strings to floats
@ -24,8 +26,8 @@ def parse_input(input_string):
for num in number_strings:
try:
float_num = float(num)
if '.' in num:
digits_count = len(num.split('.')[-1])
if "." in num:
digits_count = len(num.split(".")[-1])
else:
digits_count = 0
numbers.append(float_num)
@ -34,13 +36,16 @@ def parse_input(input_string):
continue
return numbers, decimal_counts
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python random_digits.py <number1> <number2> ... [min_percentage max_percentage]")
print(
"Usage: python random_digits.py <number1> <number2> ... [min_percentage max_percentage]"
)
sys.exit(1)
try:
input_string = ' '.join(sys.argv[1:])
input_string = " ".join(sys.argv[1:])
numbers, decimal_counts = parse_input(input_string)
min_percentage = 1
max_percentage = 20
@ -62,7 +67,7 @@ if __name__ == "__main__":
randomized_numbers = randomize_numbers(numbers, min_percentage, max_percentage)
formatted_numbers = []
for i, num in enumerate(randomized_numbers):
format_str = f'.{decimal_counts[i]}f'
format_str = f".{decimal_counts[i]}f"
formatted_numbers.append(float(format(num, format_str)))
print("Original numbers:", numbers)
@ -71,4 +76,3 @@ if __name__ == "__main__":
print(f"Error: {e}")
print("Please provide valid numbers and percentages.")
sys.exit(1)

View File

@ -1,2 +1,2 @@
selenium
requests
selenium

View File

@ -1,13 +1,16 @@
import os
import requests
import argparse
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
from urllib.parse import urlparse
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
# Initialize argument parser to accept the website URL as an argument
parser = argparse.ArgumentParser(description='Download images from a comic website.')
parser.add_argument('url', type=str, help='The URL of the website to start downloading images from')
parser = argparse.ArgumentParser(description="Download images from a comic website.")
parser.add_argument(
"url", type=str, help="The URL of the website to start downloading images from"
)
args = parser.parse_args()
# Initialize WebDriver (Use the appropriate driver for your browser)
@ -18,6 +21,7 @@ url = args.url
print(f"Opening the website: {url}")
driver.get(url)
# A function to download images by URL
def download_image(url):
# Extract image name from URL
@ -27,13 +31,13 @@ def download_image(url):
if os.path.exists(image_name):
print(f"Image {image_name} already exists, skipping download.")
return False
else:
print(f"Downloading image from URL: {url}")
img_data = requests.get(url).content
with open(image_name, 'wb') as handler:
handler.write(img_data)
print(f'Image {image_name} downloaded successfully')
return True
print(f"Downloading image from URL: {url}")
img_data = requests.get(url).content
with open(image_name, "wb") as handler:
handler.write(img_data)
print(f"Image {image_name} downloaded successfully")
return True
# No need to define a specific number of images now
count = 1
@ -42,10 +46,10 @@ while True:
print(f"Processing image {count}...")
# Find the image element by its ID
image_element = driver.find_element(By.ID, 'cc-comic')
image_element = driver.find_element(By.ID, "cc-comic")
# Get the image URL from the 'src' attribute
image_url = image_element.get_attribute('src')
image_url = image_element.get_attribute("src")
print(f"Found image URL: {image_url}")
# Download the image if it doesn't already exist
@ -54,11 +58,11 @@ while True:
# Try to find the 'Next' button by its class
try:
print(f"Clicking the 'Next' button to load the next image...")
next_button = driver.find_element(By.CSS_SELECTOR, 'a.cc-next')
print("Clicking the 'Next' button to load the next image...")
next_button = driver.find_element(By.CSS_SELECTOR, "a.cc-next")
# Navigate to the URL in the 'href' of the next button
next_button_url = next_button.get_attribute('href')
next_button_url = next_button.get_attribute("href")
driver.get(next_button_url)
except:

View File

@ -1,23 +1,20 @@
#!/usr/bin/env python3
"""
Screen locker with workout verification for Arch Linux / i3wm
"""Screen locker with workout verification for Arch Linux / i3wm
Requires user to log their workout to unlock the screen.
"""
import tkinter as tk
import time
import sys
import re
from datetime import datetime
import json
import os
from datetime import datetime
import sys
import tkinter as tk
class ScreenLocker:
def __init__(self, demo_mode=True):
# Set up log file path
script_dir = os.path.dirname(os.path.abspath(__file__))
self.log_file = os.path.join(script_dir, 'workout_log.json')
self.log_file = os.path.join(script_dir, "workout_log.json")
# Check if already logged today
if self.has_logged_today():
@ -27,7 +24,9 @@ class ScreenLocker:
self.root = tk.Tk()
self.root.title("Workout Locker" + (" [DEMO MODE]" if demo_mode else ""))
self.demo_mode = demo_mode
self.lockout_time = 10 if demo_mode else 1800 # 10 seconds for demo, 30 minutes for production
self.lockout_time = (
10 if demo_mode else 1800
) # 10 seconds for demo, 30 minutes for production
self.workout_data = {}
# Get total screen dimensions across all monitors
@ -38,12 +37,12 @@ class ScreenLocker:
self.root.overrideredirect(True)
# Position window at 0,0 and span all monitors
self.root.geometry(f'{screen_width}x{screen_height}+0+0')
self.root.geometry(f"{screen_width}x{screen_height}+0+0")
# Make window fullscreen and on top
self.root.attributes('-fullscreen', True)
self.root.attributes('-topmost', True)
self.root.configure(bg='#1a1a1a', cursor='arrow')
self.root.attributes("-fullscreen", True)
self.root.attributes("-topmost", True)
self.root.configure(bg="#1a1a1a", cursor="arrow")
if demo_mode:
# Demo mode: only close button allowed
@ -51,17 +50,17 @@ class ScreenLocker:
close_btn = tk.Button(
self.root,
text="✕ Close Demo",
font=('Arial', 12),
bg='#ff4444',
fg='white',
font=("Arial", 12),
bg="#ff4444",
fg="white",
command=self.close,
cursor='hand2'
cursor="hand2",
)
close_btn.place(x=10, y=10)
# Create main container
self.container = tk.Frame(self.root, bg='#1a1a1a')
self.container.place(relx=0.5, rely=0.5, anchor='center')
self.container = tk.Frame(self.root, bg="#1a1a1a")
self.container.place(relx=0.5, rely=0.5, anchor="center")
# Start with initial question
self.ask_workout_done()
@ -81,38 +80,38 @@ class ScreenLocker:
question = tk.Label(
self.container,
text="Did you work out today?",
font=('Arial', 36, 'bold'),
fg='white',
bg='#1a1a1a'
font=("Arial", 36, "bold"),
fg="white",
bg="#1a1a1a",
)
question.pack(pady=30)
button_frame = tk.Frame(self.container, bg='#1a1a1a')
button_frame = tk.Frame(self.container, bg="#1a1a1a")
button_frame.pack(pady=20)
yes_btn = tk.Button(
button_frame,
text="YES",
font=('Arial', 24, 'bold'),
bg='#00aa00',
fg='white',
font=("Arial", 24, "bold"),
bg="#00aa00",
fg="white",
width=10,
command=self.ask_workout_type,
cursor='hand2' if self.demo_mode else ''
cursor="hand2" if self.demo_mode else "",
)
yes_btn.pack(side='left', padx=20)
yes_btn.pack(side="left", padx=20)
no_btn = tk.Button(
button_frame,
text="NO",
font=('Arial', 24, 'bold'),
bg='#aa0000',
fg='white',
font=("Arial", 24, "bold"),
bg="#aa0000",
fg="white",
width=10,
command=self.lockout,
cursor='hand2' if self.demo_mode else ''
cursor="hand2" if self.demo_mode else "",
)
no_btn.pack(side='left', padx=20)
no_btn.pack(side="left", padx=20)
def lockout(self):
self.clear_container()
@ -120,18 +119,18 @@ class ScreenLocker:
self.lockout_label = tk.Label(
self.container,
text=f"Go work out!\nLocked for {self.lockout_time} seconds",
font=('Arial', 48, 'bold'),
fg='#ff4444',
bg='#1a1a1a'
font=("Arial", 48, "bold"),
fg="#ff4444",
bg="#1a1a1a",
)
self.lockout_label.pack(pady=30)
self.countdown_label = tk.Label(
self.container,
text=str(self.lockout_time),
font=('Arial', 120, 'bold'),
fg='white',
bg='#1a1a1a'
font=("Arial", 120, "bold"),
fg="white",
bg="#1a1a1a",
)
self.countdown_label.pack(pady=30)
@ -152,92 +151,106 @@ class ScreenLocker:
question = tk.Label(
self.container,
text="What type of workout?",
font=('Arial', 36, 'bold'),
fg='white',
bg='#1a1a1a'
font=("Arial", 36, "bold"),
fg="white",
bg="#1a1a1a",
)
question.pack(pady=30)
button_frame = tk.Frame(self.container, bg='#1a1a1a')
button_frame = tk.Frame(self.container, bg="#1a1a1a")
button_frame.pack(pady=20)
running_btn = tk.Button(
button_frame,
text="RUNNING",
font=('Arial', 24, 'bold'),
bg='#0066cc',
fg='white',
font=("Arial", 24, "bold"),
bg="#0066cc",
fg="white",
width=15,
command=self.ask_running_details,
cursor='hand2' if self.demo_mode else ''
cursor="hand2" if self.demo_mode else "",
)
running_btn.pack(side='left', padx=20)
running_btn.pack(side="left", padx=20)
strength_btn = tk.Button(
button_frame,
text="STRENGTH",
font=('Arial', 24, 'bold'),
bg='#cc6600',
fg='white',
font=("Arial", 24, "bold"),
bg="#cc6600",
fg="white",
width=15,
command=self.ask_strength_details,
cursor='hand2' if self.demo_mode else ''
cursor="hand2" if self.demo_mode else "",
)
strength_btn.pack(side='left', padx=20)
strength_btn.pack(side="left", padx=20)
def ask_running_details(self):
self.clear_container()
self.workout_data['type'] = 'running'
self.workout_data["type"] = "running"
title = tk.Label(
self.container,
text="Running Details",
font=('Arial', 36, 'bold'),
fg='white',
bg='#1a1a1a'
font=("Arial", 36, "bold"),
fg="white",
bg="#1a1a1a",
)
title.pack(pady=20)
# Distance
dist_frame = tk.Frame(self.container, bg='#1a1a1a')
dist_frame = tk.Frame(self.container, bg="#1a1a1a")
dist_frame.pack(pady=10)
tk.Label(dist_frame, text="Distance (km):", font=('Arial', 20), fg='white', bg='#1a1a1a').pack(side='left', padx=10)
self.distance_entry = tk.Entry(dist_frame, font=('Arial', 20), width=10)
self.distance_entry.pack(side='left', padx=10)
tk.Label(
dist_frame,
text="Distance (km):",
font=("Arial", 20),
fg="white",
bg="#1a1a1a",
).pack(side="left", padx=10)
self.distance_entry = tk.Entry(dist_frame, font=("Arial", 20), width=10)
self.distance_entry.pack(side="left", padx=10)
# Time
time_frame = tk.Frame(self.container, bg='#1a1a1a')
time_frame = tk.Frame(self.container, bg="#1a1a1a")
time_frame.pack(pady=10)
tk.Label(time_frame, text="Time (minutes):", font=('Arial', 20), fg='white', bg='#1a1a1a').pack(side='left', padx=10)
self.time_entry = tk.Entry(time_frame, font=('Arial', 20), width=10)
self.time_entry.pack(side='left', padx=10)
tk.Label(
time_frame,
text="Time (minutes):",
font=("Arial", 20),
fg="white",
bg="#1a1a1a",
).pack(side="left", padx=10)
self.time_entry = tk.Entry(time_frame, font=("Arial", 20), width=10)
self.time_entry.pack(side="left", padx=10)
# Pace
pace_frame = tk.Frame(self.container, bg='#1a1a1a')
pace_frame = tk.Frame(self.container, bg="#1a1a1a")
pace_frame.pack(pady=10)
tk.Label(pace_frame, text="Pace (min/km):", font=('Arial', 20), fg='white', bg='#1a1a1a').pack(side='left', padx=10)
self.pace_entry = tk.Entry(pace_frame, font=('Arial', 20), width=10)
self.pace_entry.pack(side='left', padx=10)
tk.Label(
pace_frame,
text="Pace (min/km):",
font=("Arial", 20),
fg="white",
bg="#1a1a1a",
).pack(side="left", padx=10)
self.pace_entry = tk.Entry(pace_frame, font=("Arial", 20), width=10)
self.pace_entry.pack(side="left", padx=10)
# Timer countdown label
self.timer_label = tk.Label(
self.container,
text="",
font=('Arial', 16),
fg='#ffaa00',
bg='#1a1a1a'
self.container, text="", font=("Arial", 16), fg="#ffaa00", bg="#1a1a1a"
)
self.timer_label.pack(pady=10)
self.submit_btn = tk.Button(
self.container,
text="SUBMIT (locked)",
font=('Arial', 24, 'bold'),
bg='#666666',
fg='white',
font=("Arial", 24, "bold"),
bg="#666666",
fg="white",
width=15,
state='disabled',
cursor='hand2' if self.demo_mode else ''
state="disabled",
cursor="hand2" if self.demo_mode else "",
)
self.submit_btn.pack(pady=10)
@ -245,12 +258,12 @@ class ScreenLocker:
back_btn = tk.Button(
self.container,
text="← BACK",
font=('Arial', 18),
bg='#666666',
fg='white',
font=("Arial", 18),
bg="#666666",
fg="white",
width=15,
command=self.ask_workout_type,
cursor='hand2' if self.demo_mode else ''
cursor="hand2" if self.demo_mode else "",
)
back_btn.pack(pady=10)
@ -285,7 +298,9 @@ class ScreenLocker:
tolerance = expected_pace * 0.15 # 15% tolerance
if pace_diff > tolerance:
self.show_error(f"Pace doesn't match! Expected ~{expected_pace:.2f} min/km, got {pace:.2f}")
self.show_error(
f"Pace doesn't match! Expected ~{expected_pace:.2f} min/km, got {pace:.2f}"
)
return
# Data looks good
@ -296,71 +311,97 @@ class ScreenLocker:
def ask_strength_details(self):
self.clear_container()
self.workout_data['type'] = 'strength'
self.workout_data["type"] = "strength"
title = tk.Label(
self.container,
text="Strength Training Details",
font=('Arial', 36, 'bold'),
fg='white',
bg='#1a1a1a'
font=("Arial", 36, "bold"),
fg="white",
bg="#1a1a1a",
)
title.pack(pady=20)
# Exercises
ex_frame = tk.Frame(self.container, bg='#1a1a1a')
ex_frame = tk.Frame(self.container, bg="#1a1a1a")
ex_frame.pack(pady=10)
tk.Label(ex_frame, text="Exercises (comma-separated):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10)
self.exercises_entry = tk.Entry(ex_frame, font=('Arial', 18), width=30)
self.exercises_entry.pack(side='left', padx=10)
tk.Label(
ex_frame,
text="Exercises (comma-separated):",
font=("Arial", 18),
fg="white",
bg="#1a1a1a",
).pack(side="left", padx=10)
self.exercises_entry = tk.Entry(ex_frame, font=("Arial", 18), width=30)
self.exercises_entry.pack(side="left", padx=10)
# Sets per exercise
sets_frame = tk.Frame(self.container, bg='#1a1a1a')
sets_frame = tk.Frame(self.container, bg="#1a1a1a")
sets_frame.pack(pady=10)
tk.Label(sets_frame, text="Sets per exercise (comma-separated):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10)
self.sets_entry = tk.Entry(sets_frame, font=('Arial', 18), width=20)
self.sets_entry.pack(side='left', padx=10)
tk.Label(
sets_frame,
text="Sets per exercise (comma-separated):",
font=("Arial", 18),
fg="white",
bg="#1a1a1a",
).pack(side="left", padx=10)
self.sets_entry = tk.Entry(sets_frame, font=("Arial", 18), width=20)
self.sets_entry.pack(side="left", padx=10)
# Reps per set
reps_frame = tk.Frame(self.container, bg='#1a1a1a')
reps_frame = tk.Frame(self.container, bg="#1a1a1a")
reps_frame.pack(pady=10)
tk.Label(reps_frame, text="Reps per set (comma-separated):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10)
self.reps_entry = tk.Entry(reps_frame, font=('Arial', 18), width=20)
self.reps_entry.pack(side='left', padx=10)
tk.Label(
reps_frame,
text="Reps per set (comma-separated):",
font=("Arial", 18),
fg="white",
bg="#1a1a1a",
).pack(side="left", padx=10)
self.reps_entry = tk.Entry(reps_frame, font=("Arial", 18), width=20)
self.reps_entry.pack(side="left", padx=10)
# Weights
weights_frame = tk.Frame(self.container, bg='#1a1a1a')
weights_frame = tk.Frame(self.container, bg="#1a1a1a")
weights_frame.pack(pady=10)
tk.Label(weights_frame, text="Weight per exercise in kg (comma-separated):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10)
self.weights_entry = tk.Entry(weights_frame, font=('Arial', 18), width=20)
self.weights_entry.pack(side='left', padx=10)
tk.Label(
weights_frame,
text="Weight per exercise in kg (comma-separated):",
font=("Arial", 18),
fg="white",
bg="#1a1a1a",
).pack(side="left", padx=10)
self.weights_entry = tk.Entry(weights_frame, font=("Arial", 18), width=20)
self.weights_entry.pack(side="left", padx=10)
# Total weight lifted
total_frame = tk.Frame(self.container, bg='#1a1a1a')
total_frame = tk.Frame(self.container, bg="#1a1a1a")
total_frame.pack(pady=10)
tk.Label(total_frame, text="Total weight lifted (kg):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10)
self.total_weight_entry = tk.Entry(total_frame, font=('Arial', 18), width=15)
self.total_weight_entry.pack(side='left', padx=10)
tk.Label(
total_frame,
text="Total weight lifted (kg):",
font=("Arial", 18),
fg="white",
bg="#1a1a1a",
).pack(side="left", padx=10)
self.total_weight_entry = tk.Entry(total_frame, font=("Arial", 18), width=15)
self.total_weight_entry.pack(side="left", padx=10)
# Timer countdown label
self.timer_label = tk.Label(
self.container,
text="",
font=('Arial', 16),
fg='#ffaa00',
bg='#1a1a1a'
self.container, text="", font=("Arial", 16), fg="#ffaa00", bg="#1a1a1a"
)
self.timer_label.pack(pady=10)
self.submit_btn = tk.Button(
self.container,
text="SUBMIT (locked)",
font=('Arial', 24, 'bold'),
bg='#666666',
fg='white',
font=("Arial", 24, "bold"),
bg="#666666",
fg="white",
width=15,
state='disabled',
cursor='hand2' if self.demo_mode else ''
state="disabled",
cursor="hand2" if self.demo_mode else "",
)
self.submit_btn.pack(pady=10)
@ -368,32 +409,40 @@ class ScreenLocker:
back_btn = tk.Button(
self.container,
text="← BACK",
font=('Arial', 18),
bg='#666666',
fg='white',
font=("Arial", 18),
bg="#666666",
fg="white",
width=15,
command=self.ask_workout_type,
cursor='hand2' if self.demo_mode else ''
cursor="hand2" if self.demo_mode else "",
)
back_btn.pack(pady=10)
# Start 30 second timer
self.submit_unlock_time = 30
self.entries_to_check = [self.exercises_entry, self.sets_entry, self.reps_entry, self.weights_entry, self.total_weight_entry]
self.entries_to_check = [
self.exercises_entry,
self.sets_entry,
self.reps_entry,
self.weights_entry,
self.total_weight_entry,
]
self.submit_command = self.verify_strength_data
self.update_submit_timer()
def verify_strength_data(self):
try:
exercises = [e.strip() for e in self.exercises_entry.get().split(',')]
sets = [int(s.strip()) for s in self.sets_entry.get().split(',')]
reps = [int(r.strip()) for r in self.reps_entry.get().split(',')]
weights = [float(w.strip()) for w in self.weights_entry.get().split(',')]
exercises = [e.strip() for e in self.exercises_entry.get().split(",")]
sets = [int(s.strip()) for s in self.sets_entry.get().split(",")]
reps = [int(r.strip()) for r in self.reps_entry.get().split(",")]
weights = [float(w.strip()) for w in self.weights_entry.get().split(",")]
total_weight = float(self.total_weight_entry.get())
# Check all lists have same length
if not (len(exercises) == len(sets) == len(reps) == len(weights)):
self.show_error("Number of exercises, sets, reps, and weights must match")
self.show_error(
"Number of exercises, sets, reps, and weights must match"
)
return
# Check for empty or lazy entries
@ -415,12 +464,16 @@ class ScreenLocker:
return
# Calculate expected total weight
expected_total = sum(sets[i] * reps[i] * weights[i] for i in range(len(exercises)))
expected_total = sum(
sets[i] * reps[i] * weights[i] for i in range(len(exercises))
)
weight_diff = abs(total_weight - expected_total)
tolerance = expected_total * 0.15 # 15% tolerance
if weight_diff > tolerance:
self.show_error(f"Total weight doesn't match! Expected ~{expected_total:.1f} kg, got {total_weight:.1f}")
self.show_error(
f"Total weight doesn't match! Expected ~{expected_total:.1f} kg, got {total_weight:.1f}"
)
return
# Data looks good
@ -434,7 +487,9 @@ class ScreenLocker:
# Check if widgets still exist (user might have clicked back)
try:
if self.submit_unlock_time > 0:
self.timer_label.config(text=f"Submit available in {self.submit_unlock_time} seconds...")
self.timer_label.config(
text=f"Submit available in {self.submit_unlock_time} seconds..."
)
self.submit_unlock_time -= 1
self.root.after(1000, self.update_submit_timer)
else:
@ -445,9 +500,9 @@ class ScreenLocker:
# Enable submit button
self.submit_btn.config(
text="SUBMIT",
state='normal',
bg='#00aa00',
command=self.submit_command
state="normal",
bg="#00aa00",
command=self.submit_command,
)
self.timer_label.config(text="You can now submit!")
else:
@ -466,9 +521,9 @@ class ScreenLocker:
if all_filled:
self.submit_btn.config(
text="SUBMIT",
state='normal',
bg='#00aa00',
command=self.submit_command
state="normal",
bg="#00aa00",
command=self.submit_command,
)
self.timer_label.config(text="You can now submit!")
else:
@ -484,31 +539,31 @@ class ScreenLocker:
error_label = tk.Label(
self.container,
text="ERROR",
font=('Arial', 48, 'bold'),
fg='#ff4444',
bg='#1a1a1a'
font=("Arial", 48, "bold"),
fg="#ff4444",
bg="#1a1a1a",
)
error_label.pack(pady=20)
msg_label = tk.Label(
self.container,
text=message,
font=('Arial', 24),
fg='white',
bg='#1a1a1a',
wraplength=800
font=("Arial", 24),
fg="white",
bg="#1a1a1a",
wraplength=800,
)
msg_label.pack(pady=20)
retry_btn = tk.Button(
self.container,
text="TRY AGAIN",
font=('Arial', 24, 'bold'),
bg='#0066cc',
fg='white',
font=("Arial", 24, "bold"),
bg="#0066cc",
fg="white",
width=15,
command=self.ask_workout_done,
cursor='hand2' if self.demo_mode else ''
cursor="hand2" if self.demo_mode else "",
)
retry_btn.pack(pady=30)
@ -521,18 +576,18 @@ class ScreenLocker:
success_label = tk.Label(
self.container,
text="Great job! 💪",
font=('Arial', 48, 'bold'),
fg='#00ff00',
bg='#1a1a1a'
font=("Arial", 48, "bold"),
fg="#00ff00",
bg="#1a1a1a",
)
success_label.pack(pady=30)
unlock_label = tk.Label(
self.container,
text="Screen Unlocked!",
font=('Arial', 36),
fg='white',
bg='#1a1a1a'
font=("Arial", 36),
fg="white",
bg="#1a1a1a",
)
unlock_label.pack(pady=20)
@ -544,12 +599,12 @@ class ScreenLocker:
return False
try:
with open(self.log_file, 'r') as f:
with open(self.log_file) as f:
logs = json.load(f)
today = datetime.now().strftime('%Y-%m-%d')
today = datetime.now().strftime("%Y-%m-%d")
return today in logs
except (json.JSONDecodeError, IOError):
except (OSError, json.JSONDecodeError):
return False
def save_workout_log(self):
@ -558,23 +613,23 @@ class ScreenLocker:
logs = {}
if os.path.exists(self.log_file):
try:
with open(self.log_file, 'r') as f:
with open(self.log_file) as f:
logs = json.load(f)
except (json.JSONDecodeError, IOError):
except (OSError, json.JSONDecodeError):
logs = {}
# Add today's workout
today = datetime.now().strftime('%Y-%m-%d')
today = datetime.now().strftime("%Y-%m-%d")
logs[today] = {
'timestamp': datetime.now().isoformat(),
'workout_data': self.workout_data
"timestamp": datetime.now().isoformat(),
"workout_data": self.workout_data,
}
# Save updated logs
try:
with open(self.log_file, 'w') as f:
with open(self.log_file, "w") as f:
json.dump(logs, f, indent=2)
except IOError as e:
except OSError as e:
print(f"Warning: Could not save workout log: {e}")
def close(self):
@ -585,11 +640,11 @@ class ScreenLocker:
self.root.mainloop()
if __name__ == '__main__':
if __name__ == "__main__":
# Check for --production flag
demo_mode = True # Default to demo mode for safety
if len(sys.argv) > 1 and sys.argv[1] == '--production':
if len(sys.argv) > 1 and sys.argv[1] == "--production":
demo_mode = False
locker = ScreenLocker(demo_mode=demo_mode)

View File

@ -0,0 +1,20 @@
{
"2025-11-27": {
"timestamp": "2025-11-27T19:45:38.894904",
"workout_data": {
"type": "strength"
}
},
"2025-11-28": {
"timestamp": "2025-11-28T12:46:09.077724",
"workout_data": {
"type": "strength"
}
},
"2025-11-30": {
"timestamp": "2025-11-30T12:12:44.884093",
"workout_data": {
"type": "strength"
}
}
}

View File

@ -1,6 +1,5 @@
def calculate_symmetric_weights(N, middle_weight, factors=None):
"""
Calculate symmetric weights for both even and odd N.
"""Calculate symmetric weights for both even and odd N.
N: Number in which to split.
middle_weight: The middle value for symmetry.
@ -25,9 +24,9 @@ def calculate_symmetric_weights(N, middle_weight, factors=None):
return weights
def scale_to_total(X, weights):
"""
Scale the weights so that their sum is proportional to X.
"""Scale the weights so that their sum is proportional to X.
X: Total value to distribute.
weights: The list of weights to be scaled.
@ -38,9 +37,9 @@ def scale_to_total(X, weights):
return distances
def split_x_into_n_symmetrically(X, N, factors):
"""
X: Total value to distribute.
"""X: Total value to distribute.
N: Number in which we split.
factors: List controlling the difference in weights between consecutive days.
Must have length of N // 2 (if N is odd) or (N // 2 - 1) (if N is even).
@ -48,9 +47,9 @@ def split_x_into_n_symmetrically(X, N, factors):
weights = calculate_symmetric_weights(N, middle_weight=1, factors=factors)
return scale_to_total(X, weights)
def split_x_into_n_middle(X, N, middle_value):
"""
X: Total value to distribute.
"""X: Total value to distribute.
N: Number in which we split.
middle_value: Value of the middle number (the biggest weight).
"""

View File

@ -21,9 +21,11 @@ python3 PYTHON/analyze_chess_game.py lichess_bot_game_8GSdY3Ci.log
```
Options:
- `--engine /path/to/stockfish` to specify a custom engine path
- `--time 0.2` seconds per evaluation (default)
- `--depth 12` fixed depth instead of time
The script prints a table with, for each ply:
- side to move, SAN move, eval before/after from mover's POV, delta, classification, and Stockfish best move suggestion.

217
PYTHON/stockfish_analysis/analyze_chess_game.py Normal file → Executable file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3
"""
Analyze a chess game's moves using a local Stockfish engine and rate each move.
"""Analyze a chess game's moves using a local Stockfish engine and rate each move.
Usage:
python3 PYTHON/analyze_chess_game.py <path-to-file>
@ -22,11 +21,10 @@ from __future__ import annotations
import argparse
import io
import multiprocessing
import os
import re
import sys
from typing import Optional, Tuple
import multiprocessing
try:
import psutil # type: ignore
@ -37,13 +35,15 @@ try:
import chess
import chess.engine
import chess.pgn
except Exception as e: # pragma: no cover
except Exception: # pragma: no cover
print("Missing dependency. Please install python-chess:", file=sys.stderr)
print(" pip install -r PYTHON/stockfish_analysis/requirements.txt", file=sys.stderr)
print(
" pip install -r PYTHON/stockfish_analysis/requirements.txt", file=sys.stderr
)
raise
def extract_pgn_text(raw: str) -> Optional[str]:
def extract_pgn_text(raw: str) -> str | None:
"""Try to extract a PGN block from a possibly noisy file.
Strategies tried in order:
@ -79,7 +79,9 @@ def extract_pgn_text(raw: str) -> Optional[str]:
return None
def score_to_cp(score: chess.engine.PovScore, pov_white: bool) -> Tuple[Optional[int], Optional[int]]:
def score_to_cp(
score: chess.engine.PovScore, pov_white: bool
) -> tuple[int | None, int | None]:
"""Return tuple (cp, mate_in) from a PovScore for the given POV color.
If it's a mate score, cp will be None and mate_in will be +/-N (positive means mate for POV side).
@ -93,7 +95,7 @@ def score_to_cp(score: chess.engine.PovScore, pov_white: bool) -> Tuple[Optional
return s.score(mate_score=None), None
def classify_cp_loss(cp_loss: Optional[int]) -> str:
def classify_cp_loss(cp_loss: int | None) -> str:
"""Classify move quality using Lichess-like centipawn loss bands.
Loss is best_eval(cp) - played_eval(cp), from the mover's POV (positive is worse).
@ -120,7 +122,7 @@ def classify_cp_loss(cp_loss: Optional[int]) -> str:
return "Blunder"
def fmt_eval(cp: Optional[int], mate_in: Optional[int]) -> str:
def fmt_eval(cp: int | None, mate_in: int | None) -> str:
if mate_in is not None:
sign = "+" if mate_in > 0 else ""
return f"M{sign}{mate_in}"
@ -130,7 +132,7 @@ def fmt_eval(cp: Optional[int], mate_in: Optional[int]) -> str:
return f"{cp/100.0:+.2f}"
def _parse_threads(value: str) -> Optional[int]:
def _parse_threads(value: str) -> int | None:
v = value.strip().lower()
if v in ("auto", "max", ""): # auto-detect
return None
@ -141,7 +143,7 @@ def _parse_threads(value: str) -> Optional[int]:
raise argparse.ArgumentTypeError("--threads must be an integer or 'auto'")
def _parse_hash_mb(value: str) -> Optional[int]:
def _parse_hash_mb(value: str) -> int | None:
v = value.strip().lower()
if v in ("auto", "max", ""): # auto-detect
return None
@ -152,7 +154,7 @@ def _parse_hash_mb(value: str) -> Optional[int]:
raise argparse.ArgumentTypeError("--hash-mb must be an integer (MB) or 'auto'")
def _detect_total_mem_mb() -> Optional[int]:
def _detect_total_mem_mb() -> int | None:
# Prefer psutil if available
if psutil is not None:
try:
@ -161,7 +163,7 @@ def _detect_total_mem_mb() -> Optional[int]:
pass
# Fallback: Linux /proc/meminfo
try:
with open("/proc/meminfo", "r", encoding="utf-8", errors="ignore") as f:
with open("/proc/meminfo", encoding="utf-8", errors="ignore") as f:
for line in f:
if line.startswith("MemTotal:"):
parts = line.split()
@ -183,7 +185,7 @@ def _auto_hash_mb(threads_wanted: int, engine_options) -> int:
opt = engine_options.get("Hash")
max_allowed = None
try:
max_allowed = getattr(opt, "max") if opt is not None else None
max_allowed = opt.max if opt is not None else None
except Exception:
max_allowed = None
if isinstance(max_allowed, int):
@ -195,27 +197,61 @@ def _auto_hash_mb(threads_wanted: int, engine_options) -> int:
def main():
ap = argparse.ArgumentParser(description="Analyze a chess game's moves with Stockfish and rate each move.")
ap = argparse.ArgumentParser(
description="Analyze a chess game's moves with Stockfish and rate each move."
)
ap.add_argument("file", help="Path to a PGN file or a log containing a PGN section")
ap.add_argument("--engine", default="stockfish", help="Path to stockfish executable (default: stockfish)")
ap.add_argument(
"--engine",
default="stockfish",
help="Path to stockfish executable (default: stockfish)",
)
# Exactly one of time or depth may be provided; default to time
ap.add_argument("--time", type=float, default=0.5, help="Analysis time per evaluation in seconds (default: 0.5)")
ap.add_argument("--depth", type=int, default=None, help="Fixed depth per evaluation (overrides --time)")
ap.add_argument(
"--time",
type=float,
default=0.5,
help="Analysis time per evaluation in seconds (default: 0.5)",
)
ap.add_argument(
"--depth",
type=int,
default=None,
help="Fixed depth per evaluation (overrides --time)",
)
# Performance knobs
ap.add_argument("--threads", type=_parse_threads, default=None, metavar="auto|N",
help="Engine threads to use (default: auto = all logical cores)")
ap.add_argument("--hash-mb", type=_parse_hash_mb, default=None, metavar="auto|MB",
help="Hash table size in MB (default: auto = up to half RAM, capped)")
ap.add_argument("--multipv", type=int, default=2, help="Number of principal variations to compute (default: 1)")
ap.add_argument("--last-move-only", action="store_true",
help="Analyze only the last move of the main line (reports its eval and the best move)")
ap.add_argument(
"--threads",
type=_parse_threads,
default=None,
metavar="auto|N",
help="Engine threads to use (default: auto = all logical cores)",
)
ap.add_argument(
"--hash-mb",
type=_parse_hash_mb,
default=None,
metavar="auto|MB",
help="Hash table size in MB (default: auto = up to half RAM, capped)",
)
ap.add_argument(
"--multipv",
type=int,
default=2,
help="Number of principal variations to compute (default: 1)",
)
ap.add_argument(
"--last-move-only",
action="store_true",
help="Analyze only the last move of the main line (reports its eval and the best move)",
)
args = ap.parse_args()
if not os.path.isfile(args.file):
print(f"Input not found: {args.file}", file=sys.stderr)
sys.exit(1)
with open(args.file, "r", encoding="utf-8", errors="replace") as f:
with open(args.file, encoding="utf-8", errors="replace") as f:
raw = f.read()
pgn_text = extract_pgn_text(raw)
@ -233,7 +269,10 @@ def main():
engine = chess.engine.SimpleEngine.popen_uci([args.engine])
except FileNotFoundError:
print(f"Could not launch engine at: {args.engine}", file=sys.stderr)
print("Ensure Stockfish is installed and in PATH, or specify with --engine.", file=sys.stderr)
print(
"Ensure Stockfish is installed and in PATH, or specify with --engine.",
file=sys.stderr,
)
sys.exit(4)
# Configure engine performance options if available
@ -243,7 +282,9 @@ def main():
options = {}
# Threads
wanted_threads = args.threads if args.threads is not None else (multiprocessing.cpu_count() or 1)
wanted_threads = (
args.threads if args.threads is not None else (multiprocessing.cpu_count() or 1)
)
# Respect engine bounds if present
if "Threads" in options:
try:
@ -307,18 +348,26 @@ def main():
result = game.headers.get("Result", "*")
print(f" {white} vs {black} Result: {result}")
print()
print("Columns: ply side move played_eval best_eval loss class best_suggestion")
print(
"Columns: ply side move played_eval best_eval loss class best_suggestion"
)
# Brief performance summary (best-effort)
try:
thr_show = int(wanted_threads)
except Exception:
thr_show = 1
try:
hash_show = int(engine.options.get("Hash").value) if hasattr(engine, "options") and engine.options.get("Hash") else None
hash_show = (
int(engine.options.get("Hash").value)
if hasattr(engine, "options") and engine.options.get("Hash")
else None
)
except Exception:
hash_show = None
if hash_show is not None:
print(f"Using engine options: Threads={thr_show}, Hash={hash_show} MB, MultiPV={effective_mpv}")
print(
f"Using engine options: Threads={thr_show}, Hash={hash_show} MB, MultiPV={effective_mpv}"
)
else:
print(f"Using engine options: Threads={thr_show}, MultiPV={effective_mpv}")
@ -339,10 +388,20 @@ def main():
# If this is the final move in the mainline, analyze it and stop.
if not move_node.variations:
# Analyse current position to get engine best move suggestion
info_root_raw = engine.analyse(board, limit=limit, multipv=effective_mpv)
info_root = info_root_raw[0] if isinstance(info_root_raw, list) else info_root_raw
info_root_raw = engine.analyse(
board, limit=limit, multipv=effective_mpv
)
info_root = (
info_root_raw[0]
if isinstance(info_root_raw, list)
else info_root_raw
)
best_move = None
if info_root is not None and "pv" in info_root and info_root["pv"]:
if (
info_root is not None
and "pv" in info_root
and info_root["pv"]
):
best_move = info_root["pv"][0]
if best_move is None:
res = engine.play(board, limit)
@ -353,29 +412,47 @@ def main():
# Evaluate played move
board_played = board.copy()
board_played.push(move)
info_played_raw = engine.analyse(board_played, limit=limit, multipv=effective_mpv)
info_played = info_played_raw[0] if isinstance(info_played_raw, list) else info_played_raw
info_played_raw = engine.analyse(
board_played, limit=limit, multipv=effective_mpv
)
info_played = (
info_played_raw[0]
if isinstance(info_played_raw, list)
else info_played_raw
)
if info_played is None or "score" not in info_played:
played_cp, played_mate = None, None
else:
played_cp, played_mate = score_to_cp(info_played["score"], pov_white=mover_white)
played_cp, played_mate = score_to_cp(
info_played["score"], pov_white=mover_white
)
# Evaluate best move position (for mover POV)
best_san = board.san(best_move) if best_move is not None else "?"
best_san = (
board.san(best_move) if best_move is not None else "?"
)
if best_move is not None:
board_best = board.copy()
board_best.push(best_move)
info_best_raw = engine.analyse(board_best, limit=limit, multipv=effective_mpv)
info_best = info_best_raw[0] if isinstance(info_best_raw, list) else info_best_raw
info_best_raw = engine.analyse(
board_best, limit=limit, multipv=effective_mpv
)
info_best = (
info_best_raw[0]
if isinstance(info_best_raw, list)
else info_best_raw
)
if info_best is None or "score" not in info_best:
best_cp, best_mate = None, None
else:
best_cp, best_mate = score_to_cp(info_best["score"], pov_white=mover_white)
best_cp, best_mate = score_to_cp(
info_best["score"], pov_white=mover_white
)
else:
best_cp, best_mate = None, None
# Compute loss/classification
cp_loss: Optional[int] = None
cp_loss: int | None = None
classification = "Unknown"
if best_mate is not None or played_mate is not None:
if best_mate is not None and played_mate is not None:
@ -397,10 +474,9 @@ def main():
classification = "Blunder"
else:
classification = "Blunder"
else:
if best_cp is not None and played_cp is not None:
cp_loss = max(0, best_cp - played_cp)
classification = classify_cp_loss(cp_loss)
elif best_cp is not None and played_cp is not None:
cp_loss = max(0, best_cp - played_cp)
classification = classify_cp_loss(cp_loss)
side = "W" if mover_white else "B"
print(
@ -422,8 +498,14 @@ def main():
mover_white = board.turn
# Analyse position to get engine best move suggestion
info_root_raw = engine.analyse(board, limit=limit, multipv=effective_mpv)
info_root = info_root_raw[0] if isinstance(info_root_raw, list) else info_root_raw
info_root_raw = engine.analyse(
board, limit=limit, multipv=effective_mpv
)
info_root = (
info_root_raw[0]
if isinstance(info_root_raw, list)
else info_root_raw
)
best_move = None
if info_root is not None and "pv" in info_root and info_root["pv"]:
best_move = info_root["pv"][0]
@ -436,29 +518,45 @@ def main():
san = board.san(move)
board_played = board.copy()
board_played.push(move)
info_played_raw = engine.analyse(board_played, limit=limit, multipv=effective_mpv)
info_played = info_played_raw[0] if isinstance(info_played_raw, list) else info_played_raw
info_played_raw = engine.analyse(
board_played, limit=limit, multipv=effective_mpv
)
info_played = (
info_played_raw[0]
if isinstance(info_played_raw, list)
else info_played_raw
)
if info_played is None or "score" not in info_played:
played_cp, played_mate = None, None
else:
played_cp, played_mate = score_to_cp(info_played["score"], pov_white=mover_white)
played_cp, played_mate = score_to_cp(
info_played["score"], pov_white=mover_white
)
# Evaluate best move position (for mover POV)
best_san = board.san(best_move) if best_move is not None else "?"
if best_move is not None:
board_best = board.copy()
board_best.push(best_move)
info_best_raw = engine.analyse(board_best, limit=limit, multipv=effective_mpv)
info_best = info_best_raw[0] if isinstance(info_best_raw, list) else info_best_raw
info_best_raw = engine.analyse(
board_best, limit=limit, multipv=effective_mpv
)
info_best = (
info_best_raw[0]
if isinstance(info_best_raw, list)
else info_best_raw
)
if info_best is None or "score" not in info_best:
best_cp, best_mate = None, None
else:
best_cp, best_mate = score_to_cp(info_best["score"], pov_white=mover_white)
best_cp, best_mate = score_to_cp(
info_best["score"], pov_white=mover_white
)
else:
best_cp, best_mate = None, None
# Compute centipawn loss bands
cp_loss: Optional[int] = None
cp_loss: int | None = None
classification = "Unknown"
# Handle mate cases first
if best_mate is not None or played_mate is not None:
@ -486,10 +584,9 @@ def main():
else:
# Losing a forced mate or missing one
classification = "Blunder"
else:
if best_cp is not None and played_cp is not None:
cp_loss = max(0, best_cp - played_cp)
classification = classify_cp_loss(cp_loss)
elif best_cp is not None and played_cp is not None:
cp_loss = max(0, best_cp - played_cp)
classification = classify_cp_loss(cp_loss)
side = "W" if mover_white else "B"
print(

View File

@ -1,2 +1,2 @@
python-chess>=1.999
psutil>=5.9
python-chess>=1.999

View File

@ -1,14 +1,15 @@
# tagDivider
Python script creating two directories, showing images in the script directory and putting those images into one of those directories depending on user input
How to use:
1. Install opencv for python3
a) Linux: sudo apt-get install python3-opencv
2. Put the script into whatever folder you have images in
3. Run the script
python3 tagDivider.py
4. Enter folders names in terminal
5. Click "a" or "d" accordingly to folder you want image to be in
1. Install opencv for python3
a) Linux: sudo apt-get install python3-opencv
2. Put the script into whatever folder you have images in
3. Run the script
python3 tagDivider.py
4. Enter folders names in terminal
5. Click "a" or "d" accordingly to folder you want image to be in
If you want to change default buttons just modify script

View File

@ -1,35 +1,72 @@
import os # for: os.getcwd; os.mkdir; os.listdir;
from os import path # for: os.path.abspath
import shutil # for: shutil.move
import cv2 # for: cv2.imread; cv2.namedWindow; cv2.imshow; cv2.waitKey; cv2.destroyAllWindows; cv2.IMREAD_COLOR
IMAGE_EXTENSION = (".bmp", ".dib", ".jpeg", ".jpg", ".jpe", ".jp2", ".png", ".pbm", ".pgm", ".ppm", ".pxm", ".pnm", ".pfm", ".sr", ".ras", ".tiff", ".tif", ".exr", ".hdr", ".pic") # Stolen from here: https://docs.opencv.org/4.5.2/d4/da8/group__imgcodecs.html I didn't include .webp because if the image is animated shit does not work
LEFT_FOLDER_CODE = 100 # Default 100 - 'd'
RIGHT_FOLDER_CODE = 97 # Default 97 - 'a'
import os # for: os.getcwd; os.mkdir; os.listdir;
from os import path # for: os.path.abspath
import shutil # for: shutil.move
import cv2 # for: cv2.imread; cv2.namedWindow; cv2.imshow; cv2.waitKey; cv2.destroyAllWindows; cv2.IMREAD_COLOR
IMAGE_EXTENSION = (
".bmp",
".dib",
".jpeg",
".jpg",
".jpe",
".jp2",
".png",
".pbm",
".pgm",
".ppm",
".pxm",
".pnm",
".pfm",
".sr",
".ras",
".tiff",
".tif",
".exr",
".hdr",
".pic",
) # Stolen from here: https://docs.opencv.org/4.5.2/d4/da8/group__imgcodecs.html I didn't include .webp because if the image is animated shit does not work
LEFT_FOLDER_CODE = 100 # Default 100 - 'd'
RIGHT_FOLDER_CODE = 97 # Default 97 - 'a'
# Change by checking: https://www.ascii-code.com/
firstFolderName = input("Enter first folder name: [a] ")
secondFolderName = input("Enter second folder name: [d] ")
currentPath = os.path.abspath(os.getcwd()) # Stolen from: https://stackoverflow.com/q/3430372
os.chdir(currentPath) # Change working directory to the path where the python file is
currentPath = os.path.abspath(
os.getcwd()
) # Stolen from: https://stackoverflow.com/q/3430372
os.chdir(currentPath) # Change working directory to the path where the python file is
if path.isdir(firstFolderName) != 1: # Check if folder already exists, if it does not make it
os.mkdir(firstFolderName)
if (
path.isdir(firstFolderName) != 1
): # Check if folder already exists, if it does not make it
os.mkdir(firstFolderName)
if path.isdir(secondFolderName) != 1:
os.mkdir(secondFolderName)
os.mkdir(secondFolderName)
for filename in os.listdir(os.getcwd()): # Go through every file in the working directory
if (filename.lower()).endswith(IMAGE_EXTENSION): # If the file name ends with image extension
for filename in os.listdir(
os.getcwd()
): # Go through every file in the working directory
if (filename.lower()).endswith(
IMAGE_EXTENSION
): # If the file name ends with image extension
print(filename)
image = cv2.imread(filename, cv2.IMREAD_COLOR)
window_name = filename.split('.')[0]
cv2.namedWindow(window_name) # Window name is the same as image file name
window_name = filename.split(".")[0]
cv2.namedWindow(window_name) # Window name is the same as image file name
cv2.imshow(window_name, image)
key = cv2.waitKey()
if key == RIGHT_FOLDER_CODE:
shutil.move(currentPath + "/" + filename, currentPath + "/" + firstFolderName + "/" + filename)
shutil.move(
currentPath + "/" + filename,
currentPath + "/" + firstFolderName + "/" + filename,
)
elif key == LEFT_FOLDER_CODE:
shutil.move(currentPath + "/" + filename, currentPath + "/" + secondFolderName + "/" + filename)
#else:
shutil.move(
currentPath + "/" + filename,
currentPath + "/" + secondFolderName + "/" + filename,
)
# else:
# print(key)
cv2.destroyAllWindows()

View File

@ -15,5 +15,6 @@ npm run dev
Then open the printed local URL (default http://localhost:5173).
Notes:
- The Battery Status API may be unavailable or disabled in some browsers for privacy reasons. In that case, the app will show a helpful message.
- On laptops it typically works in Chromium-based browsers; mobile support varies.

View File

@ -4,9 +4,9 @@
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"typecheck": "tsc --noEmit",
"build": "vite build",
"dev": "vite",
"typecheck": "tsc --noEmit",
"build": "vite build",
"preview": "vite preview --strictPort --port 5173"
},
"dependencies": {
@ -14,7 +14,7 @@
"react-dom": "^18.3.1"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.3.1",
"@vitejs/plugin-react": "^4.3.1",
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
"typescript": "^5.5.4",

View File

@ -1,12 +1,13 @@
# Champions League Live Scores (React + TS)
This app displays live and today's UEFA Champions League results. It uses:
- React + TypeScript (Vite) for the frontend
- A tiny Express proxy server that calls football-data.org to fetch match data
## Setup
1) Create a `.env` file in `TS/champions_leauge_scores/`:
1. Create a `.env` file in `TS/champions_leauge_scores/`:
```
FOOTBALL_DATA_API_KEY=your_api_token_here
@ -15,7 +16,7 @@ PORT=8787
Sign up at https://www.football-data.org/ to get a free API token. Free tier has rate limits.
2) Install dependencies and run both servers:
2. Install dependencies and run both servers:
```
npm install
@ -26,9 +27,11 @@ npm run dev
- API Proxy: http://localhost:8787
## Notes
- Live endpoint: `GET /api/live`
- Today endpoint: `GET /api/matches` (uses today's date by default)
- Edit polling intervals in `src/App.tsx` if needed.
## License
MIT

View File

@ -4,29 +4,29 @@
"private": true,
"type": "module",
"scripts": {
"dev": "concurrently \"vite\" \"npm:server:dev\"",
"dev": "concurrently \"vite\" \"npm:server:dev\"",
"build": "vite build",
"preview": "vite preview",
"server:dev": "tsx watch server/src/server.ts"
"server:dev": "tsx watch server/src/server.ts"
},
"dependencies": {
"axios": "^1.7.2",
"dotenv": "^16.4.5",
"cors": "^2.8.5",
"cors": "^2.8.5",
"express": "^4.19.2",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.3.1",
"@vitejs/plugin-react": "^4.3.1",
"@types/express": "^4.17.21",
"@types/cors": "^2.8.17",
"@types/cors": "^2.8.17",
"@types/node": "^20.12.12",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"concurrently": "^8.2.2",
"ts-node-dev": "^2.0.0",
"tsx": "^4.19.2",
"ts-node-dev": "^2.0.0",
"tsx": "^4.19.2",
"typescript": "^5.4.5",
"vite": "^5.3.3"
}

View File

@ -20,15 +20,10 @@
"outputPath": "dist/two-inputs",
"index": "src/index.html",
"browser": "src/main.ts",
"polyfills": [
"zone.js"
],
"polyfills": ["zone.js"],
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"assets": ["src/favicon.ico", "src/assets"],
"styles": [
"@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.scss"
@ -80,16 +75,10 @@
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": [
"zone.js",
"zone.js/testing"
],
"polyfills": ["zone.js", "zone.js/testing"],
"tsConfig": "tsconfig.spec.json",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"assets": ["src/favicon.ico", "src/assets"],
"styles": [
"@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.scss"

View File

@ -5,10 +5,6 @@
"outDir": "./out-tsc/app",
"types": []
},
"files": [
"src/main.ts"
],
"include": [
"src/**/*.d.ts"
]
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"]
}

View File

@ -18,10 +18,7 @@
"target": "ES2022",
"module": "ES2022",
"useDefineForClassFields": false,
"lib": [
"ES2022",
"dom"
]
"lib": ["ES2022", "dom"]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,

View File

@ -3,12 +3,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": [
"jasmine"
]
"types": ["jasmine"]
},
"include": [
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}

View File

@ -9,7 +9,9 @@ Mini Articles (<=14KB)
- Local persistence via localStorage (no server required)
How to open
- Open `site/index.html` in a browser.
Tests
- `pytest` includes a test to enforce the 14KB budget for `index.html`.

View File

@ -476,4 +476,3 @@ server_c.c:44:12: style: The function 'append_file_line' is never used. [unusedF
static int append_file_line(const char* path, const char* line){ FILE* f=fopen(path,"ab"); if(!f) return -1; size_t n=fwrite(line,1,strlen(line),f); n+=fwrite("\n",1,1,f); fclose(f); return (int)n>=0?0:-1; }
^
nofile:0:0: information: Active checkers: 117/966 (use --checkers-report=<filename> to see details) [checkersReport]

View File

@ -1 +1,24 @@
[{"id":"29176c917f1a66c3","title":"full featuresd article","author":"author","body":"<img loading=\"lazy\" decoding=\"async\" src=\"/uploads/29176c9e5c008daf.jpg\" alt=\"image\"><div><br></div><div>This is an important image of course :)</div><div><br></div><div>and nother:<br><img loading=\"lazy\" decoding=\"async\" src=\"/uploads/29176c9f62367245.jpg\" alt=\"image\"><br></div>","thumb":"/uploads/29176c9e7818ee30.jpg","createdAt":1757331025041},{"id":"199259f41b8933b8","title":"Whats heveier","body":"A kilogrem of stel or kilogrem of fethers","thumb":"/uploads/27d6402e78a09d65.jpg","createdAt":1757272818104},{"id":"19925965dead21e7","title":"UwU its my first article","body":":)))<div><img src=\"/uploads/27d640335a16f85f.jpg\"><br></div>","thumb":"/uploads/27d640310e163446.jpg","createdAt":1757272235498}]
[
{
"id": "29176c917f1a66c3",
"title": "full featuresd article",
"author": "author",
"body": "<img loading=\"lazy\" decoding=\"async\" src=\"/uploads/29176c9e5c008daf.jpg\" alt=\"image\"><div><br></div><div>This is an important image of course :)</div><div><br></div><div>and nother:<br><img loading=\"lazy\" decoding=\"async\" src=\"/uploads/29176c9f62367245.jpg\" alt=\"image\"><br></div>",
"thumb": "/uploads/29176c9e7818ee30.jpg",
"createdAt": 1757331025041
},
{
"id": "199259f41b8933b8",
"title": "Whats heveier",
"body": "A kilogrem of stel or kilogrem of fethers",
"thumb": "/uploads/27d6402e78a09d65.jpg",
"createdAt": 1757272818104
},
{
"id": "19925965dead21e7",
"title": "UwU its my first article",
"body": ":)))<div><img src=\"/uploads/27d640335a16f85f.jpg\"><br></div>",
"thumb": "/uploads/27d640310e163446.jpg",
"createdAt": 1757272235498
}
]

View File

@ -1,11 +1,11 @@
import json
import os
import time
import urllib.request
import urllib.error
import subprocess
import socket
from pathlib import Path
import socket
import subprocess
import time
import urllib.error
import urllib.request
def _req(url, method="GET", data=None):
@ -40,47 +40,55 @@ def test_crud_roundtrip(tmp_path):
# wait briefly for server to be ready
for _ in range(30):
try:
with urllib.request.urlopen(base + "/api/articles", timeout=0.2) as resp:
with urllib.request.urlopen(
base + "/api/articles", timeout=0.2
) as resp:
resp.read()
break
except Exception:
time.sleep(0.05)
# Create
code, body = _req(base+"/api/articles", method="POST", data={
"title": "T1",
"body": "<p>Hello</p>",
"thumb": "data:image/png;base64,xyz"
})
code, body = _req(
base + "/api/articles",
method="POST",
data={
"title": "T1",
"body": "<p>Hello</p>",
"thumb": "data:image/png;base64,xyz",
},
)
assert code == 201
created = json.loads(body)
art_id = created["id"]
# List
code, body = _req(base+"/api/articles")
code, body = _req(base + "/api/articles")
assert code == 200
items = json.loads(body)
assert any(a["id"] == art_id for a in items)
# Get one
code, body = _req(base+f"/api/articles/{art_id}")
code, body = _req(base + f"/api/articles/{art_id}")
assert code == 200
got = json.loads(body)
assert got["title"] == "T1"
# Update
code, body = _req(base+f"/api/articles/{art_id}", method="PUT", data={"title": "T2"})
code, body = _req(
base + f"/api/articles/{art_id}", method="PUT", data={"title": "T2"}
)
assert code == 200
updated = json.loads(body)
assert updated["title"] == "T2"
# Delete
code, _ = _req(base+f"/api/articles/{art_id}", method="DELETE")
code, _ = _req(base + f"/api/articles/{art_id}", method="DELETE")
assert code == 204
# Ensure gone
try:
_req(base+f"/api/articles/{art_id}")
_req(base + f"/api/articles/{art_id}")
assert False, "Expected 404"
except urllib.error.HTTPError as e:
assert e.code == 404

View File

@ -4,7 +4,7 @@ import os
BUDGET = 14 * 1024 # 14 KiB
HERE = os.path.dirname(__file__)
SITE_FILE = os.path.join(HERE, 'index.html')
SITE_FILE = os.path.join(HERE, "index.html")
def test_site_file_exists():

352
lint_python.sh Executable file
View File

@ -0,0 +1,352 @@
#!/usr/bin/env bash
# ==============================================================================
# Python Linting Script - Run ALL linters with aggressive settings
# ==============================================================================
# Usage:
# ./lint_python.sh # Lint all Python files
# ./lint_python.sh --fix # Lint and auto-fix where possible
# ./lint_python.sh <file.py> # Lint specific file
# ./lint_python.sh --quick # Quick lint (ruff + mypy only)
# ./lint_python.sh --report # Generate detailed reports
# ==============================================================================
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
BOLD='\033[1m'
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="${SCRIPT_DIR}"
PYTHON_PATHS=(
"PYTHON"
"articles"
"poker-modifier-app"
"tests"
)
EXCLUDE_PATHS=(
".venv"
"__pycache__"
".git"
"Bash/ffmpeg-build"
".pytest_cache"
".ruff_cache"
".mypy_cache"
)
# Build exclude pattern for find
EXCLUDE_PATTERN=""
for path in "${EXCLUDE_PATHS[@]}"; do
EXCLUDE_PATTERN="${EXCLUDE_PATTERN} -path '*/${path}/*' -prune -o"
done
# Parse arguments
FIX_MODE=false
QUICK_MODE=false
REPORT_MODE=false
TARGET_FILES=""
VERBOSE=false
while [[ $# -gt 0 ]]; do
case $1 in
--fix|-f)
FIX_MODE=true
shift
;;
--quick|-q)
QUICK_MODE=true
shift
;;
--report|-r)
REPORT_MODE=true
shift
;;
--verbose|-v)
VERBOSE=true
shift
;;
--help|-h)
echo "Usage: $0 [OPTIONS] [FILES...]"
echo ""
echo "Options:"
echo " --fix, -f Auto-fix issues where possible"
echo " --quick, -q Quick mode (ruff + mypy only)"
echo " --report, -r Generate detailed reports to ./lint-reports/"
echo " --verbose, -v Show verbose output"
echo " --help, -h Show this help message"
echo ""
echo "Examples:"
echo " $0 # Lint all Python files"
echo " $0 --fix # Lint and auto-fix"
echo " $0 PYTHON/ # Lint specific directory"
echo " $0 --quick --fix # Quick lint with auto-fix"
exit 0
;;
*)
TARGET_FILES="${TARGET_FILES} $1"
shift
;;
esac
done
# If no target specified, use default paths
if [[ -z "${TARGET_FILES}" ]]; then
TARGET_FILES="${PYTHON_PATHS[*]}"
fi
# Create reports directory if needed
if [[ "${REPORT_MODE}" == true ]]; then
mkdir -p "${PROJECT_ROOT}/lint-reports"
fi
# Track overall status
OVERALL_STATUS=0
FAILED_TOOLS=()
# ==============================================================================
# Helper functions
# ==============================================================================
print_header() {
echo ""
echo -e "${BOLD}${BLUE}══════════════════════════════════════════════════════════════${NC}"
echo -e "${BOLD}${BLUE} $1${NC}"
echo -e "${BOLD}${BLUE}══════════════════════════════════════════════════════════════${NC}"
}
print_subheader() {
echo ""
echo -e "${CYAN}──────────────────────────────────────────────────────────────${NC}"
echo -e "${CYAN} $1${NC}"
echo -e "${CYAN}──────────────────────────────────────────────────────────────${NC}"
}
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
print_error() {
echo -e "${RED}✗${NC} $1"
}
print_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
run_tool() {
local tool_name="$1"
local tool_cmd="$2"
local report_file="${PROJECT_ROOT}/lint-reports/${tool_name}.txt"
print_subheader "Running ${tool_name}..."
if [[ "${REPORT_MODE}" == true ]]; then
if eval "${tool_cmd}" 2>&1 | tee "${report_file}"; then
print_success "${tool_name} passed"
return 0
else
print_error "${tool_name} found issues (see ${report_file})"
FAILED_TOOLS+=("${tool_name}")
return 1
fi
else
if eval "${tool_cmd}"; then
print_success "${tool_name} passed"
return 0
else
print_error "${tool_name} found issues"
FAILED_TOOLS+=("${tool_name}")
return 1
fi
fi
}
check_tool() {
if command -v "$1" &> /dev/null; then
return 0
else
print_warning "$1 not found, skipping..."
return 1
fi
}
# ==============================================================================
# Main linting process
# ==============================================================================
print_header "Python Linting Suite - Aggressive Mode"
echo ""
print_info "Target: ${TARGET_FILES}"
print_info "Fix mode: ${FIX_MODE}"
print_info "Quick mode: ${QUICK_MODE}"
print_info "Report mode: ${REPORT_MODE}"
cd "${PROJECT_ROOT}"
# ==============================================================================
# RUFF - Primary linter and formatter
# ==============================================================================
if check_tool ruff; then
if [[ "${FIX_MODE}" == true ]]; then
run_tool "ruff-lint" "ruff check --fix --show-fixes ${TARGET_FILES}" || OVERALL_STATUS=1
run_tool "ruff-format" "ruff format ${TARGET_FILES}" || OVERALL_STATUS=1
else
run_tool "ruff-lint" "ruff check ${TARGET_FILES}" || OVERALL_STATUS=1
run_tool "ruff-format-check" "ruff format --check ${TARGET_FILES}" || OVERALL_STATUS=1
fi
fi
# ==============================================================================
# MYPY - Static type checking
# ==============================================================================
if check_tool mypy; then
run_tool "mypy" "mypy --strict --ignore-missing-imports ${TARGET_FILES}" || OVERALL_STATUS=1
fi
# Quick mode exits here
if [[ "${QUICK_MODE}" == true ]]; then
print_header "Quick Lint Complete"
if [[ ${#FAILED_TOOLS[@]} -gt 0 ]]; then
print_error "Failed tools: ${FAILED_TOOLS[*]}"
exit 1
else
print_success "All quick checks passed!"
exit 0
fi
fi
# ==============================================================================
# PYLINT - Comprehensive linting
# ==============================================================================
if check_tool pylint; then
run_tool "pylint" "pylint --rcfile=pyproject.toml --jobs=0 --fail-under=0 ${TARGET_FILES}" || OVERALL_STATUS=1
fi
# ==============================================================================
# BANDIT - Security linting
# ==============================================================================
if check_tool bandit; then
run_tool "bandit" "bandit -c pyproject.toml -r ${TARGET_FILES} --severity-level low --confidence-level low" || OVERALL_STATUS=1
fi
# ==============================================================================
# VULTURE - Dead code detection
# ==============================================================================
if check_tool vulture; then
run_tool "vulture" "vulture --min-confidence 80 ${TARGET_FILES}" || OVERALL_STATUS=1
fi
# ==============================================================================
# FLAKE8 - Traditional linter
# ==============================================================================
if check_tool flake8; then
run_tool "flake8" "flake8 --max-line-length=88 --extend-ignore=E203,W503 --max-complexity=10 ${TARGET_FILES}" || OVERALL_STATUS=1
fi
# ==============================================================================
# PYCODESTYLE - PEP 8 style checker
# ==============================================================================
if check_tool pycodestyle; then
run_tool "pycodestyle" "pycodestyle --max-line-length=88 --ignore=E203,W503 ${TARGET_FILES}" || OVERALL_STATUS=1
fi
# ==============================================================================
# PYDOCSTYLE - Docstring style checker
# ==============================================================================
if check_tool pydocstyle; then
run_tool "pydocstyle" "pydocstyle --convention=google ${TARGET_FILES}" || OVERALL_STATUS=1
fi
# ==============================================================================
# RADON - Complexity metrics
# ==============================================================================
if check_tool radon; then
print_subheader "Running radon (complexity analysis)..."
echo ""
echo -e "${MAGENTA}Cyclomatic Complexity:${NC}"
radon cc -a -s ${TARGET_FILES} || true
echo ""
echo -e "${MAGENTA}Maintainability Index:${NC}"
radon mi -s ${TARGET_FILES} || true
if [[ "${REPORT_MODE}" == true ]]; then
radon cc -a -s ${TARGET_FILES} > "${PROJECT_ROOT}/lint-reports/radon-cc.txt" 2>&1 || true
radon mi -s ${TARGET_FILES} > "${PROJECT_ROOT}/lint-reports/radon-mi.txt" 2>&1 || true
fi
fi
# ==============================================================================
# INTERROGATE - Docstring coverage
# ==============================================================================
if check_tool interrogate; then
run_tool "interrogate" "interrogate -v --fail-under=0 ${TARGET_FILES}" || OVERALL_STATUS=1
fi
# ==============================================================================
# PYRIGHT - Microsoft's type checker (optional, very strict)
# ==============================================================================
if check_tool pyright; then
run_tool "pyright" "pyright ${TARGET_FILES}" || OVERALL_STATUS=1
fi
# ==============================================================================
# AUTOFLAKE - Unused imports/variables (fix mode only)
# ==============================================================================
if [[ "${FIX_MODE}" == true ]] && check_tool autoflake; then
print_subheader "Running autoflake (removing unused imports)..."
find ${TARGET_FILES} -name "*.py" -type f -exec autoflake --in-place --remove-all-unused-imports --remove-unused-variables {} \;
print_success "autoflake completed"
fi
# ==============================================================================
# PYUPGRADE - Upgrade Python syntax (fix mode only)
# ==============================================================================
if [[ "${FIX_MODE}" == true ]] && check_tool pyupgrade; then
print_subheader "Running pyupgrade (upgrading syntax to Python 3.10+)..."
find ${TARGET_FILES} -name "*.py" -type f -exec pyupgrade --py310-plus {} \;
print_success "pyupgrade completed"
fi
# ==============================================================================
# CODESPELL - Spell checking
# ==============================================================================
if check_tool codespell; then
if [[ "${FIX_MODE}" == true ]]; then
run_tool "codespell" "codespell -w --skip='*.json,*.lock,.git,__pycache__,.venv' ${TARGET_FILES}" || OVERALL_STATUS=1
else
run_tool "codespell" "codespell --skip='*.json,*.lock,.git,__pycache__,.venv' ${TARGET_FILES}" || OVERALL_STATUS=1
fi
fi
# ==============================================================================
# Summary
# ==============================================================================
print_header "Linting Summary"
echo ""
if [[ ${#FAILED_TOOLS[@]} -gt 0 ]]; then
print_error "The following tools reported issues:"
for tool in "${FAILED_TOOLS[@]}"; do
echo " - ${tool}"
done
echo ""
if [[ "${REPORT_MODE}" == true ]]; then
print_info "Detailed reports saved to: ${PROJECT_ROOT}/lint-reports/"
fi
print_info "Run with --fix to auto-fix issues where possible"
exit 1
else
print_success "All linting checks passed!"
exit 0
fi

View File

@ -47,7 +47,10 @@ A fun web application that randomly applies modifiers to Texas Hold'em poker gam
You can easily add new modifiers by using the `addModifier()` method:
```javascript
window.pokerApp.addModifier("Your Modifier Name", "Description of what it does");
window.pokerApp.addModifier(
"Your Modifier Name",
"Description of what it does",
);
```
## Browser Compatibility

View File

@ -32,6 +32,7 @@ python poker_modifier_app.py
## Modifiers Included
### Classic Poker Modifiers
- **High Stakes**: All bets are doubled
- **Wild Card**: Next card can be used as any card
- **Bluff Master**: See one opponent's card before betting
@ -51,6 +52,7 @@ python poker_modifier_app.py
## Modifiers Included
### Classic Poker Modifiers
- **High Stakes**: All bets are doubled
- **Wild Card**: Next card can be used as any card
- **Bluff Master**: See one opponent's card before betting
@ -68,6 +70,7 @@ python poker_modifier_app.py
- **Chip Challenge**: Winner gets extra house chips
### Drinking Game Modifiers
- **Red or Black**: Guess community card colors for double winnings
- **Pocket Rockets**: Pocket Aces trigger drinks for everyone else
- **Rainbow Flop**: 3-suit flop boosts flush draws

299
pyproject.toml Normal file
View File

@ -0,0 +1,299 @@
[project]
name = "testsandmisc"
version = "0.1.0"
description = "Collection of miscellaneous tests and scripts"
requires-python = ">=3.10"
# ============================================================================
# RUFF - Extremely fast Python linter and formatter (written in Rust)
# ============================================================================
[tool.ruff]
line-length = 88 # Black-compatible line length (stricter)
target-version = "py310"
# Include all Python files
include = ["*.py", "**/*.py"]
# Exclude vendored/build directories
exclude = [
".git",
".venv",
"__pycache__",
"build",
"dist",
".eggs",
"Bash/ffmpeg-build", # Vendored FFmpeg tools
]
[tool.ruff.lint]
# AGGRESSIVE: Select ALL rules from all categories
select = ["ALL"]
# Minimal ignores - only conflicting rules
ignore = [
"D203", # 1 blank line required before class docstring (conflicts with D211)
"D213", # Multi-line docstring summary should start at second line (conflicts with D212)
"COM812", # Trailing comma missing (conflicts with formatter)
"ISC001", # Implicit string concatenation (conflicts with formatter)
"ANN101", # Missing type annotation for self (deprecated)
"ANN102", # Missing type annotation for cls (deprecated)
]
# Allow ALL rules to be auto-fixed
fixable = ["ALL"]
unfixable = []
# Per-file ignores for test files
[tool.ruff.lint.per-file-ignores]
"**/tests/**/*.py" = [
"S101", # Allow assert in tests
"PLR2004", # Allow magic values in tests
"D100", # Allow missing module docstring in tests
"D103", # Allow missing function docstring in tests
]
"**/conftest.py" = [
"D100", # Allow missing module docstring
"D103", # Allow missing function docstring
]
[tool.ruff.lint.pydocstyle]
convention = "google" # Use Google docstring convention
[tool.ruff.lint.isort]
force-single-line = false
force-sort-within-sections = true
known-first-party = ["PYTHON"]
[tool.ruff.lint.flake8-quotes]
docstring-quotes = "double"
inline-quotes = "double"
[tool.ruff.lint.flake8-tidy-imports]
ban-relative-imports = "all"
[tool.ruff.lint.pylint]
max-args = 5
max-branches = 12
max-returns = 6
max-statements = 50
[tool.ruff.lint.mccabe]
max-complexity = 10
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
docstring-code-format = true
# ============================================================================
# MYPY - Static type checker (most aggressive settings)
# ============================================================================
[tool.mypy]
python_version = "3.10"
# Strict mode enables most checks
strict = true
# Additional aggressive settings
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_decorators = true
no_implicit_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_unreachable = true
# Extra strict settings
disallow_any_unimported = true
disallow_any_explicit = false # Too aggressive for practical use
disallow_any_generics = true
disallow_subclassing_any = true
strict_equality = true
extra_checks = true
# Allow missing imports for third-party packages
ignore_missing_imports = true
# Show error codes
show_error_codes = true
# Enable colored output
color_output = true
# Exclude vendored directories
exclude = [
"Bash/ffmpeg-build/",
".venv/",
]
# ============================================================================
# PYLINT - Comprehensive Python linter
# ============================================================================
[tool.pylint.main]
# Analyse import fallback blocks
analyse-fallback-blocks = true
# Pickle collected data for later comparisons
persistent = true
# Jobs to use for parallel execution (0 = auto)
jobs = 0
# Minimum Python version
py-version = "3.10"
# Ignore vendored directories
ignore = ["Bash", ".venv", "__pycache__"]
# Ignore patterns
ignore-patterns = [".*\\.pyi$"]
[tool.pylint.messages_control]
# Enable all checks by disabling disable
enable = "all"
# Minimal disabled checks - only truly problematic ones
disable = [
"raw-checker-failed",
"bad-inline-option",
"locally-disabled",
"file-ignored",
"suppressed-message",
"useless-suppression",
"deprecated-pragma",
"use-symbolic-message-instead",
"too-few-public-methods", # Often false positive for data classes
]
[tool.pylint.format]
# Maximum line length (same as ruff/black)
max-line-length = 88
# Maximum module lines
max-module-lines = 1000
[tool.pylint.design]
# Maximum arguments for function/method
max-args = 5
# Maximum local variables
max-locals = 15
# Maximum return statements
max-returns = 6
# Maximum branches in function body
max-branches = 12
# Maximum statements in function body
max-statements = 50
# Maximum parents for a class
max-parents = 7
# Maximum attributes for a class
max-attributes = 10
# Maximum public methods for a class
max-public-methods = 20
# Minimum public methods for a class
min-public-methods = 1
[tool.pylint.similarities]
# Minimum lines for similarity check
min-similarity-lines = 4
# Ignore comments when computing similarities
ignore-comments = true
# Ignore docstrings when computing similarities
ignore-docstrings = true
# Ignore imports when computing similarities
ignore-imports = true
[tool.pylint.spelling]
# No spelling dictionary to avoid false positives
spelling-dict = ""
[tool.pylint.typecheck]
# Ignore missing members for dynamic modules
ignored-modules = ["chess", "pygame", "cv2", "PIL"]
# ============================================================================
# BANDIT - Security linter
# ============================================================================
[tool.bandit]
# Exclude test directories and vendored code
exclude_dirs = ["tests", ".venv", "Bash/ffmpeg-build"]
# Use all tests
tests = []
# No skipped tests (most aggressive)
skips = []
# Severity level: all (1=low, 2=medium, 3=high)
severity = 1
# Confidence level: all
confidence = 1
# ============================================================================
# BLACK - Code formatter (for comparison/fallback)
# ============================================================================
[tool.black]
line-length = 88
target-version = ["py310"]
include = '\.pyi?$'
extend-exclude = '''
/(
\.git
| \.venv
| __pycache__
| Bash/ffmpeg-build
)/
'''
# ============================================================================
# ISORT - Import sorting (for comparison/fallback, ruff handles this)
# ============================================================================
[tool.isort]
profile = "black"
line_length = 88
force_single_line = false
force_sort_within_sections = true
known_first_party = ["PYTHON"]
skip = [".venv", "__pycache__", "Bash/ffmpeg-build"]
# ============================================================================
# PYTEST - Testing framework configuration
# ============================================================================
[tool.pytest.ini_options]
testpaths = ["tests", "PYTHON", "articles"]
python_files = ["test_*.py", "*_test.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = [
"-v",
"--strict-markers",
"--strict-config",
"-ra",
]
filterwarnings = [
"error",
"ignore::DeprecationWarning",
]
# ============================================================================
# COVERAGE - Code coverage configuration
# ============================================================================
[tool.coverage.run]
source = ["PYTHON", "articles", "poker-modifier-app"]
branch = true
omit = [
"*/__pycache__/*",
"*/tests/*",
"*/.venv/*",
"Bash/*",
]
[tool.coverage.report]
# Fail under this percentage
fail_under = 0
show_missing = true
skip_covered = false
exclude_lines = [
"pragma: no cover",
"def __repr__",
"raise AssertionError",
"raise NotImplementedError",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]
# ============================================================================
# VULTURE - Dead code detection
# ============================================================================
# Note: Vulture uses command-line args, but we can document settings here
# vulture --min-confidence 80 --exclude ".venv,Bash" .
# ============================================================================
# PYDOCSTYLE - Docstring style checker (ruff handles this, but for standalone)
# ============================================================================
# Configured in ruff.lint.pydocstyle above

191
requirements-dev.txt Normal file
View File

@ -0,0 +1,191 @@
# ==============================================================================
# Python Development Dependencies - Linting, Formatting, and Testing
# ==============================================================================
# Install with: pip install -r requirements-dev.txt
# ==============================================================================
# Include base requirements
-r requirements.txt
# add-trailing-comma - Add trailing commas
add-trailing-comma>=3.1.0
# autoflake - Remove unused imports and variables
autoflake>=2.2.0
# autopep8 - PEP 8 formatting (alternative formatter)
autopep8>=2.0.0
# ==============================================================================
# SECURITY LINTERS
# ==============================================================================
# Bandit - Security linter
bandit>=1.7.0
# Black - The uncompromising code formatter (fallback/comparison)
black>=24.0.0
# ==============================================================================
# SPELL CHECKING
# ==============================================================================
# codespell - Fix common misspellings
codespell>=2.2.0
# Coverage.py - Code coverage measurement
coverage>=7.4.0
# darglint - Check docstrings match function signatures
darglint>=1.8.0
# dead - Find dead code
dead>=1.5.0
# docformatter - Formats docstrings
docformatter>=1.7.0
# fixit - Auto-fix linting errors
fixit>=2.1.0
# Flake8 - Linting tool (wraps pyflakes, pycodestyle, mccabe)
flake8>=7.0.0
flake8-annotations>=3.0.0 # Type annotation checks
flake8-bandit>=4.1.0 # Security checks via bandit
# Flake8 plugins for maximum coverage
flake8-bugbear>=24.0.0 # Additional bug detection
flake8-comprehensions>=3.14.0 # Better list/dict/set comprehensions
flake8-docstrings>=1.7.0 # Docstring checks
flake8-eradicate>=1.5.0 # Dead code detection
flake8-pie>=0.16.0 # Miscellaneous lints
flake8-print>=5.0.0 # Detect print statements
flake8-pyi>=24.0.0 # Type stub file checks
flake8-pytest-style>=2.0.0 # Pytest style checks
flake8-return>=1.2.0 # Better return statement checks
flake8-simplify>=0.21.0 # Simplification suggestions
# Hypothesis - Property-based testing
hypothesis>=6.98.0
# ==============================================================================
# IMPORT CHECKING
# ==============================================================================
# importlib-metadata for import analysis
importlib-metadata>=7.0.0
# ==============================================================================
# DOCUMENTATION
# ==============================================================================
# pep257 - PEP 257 docstring checker (legacy, use pydocstyle)
# interrogate - Check docstring coverage
interrogate>=1.5.0
# isort - Import sorting (ruff handles this, but useful standalone)
isort>=5.13.0
# mccabe - McCabe complexity checker
mccabe>=0.7.0
# ==============================================================================
# TYPE CHECKING
# ==============================================================================
# MyPy - Static type checker
mypy>=1.8.0
# pip-audit - Audit Python packages for known vulnerabilities
pip-audit>=2.6.0
# pipdeptree - Show dependency tree
pipdeptree>=2.14.0
# ==============================================================================
# PRE-COMMIT
# ==============================================================================
# pre-commit - Git hook management
pre-commit>=3.6.0
# prospector - Python static analysis tool
prospector>=1.10.0
# pycodestyle - Python style guide checker (PEP 8)
pycodestyle>=2.11.0
# pydocstyle - Docstring style checker (PEP 257)
pydocstyle>=6.3.0
# pyflakes - Passive checker of Python programs
pyflakes>=3.2.0
# pylama - Code audit tool (wraps multiple linters)
pylama>=8.4.0
# ==============================================================================
# LINTERS
# ==============================================================================
# Pylint - Comprehensive Python linter
pylint>=3.0.0
# Pyright - Microsoft's type checker (very strict)
pyright>=1.1.350
# ==============================================================================
# TESTING
# ==============================================================================
# pytest - Testing framework
pytest>=8.0.0
# pytest plugins
pytest-cov>=4.1.0 # Coverage plugin
pytest-randomly>=3.15.0 # Randomize test order
pytest-sugar>=1.0.0 # Better test output
pytest-timeout>=2.2.0 # Test timeouts
pytest-xdist>=3.5.0 # Parallel test execution
# ==============================================================================
# ADDITIONAL TOOLS
# ==============================================================================
# pyupgrade - Upgrade Python syntax
pyupgrade>=3.15.0
# Radon - Code metrics (complexity, maintainability)
radon>=6.0.0
# reorder-python-imports - Reorder imports
reorder-python-imports>=3.12.0
# ==============================================================================
# CODE FORMATTERS
# ==============================================================================
# Ruff - Extremely fast Python linter and formatter (replaces many tools)
ruff>=0.8.0
# Safety - Check dependencies for known security vulnerabilities
safety>=2.3.0
types-python-dateutil>=2.8.0
types-PyYAML>=6.0.0
# Type stubs for common packages
types-requests>=2.31.0
types-setuptools>=69.0.0
# ==============================================================================
# CODE QUALITY & DEAD CODE DETECTION
# ==============================================================================
# Vulture - Find dead code
vulture>=2.10
# xenon - Monitor code complexity
xenon>=0.9.0
# yapf - Yet Another Python Formatter (Google's formatter)
yapf>=0.40.0

View File

@ -1,2 +1,2 @@
python-chess>=1.999
pytest>=7.0
python-chess>=1.999