migliorati hook pre-commit e README

This commit is contained in:
senpai 2025-09-15 20:00:48 +02:00
parent 85f7c46c4e
commit 6460ba1f76
6 changed files with 65 additions and 21 deletions

1
.gitignore vendored
View file

@ -8,6 +8,7 @@ wheels/
# Library caches
.mypy_cache
.ruff_cache
# Virtual environments
.venv

View file

@ -5,21 +5,21 @@ pre-commit:
# check for hard errors
- name: 01_ruff_syntax
glob: "*.py"
run: ruff check --select E9,F63,F7,F82 --no-fix {staged_files}
run: ruff --config pyproject.toml check --select E9,F63,F7,F82 --no-fix {staged_files}
# fix (pyupgrade, import sort, ecc.)
- name: 02_ruff_fix
glob: "*.py"
run: ruff check --fix --exit-zero {staged_files}
run: ruff --config pyproject.toml check --fix {staged_files}
stage_fixed: true
# formatter
- name: 03_ruff_format
glob: "*.py"
run: ruff format {staged_files} || true
run: ruff --config pyproject.toml format {staged_files} || true
stage_fixed: true
# complete check
- name: 04_ruff_check_strict
glob: "*.py"
run: ruff check {staged_files}
run: ruff --config pyproject.toml check {staged_files}

View file

