mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-03-13 20:56:54 +01:00
module 9 ex1
This commit is contained in:
@@ -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__":
|
||||
|
||||
Reference in New Issue
Block a user