chore: initialize containerized Python learning workbook (2026 DevOps/SRE)

This commit is contained in:
soda64 committed 2026-11-30 22:55:56 +01:00
commit ae4c421815
63 files changed
+1273

No files matched your search

@@ -0,0 +1,10 @@
# 09 — Wzorce Projektowe w Pythonie (Strategy, Context Manager, DI)
## Zadania (`patterns_ex.py`)
1. `apply_discount(price_cents: int, strategy: Callable[[int], int]) -> int` — wzorzec Strategy (w Pythonie funkcje są obiektami pierwszej klasy).
2. `ManagedFlag` — Context Manager (`__enter__` ustawia `active = True`, `__exit__` zawsze przywraca `active = False`, nawet przy wyjątku).
## Uruchomienie testów
```bash
make test MOD=01-core/09-design-patterns
```
@@ -0,0 +1,26 @@
from collections.abc import Callable
from types import TracebackType
def percentage_discount(percent: int) -> Callable[[int], int]:
raise NotImplementedError("Zwróć funkcję obliczającą cenę po rabacie percent%")
def apply_discount(price_cents: int, strategy: Callable[[int], int]) -> int:
raise NotImplementedError("Zastosuj strategy do price_cents (nie zwracaj wartości < 0)")
class ManagedFlag:
def __init__(self) -> None:
self.active: bool = False
def __enter__(self) -> "ManagedFlag":
raise NotImplementedError("Ustaw self.active = True i zwróć self")
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
raise NotImplementedError("Ustaw self.active = False")
@@ -0,0 +1,16 @@
import pytest
from patterns_ex import ManagedFlag, apply_discount, percentage_discount
def test_strategy_discount() -> None:
discount_20 = percentage_discount(20)
assert apply_discount(1000, discount_20) == 800
def test_context_manager_cleans_up_even_on_error() -> None:
flag = ManagedFlag()
assert flag.active is False
with pytest.raises(RuntimeError), flag as entered:
assert entered.active is True
raise RuntimeError("boom")
assert flag.active is False