@ -1,13 +1,17 @@
# Indice
<!---toc start-->
- [Indice](#indice)
- [Configurazione iniziale](#configurazione-iniziale)
- [Installare UV](#installare-uv)
- [Clonare la repo](#clonare-la-repo)
- [Attivare il virtual environment di uv](#attivare-il-virtual-environment-di-uv)
- [Aggiornare il venv con le impostazioni del pyproject.toml](#aggiornare-il-venv-con-le-impostazioni-del-pyprojecttoml)
- [Per sicurezza installare i tool anche globalmente](#per-sicurezza-installare-i-tool-anche-globalmente)
- [Impostare il proprio nome](#impostare-il-proprio-nome)
- [Impostare l'upstream](#impostare-lupstream)
- [Impostare il formatting prima dei push](#impostare-il-formatting-prima-dei-push)
- [Impostare check e formatting prima dei push](#impostare-check-e-formatting-prima-dei-push)
- [Comandi](#comandi)
- [Python](#python)
- [Eseguire il codice sul robot](#eseguire-il-codice-sul-robot)
@ -17,6 +21,8 @@
- [Committare le modifiche](#committare-le-modifiche)
- [Pushare le modifiche al server](#pushare-le-modifiche-al-server)
<!---toc end-->
# Configurazione iniziale
> [!IMPORTANT]
@ -64,6 +70,13 @@ Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
uv sync
```
### Per sicurezza installare i tool anche globalmente
```bash
uv tool install ruff
uv tool install lefthook
```
## Impostare il proprio nome
```bash

View file

@ -7,6 +7,7 @@ requires-python = ">=3.13"
dependencies = [
"lefthook>=1.13.0",
"pybricksdev>=2.1.0",
"ruff>=0.13.0",
]
[tool.black]
@ -18,7 +19,10 @@ target-version = "py313"
line-length = 88
[tool.ruff.lint]
extend-select = ["I", "UP"] # import sort + pyupgrade-like
extend-select = ["I", "UP", "N", "C"] # isort, pyupgrade, pep8-naming, mccabe
[tool.ruff.lint.mccabe]
max-complexity = 12
[tool.ruff.format]
quote-style = "double"

28
test.py
View file

@ -9,6 +9,7 @@ print("TEST NORMALE (Pybricks)")
hub = PrimeHub()
PORTS = [Port.A, Port.B, Port.C, Port.D, Port.E, Port.F]
motori: list[Motor] = []
color_sensors: dict[Port, ColorSensor] = {}
distance_sensors: dict[Port, UltrasonicSensor] = {}
@ -18,21 +19,21 @@ for porta in PORTS:
try:
m = Motor(porta)
motori.append(m)
print("Motore trovato su porta {}".format(porta))
print(f"Motore trovato su porta {porta}")
continue
except Exception:
pass
try:
cs = ColorSensor(porta)
color_sensors[porta] = cs
print("ColorSensor trovato su porta {}".format(porta))
print(f"ColorSensor trovato su porta {porta}")
continue
except Exception:
pass
try:
us = UltrasonicSensor(porta)
distance_sensors[porta] = us
print("UltrasonicSensor (distanza) trovato su porta {}".format(porta))
print(f"UltrasonicSensor (distanza) trovato su porta {porta}")
continue
except Exception:
pass
@ -48,23 +49,21 @@ print("Valori dei sensori:")
for porta, cs in color_sensors.items():
c = cs.color()
nome = getattr(c, "name", "NONE") if c is not None else "NONE"
print("ColorSensor su porta {}: colore rilevato {}".format(porta, nome))
print(f"ColorSensor su porta {porta}: colore rilevato {nome}")
for porta, us in distance_sensors.items():
d = us.distance()
if d is None:
print("Ultrasonic su porta {}: distanza fuori portata".format(porta))
print(f"Ultrasonic su porta {porta}: distanza fuori portata")
else:
print(
"Ultrasonic su porta {}: distanza rilevata {:.1f} cm".format(porta, d / 10)
)
print(f"Ultrasonic su porta {porta}: distanza rilevata {d / 10:.1f} cm")
# Sensori integrati (IMU)
print("Valori dei sensori integrati nell'hub:")
tilt = hub.imu.tilt()
print("Tilt (roll, pitch): {}".format(tilt))
print(f"Tilt (roll, pitch): {tilt}")
gyro = hub.imu.angular_velocity()
print("Velocità angolare (x,y,z): {}".format(gyro))
print(f"Velocità angolare (x,y,z): {gyro}")
# ---------- FIX: parsing colori senza isinstance/issubclass --------------
@ -112,7 +111,7 @@ def wait_for_color(color, sensor_port=None, timeout=None, min_time=0):
"""
target = _parse_color(color)
if target is None:
print("Colore '{}' non riconosciuto nell'enum Color.".format(color))
print(f"Colore '{color}' non riconosciuto nell'enum Color.")
return False
if sensor_port is not None:
@ -142,7 +141,7 @@ def wait_for_color(color, sensor_port=None, timeout=None, min_time=0):
continue
current = cs.color()
nome = getattr(current, "name", "NONE") if current is not None else "NONE"
print("Porta {}: Colore rilevato {}".format(porta, nome))
print(f"Porta {porta}: Colore rilevato {nome}")
if current == target:
t0 = sw.time()
@ -152,9 +151,7 @@ def wait_for_color(color, sensor_port=None, timeout=None, min_time=0):
break
if sw.time() - t0 >= min_ms:
print(
"Colore {} rilevato su porta {} per almeno {} s.".format(
target.name, porta, min_time
)
f"Colore {target.name} rilevato su porta {porta} per almeno {min_time} s."
)
return True
wait(100)
@ -166,6 +163,7 @@ try:
if wait_for_color("violet"):
for m in motori:
m.dc(100)
m.dc(-100)
if wait_for_color("white", min_time=2):
for m in motori:

28
uv.lock generated
View file

@ -286,12 +286,40 @@ source = { virtual = "." }
dependencies = [
{ name = "lefthook" },
{ name = "pybricksdev" },
{ name = "ruff" },
]
[package.metadata]
requires-dist = [
{ name = "lefthook", specifier = ">=1.13.0" },
{ name = "pybricksdev", specifier = ">=2.1.0" },
{ name = "ruff", specifier = ">=0.13.0" },
]
[[package]]
name = "ruff"
version = "0.13.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/6e/1a/1f4b722862840295bcaba8c9e5261572347509548faaa99b2d57ee7bfe6a/ruff-0.13.0.tar.gz", hash = "sha256:5b4b1ee7eb35afae128ab94459b13b2baaed282b1fb0f472a73c82c996c8ae60", size = 5372863, upload-time = "2025-09-10T16:25:37.917Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/ac/fe/6f87b419dbe166fd30a991390221f14c5b68946f389ea07913e1719741e0/ruff-0.13.0-py3-none-linux_armv6l.whl", hash = "sha256:137f3d65d58ee828ae136a12d1dc33d992773d8f7644bc6b82714570f31b2004", size = 12187826, upload-time = "2025-09-10T16:24:39.5Z" },
{ url = "https://files.pythonhosted.org/packages/e4/25/c92296b1fc36d2499e12b74a3fdb230f77af7bdf048fad7b0a62e94ed56a/ruff-0.13.0-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:21ae48151b66e71fd111b7d79f9ad358814ed58c339631450c66a4be33cc28b9", size = 12933428, upload-time = "2025-09-10T16:24:43.866Z" },
{ url = "https://files.pythonhosted.org/packages/44/cf/40bc7221a949470307d9c35b4ef5810c294e6cfa3caafb57d882731a9f42/ruff-0.13.0-py3-none-macosx_11_0_arm64.whl", hash = "sha256:64de45f4ca5441209e41742d527944635a05a6e7c05798904f39c85bafa819e3", size = 12095543, upload-time = "2025-09-10T16:24:46.638Z" },
{ url = "https://files.pythonhosted.org/packages/f1/03/8b5ff2a211efb68c63a1d03d157e924997ada87d01bebffbd13a0f3fcdeb/ruff-0.13.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2b2c653ae9b9d46e0ef62fc6fbf5b979bda20a0b1d2b22f8f7eb0cde9f4963b8", size = 12312489, upload-time = "2025-09-10T16:24:49.556Z" },
{ url = "https://files.pythonhosted.org/packages/37/fc/2336ef6d5e9c8d8ea8305c5f91e767d795cd4fc171a6d97ef38a5302dadc/ruff-0.13.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:4cec632534332062bc9eb5884a267b689085a1afea9801bf94e3ba7498a2d207", size = 11991631, upload-time = "2025-09-10T16:24:53.439Z" },
{ url = "https://files.pythonhosted.org/packages/39/7f/f6d574d100fca83d32637d7f5541bea2f5e473c40020bbc7fc4a4d5b7294/ruff-0.13.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:dcd628101d9f7d122e120ac7c17e0a0f468b19bc925501dbe03c1cb7f5415b24", size = 13720602, upload-time = "2025-09-10T16:24:56.392Z" },
{ url = "https://files.pythonhosted.org/packages/fd/c8/a8a5b81d8729b5d1f663348d11e2a9d65a7a9bd3c399763b1a51c72be1ce/ruff-0.13.0-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:afe37db8e1466acb173bb2a39ca92df00570e0fd7c94c72d87b51b21bb63efea", size = 14697751, upload-time = "2025-09-10T16:24:59.89Z" },
{ url = "https://files.pythonhosted.org/packages/57/f5/183ec292272ce7ec5e882aea74937f7288e88ecb500198b832c24debc6d3/ruff-0.13.0-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:0f96a8d90bb258d7d3358b372905fe7333aaacf6c39e2408b9f8ba181f4b6ef2", size = 14095317, upload-time = "2025-09-10T16:25:03.025Z" },
{ url = "https://files.pythonhosted.org/packages/9f/8d/7f9771c971724701af7926c14dab31754e7b303d127b0d3f01116faef456/ruff-0.13.0-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:94b5e3d883e4f924c5298e3f2ee0f3085819c14f68d1e5b6715597681433f153", size = 13144418, upload-time = "2025-09-10T16:25:06.272Z" },
{ url = "https://files.pythonhosted.org/packages/a8/a6/7985ad1778e60922d4bef546688cd8a25822c58873e9ff30189cfe5dc4ab/ruff-0.13.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:03447f3d18479df3d24917a92d768a89f873a7181a064858ea90a804a7538991", size = 13370843, upload-time = "2025-09-10T16:25:09.965Z" },
{ url = "https://files.pythonhosted.org/packages/64/1c/bafdd5a7a05a50cc51d9f5711da704942d8dd62df3d8c70c311e98ce9f8a/ruff-0.13.0-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:fbc6b1934eb1c0033da427c805e27d164bb713f8e273a024a7e86176d7f462cf", size = 13321891, upload-time = "2025-09-10T16:25:12.969Z" },
{ url = "https://files.pythonhosted.org/packages/bc/3e/7817f989cb9725ef7e8d2cee74186bf90555279e119de50c750c4b7a72fe/ruff-0.13.0-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:a8ab6a3e03665d39d4a25ee199d207a488724f022db0e1fe4002968abdb8001b", size = 12119119, upload-time = "2025-09-10T16:25:16.621Z" },
{ url = "https://files.pythonhosted.org/packages/58/07/9df080742e8d1080e60c426dce6e96a8faf9a371e2ce22eef662e3839c95/ruff-0.13.0-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:d2a5c62f8ccc6dd2fe259917482de7275cecc86141ee10432727c4816235bc41", size = 11961594, upload-time = "2025-09-10T16:25:19.49Z" },
{ url = "https://files.pythonhosted.org/packages/6a/f4/ae1185349197d26a2316840cb4d6c3fba61d4ac36ed728bf0228b222d71f/ruff-0.13.0-py3-none-musllinux_1_2_i686.whl", hash = "sha256:b7b85ca27aeeb1ab421bc787009831cffe6048faae08ad80867edab9f2760945", size = 12933377, upload-time = "2025-09-10T16:25:22.371Z" },
{ url = "https://files.pythonhosted.org/packages/b6/39/e776c10a3b349fc8209a905bfb327831d7516f6058339a613a8d2aaecacd/ruff-0.13.0-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:79ea0c44a3032af768cabfd9616e44c24303af49d633b43e3a5096e009ebe823", size = 13418555, upload-time = "2025-09-10T16:25:25.681Z" },
{ url = "https://files.pythonhosted.org/packages/46/09/dca8df3d48e8b3f4202bf20b1658898e74b6442ac835bfe2c1816d926697/ruff-0.13.0-py3-none-win32.whl", hash = "sha256:4e473e8f0e6a04e4113f2e1de12a5039579892329ecc49958424e5568ef4f768", size = 12141613, upload-time = "2025-09-10T16:25:28.664Z" },
{ url = "https://files.pythonhosted.org/packages/61/21/0647eb71ed99b888ad50e44d8ec65d7148babc0e242d531a499a0bbcda5f/ruff-0.13.0-py3-none-win_amd64.whl", hash = "sha256:48e5c25c7a3713eea9ce755995767f4dcd1b0b9599b638b12946e892123d1efb", size = 13258250, upload-time = "2025-09-10T16:25:31.773Z" },
{ url = "https://files.pythonhosted.org/packages/e1/a3/03216a6a86c706df54422612981fb0f9041dbb452c3401501d4a22b942c9/ruff-0.13.0-py3-none-win_arm64.whl", hash = "sha256:ab80525317b1e1d38614addec8ac954f1b3e662de9d59114ecbf771d00cf613e", size = 12312357, upload-time = "2025-09-10T16:25:35.595Z" },
]
[[package]]