chore: initialize containerized Python learning workbook (2026 DevOps/SRE)
This commit is contained in:
commit
ae4c421815
63 files changed
+1273
No files matched your search
@@ -0,0 +1,10 @@
|
||||
# 04 — Błędy i Wyjątki (`try`/`except`, własne wyjątki)
|
||||
|
||||
## Zadania (`errors_ex.py`)
|
||||
1. `parse_positive_int(raw: str) -> int` — rzuć `InvalidNumberError`, gdy nie da się sparsować do `int`, lub `NotPositiveError`, gdy `<= 0`.
|
||||
2. `safe_divide(a: float, b: float) -> float | None` — zwróć `None` zamiast `ZeroDivisionError`.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test MOD=01-core/04-errors-exceptions
|
||||
```
|
||||
@@ -0,0 +1,14 @@
|
||||
class InvalidNumberError(ValueError):
|
||||
"""Błąd parsowania liczby."""
|
||||
|
||||
|
||||
class NotPositiveError(ValueError):
|
||||
"""Liczba nie jest dodatnia."""
|
||||
|
||||
|
||||
def parse_positive_int(raw: str) -> int:
|
||||
raise NotImplementedError("Sparsuj raw do int lub rzuć InvalidNumberError / NotPositiveError")
|
||||
|
||||
|
||||
def safe_divide(a: float, b: float) -> float | None:
|
||||
raise NotImplementedError("Podziel a / b, zwracając None dla b == 0")
|
||||
@@ -0,0 +1,17 @@
|
||||
import pytest
|
||||
from errors_ex import InvalidNumberError, NotPositiveError, parse_positive_int, safe_divide
|
||||
|
||||
|
||||
def test_parse_positive_int() -> None:
|
||||
assert parse_positive_int("42") == 42
|
||||
with pytest.raises(NotPositiveError):
|
||||
parse_positive_int("0")
|
||||
with pytest.raises(NotPositiveError):
|
||||
parse_positive_int("-3")
|
||||
with pytest.raises(InvalidNumberError):
|
||||
parse_positive_int("abc")
|
||||
|
||||
|
||||
def test_safe_divide() -> None:
|
||||
assert safe_divide(10.0, 2.0) == 5.0
|
||||
assert safe_divide(10.0, 0.0) is None
|
||||
Reference in new issue
Block a user