From ab6fc8ed90488c443fdd6b524970c6490dd85296 Mon Sep 17 00:00:00 2001 From: David GAILLETON Date: Thu, 12 Mar 2026 17:11:34 +0100 Subject: [PATCH] module 9 ex1 --- 09/ex1/alien_contact.py | 53 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/09/ex1/alien_contact.py b/09/ex1/alien_contact.py index 9ef403a..a852830 100644 --- a/09/ex1/alien_contact.py +++ b/09/ex1/alien_contact.py @@ -1,6 +1,8 @@ +from typing_extensions import Self import datetime from enum import Enum from pydantic import BaseModel, Field, model_validator +import json class ContactType(Enum): @@ -15,19 +17,58 @@ class AlienContact(BaseModel): timestamp: datetime.datetime location: str = Field(min_length=3, max_length=100) contact_type: ContactType - signal_strength: float = Field(le=0.0, ge=10.0) - duration_minutes: int = Field(le=1, ge=1440) - witness_count: int = Field(le=1, ge=100) + signal_strength: float = Field(ge=0.0, le=10.0) + duration_minutes: int = Field(ge=1, le=1440) + witness_count: int = Field(ge=1, le=100) message_received: str | None = Field(default=None, max_length=500) is_verified: bool = Field(default=False) @model_validator(mode="after") - def format_validation(cls): - pass + def format_validation(self) -> Self: + if not self.contact_id.startswith("AC"): + raise ValueError('Contact ID must start with "AC"') + if ( + self.contact_type == ContactType.PHYSICAL + and self.is_verified == False + ): + raise ValueError("Physical contact reports must be verified") + if ( + self.contact_type == ContactType.TELEPATHIC + and self.witness_count < 3 + ): + raise ValueError( + "Telepathic contact requires at least 3 witnesses" + ) + if self.signal_strength > 7.0 and not self.message_received: + raise ValueError( + "Strong signals (> 7.0) should include received messages" + ) + return self def main() -> None: - pass + try: + with open("../generated_data/alien_contacts.json") as file: + data = json.load(file) + for contact in data: + try: + ac = AlienContact(**contact) + print( + f"======================================\n" + f"Valid contact report:\n" + f"ID: {ac.contact_id}\n" + f"Type: {ac.contact_type}\n" + f"Location: {ac.location}\n" + f"Signal: {ac.signal_strength:.1f}/10\n" + f"Duration: {ac.duration_minutes} minutes\n" + f"Witness: {ac.witness_count}\n" + f"Message: {ac.message_received}\n" + f"Is verified: {ac.is_verified}\n" + ) + except Exception as err: + print(err) + except Exception as err: + print(err) if __name__ == "__main__":