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 @@
# 06 — Obiektowość, `@dataclass` i `Protocol`
## Zadania (`oop_ex.py`)
1. `BankAccount` — enkapsulacja salda (`_balance`), metody `deposit`, `withdraw -> bool`, właściwość `balance`.
2. `Notifier` (`Protocol`) + `EmailNotifier`, `SmsNotifier` + `AlertService` (kompozycja).
## Uruchomienie testów
```bash
make test MOD=01-core/06-oop-dataclasses
```
@@ -0,0 +1,45 @@
from dataclasses import dataclass
from typing import Protocol
class BankAccount:
def __init__(self, initial_balance: int = 0) -> None:
self._balance = initial_balance
@property
def balance(self) -> int:
return self._balance
def deposit(self, amount: int) -> None:
raise NotImplementedError("Dodaj amount do _balance (odrzuć <= 0 przez ValueError)")
def withdraw(self, amount: int) -> bool:
raise NotImplementedError("Wypłać jeśli _balance >= amount i zwróć True, inaczej False")
class Notifier(Protocol):
def format_message(self, msg: str) -> str: ...
@dataclass(frozen=True)
class EmailNotifier:
address: str
def format_message(self, msg: str) -> str:
raise NotImplementedError("Zwróć '[email] <msg>'")
@dataclass(frozen=True)
class SmsNotifier:
phone: str
def format_message(self, msg: str) -> str:
raise NotImplementedError("Zwróć '[sms] <msg>'")
class AlertService:
def __init__(self, notifiers: list[Notifier]) -> None:
self._notifiers = notifiers
def broadcast(self, msg: str) -> list[str]:
raise NotImplementedError("Wywołaj format_message(msg) dla każdego notifiera")
@@ -0,0 +1,16 @@
from oop_ex import AlertService, BankAccount, EmailNotifier, SmsNotifier
def test_bank_account() -> None:
acc = BankAccount(100)
acc.deposit(50)
assert acc.balance == 150
assert acc.withdraw(40) is True
assert acc.balance == 110
assert acc.withdraw(500) is False
assert acc.balance == 110
def test_alert_service_protocol_composition() -> None:
svc = AlertService([EmailNotifier("ops@example.com"), SmsNotifier("+48123456789")])
assert svc.broadcast("CPU 95%") == ["[email] CPU 95%", "[sms] CPU 95%"]