Module 05 finish + mypy strict + flake

This commit is contained in:
2026-02-13 15:10:02 +01:00
parent c44a72413e
commit b68b508a04
3 changed files with 112 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
from abc import ABC
from typing import Any, List, Protocol, Union
from typing import Any, Dict, List, Protocol, Union
from typing_extensions import override
@@ -9,18 +9,35 @@ class ProcessingStage(Protocol):
class InputStage:
def process(self, data: Any) -> Dict:
pass
def process(self, data: Any) -> Dict[int, float]:
res: Dict[int, float] = {}
i = 0
for n in data:
try:
if not isinstance(n, float):
n = float(n)
res[i] = n
i += 1
except ValueError:
continue
return res
class TransformStage:
def process(self, data: Any) -> Dict:
pass
def process(self, data: Any) -> Dict[str, float]:
res: Dict[str, Union[int, float]] = {}
res["processed_data"] = len(data)
try:
res["avg"] = sum(data.values()) / len(data)
except ZeroDivisionError:
res["avg"] = 0
return res
class OutputStage:
def process(self, data: Any) -> str:
pass
return f"Summary:\n\t\
- Processed_data: {data['processed_data']}\n\t- avg temp: {data['avg']:.1f}°C"
class ProcessingPipeline(ABC):
@@ -30,10 +47,12 @@ class ProcessingPipeline(ABC):
def add_stage(
self, stage: Union[InputStage, TransformStage, OutputStage]
) -> None:
pass
self.stages += [stage]
def process(self, data: Any) -> Any:
pass
for stage in self.stages:
data = stage.process(data)
return data
class JSONAdapter(ProcessingPipeline):
@@ -43,7 +62,10 @@ class JSONAdapter(ProcessingPipeline):
@override
def process(self, data: Any) -> Any:
return super().process(data)
res: List[Any] = []
for n in data:
res += [data[n]]
return super().process(res)
class CSVAdapter(ProcessingPipeline):
@@ -53,7 +75,7 @@ class CSVAdapter(ProcessingPipeline):
@override
def process(self, data: Any) -> Any:
return super().process(data)
return super().process(data.split(","))
class StreamAdapter(ProcessingPipeline):
@@ -63,12 +85,65 @@ class StreamAdapter(ProcessingPipeline):
@override
def process(self, data: Any) -> Any:
if not isinstance(data, List):
raise Exception
return super().process(data)
def tester():
pass
class NexusManager:
def __init__(self, pipelines: List[ProcessingPipeline]) -> None:
self.pipelines: List[ProcessingPipeline] = pipelines
def add_pipeline(self, pipeline: ProcessingPipeline) -> None:
self.pipelines += [pipeline]
def process_data(self, data: Any) -> str:
res: str | None = None
for pipeline in self.pipelines:
pipeline.add_stage(InputStage())
pipeline.add_stage(TransformStage())
pipeline.add_stage(OutputStage())
try:
res = pipeline.process(data)
break
except Exception:
continue
if res is None:
return "[ERROR] Unknown format, incompatible with pipelines"
return res
def tester() -> None:
print("=== CODE NEXUS - ENTERPRISE PIPELINE SYSTEM ===\n")
print("Initializing Nexus Manager...")
manager = NexusManager(
[
JSONAdapter("JSON_01"),
CSVAdapter("CSV_01"),
StreamAdapter("Stream_01"),
]
)
print("\n=== test JSONAdapter ===")
data: Any = {"temp": 10, "est": "10.3", "t": 10.6, "p": "Hello"}
print(f"input: {data}")
res = manager.process_data(data)
print(f"Output: {res}\n")
print("\n=== test CSVAdapter ===")
data = "10,20,40,30"
print(f'input: "{data}"')
res = manager.process_data(data)
print(f"Output: {res}\n")
print("\n=== test StreamAdapter ===")
data = [10, 30, 0, "100"]
print(f"input: {data}")
res = manager.process_data(data)
print(f"Output: {res}\n")
print("\n=== test Invalid input ===")
data = 10
print(f"input: {data}")
res = manager.process_data(data)
print(f"Output: {res}\n")
if __name__ == "__main__":
pass
tester()