commit 0608802d94ff6554ba3a9670106940efa2351749 Author: rvolovoy Date: Thu Nov 5 16:53:53 2026 +0100 data processor humble beginning diff --git a/data_processor.py b/data_processor.py new file mode 100644 index 0000000..e4e67d0 --- /dev/null +++ b/data_processor.py @@ -0,0 +1,65 @@ +from abc import ABC, abstractmethod +from typing import Any, override + + +class NotValidatedIngestException(Exception): + def __init__(self, dataType: str): + super().__init__(f"Improper {dataType} data") + + +class DataProcessor(ABC): + @abstractmethod + def validate(self, data: Any) -> bool: + pass + + @abstractmethod + def ingest(self, data: Any) -> None: + pass + + def output(self) -> tuple[int, str]: + return (0, "abc") + + +class NumericProcessor(DataProcessor): + def isElementValid(self, data: Any) -> bool: + return data is int or data is float + + @override + def validate(self, data: Any) -> bool: + if type(data) is not list: + return self.isElementValid(data) + for elem in data: + if not self.isElementValid(elem): + return False + return True + + @override + def ingest(self, data: Any) -> None: + if not self.validate(data): + raise NotValidatedIngestException("numeric") + + +def testValidateInput(processor: DataProcessor, data: Any) -> None: + ok = processor.validate(data) + print(f" Trying to validate input '{data}': {ok}") + + +def testInvalidIngestion( + processor: DataProcessor, data: Any, dataType: str +) -> None: + print( + f"Test invalid ingestion of {dataType} '{data}' without " + "prior validation:" + ) + try: + processor.ingest(data) + except NotValidatedIngestException as e: + print(f"Got exception: {e}") + + +if __name__ == "__main__": + print("=== Code Nexus - Data Processor ===\n") + numeric = NumericProcessor() + testValidateInput(numeric, 42) + testValidateInput(numeric, "Hello") + testInvalidIngestion(numeric, "foo", "string")