mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-03-14 05:06:55 +01:00
Compare commits
3 Commits
384ad04a08
...
2abc1adcf4
| Author | SHA1 | Date | |
|---|---|---|---|
| 2abc1adcf4 | |||
| 616258b4f5 | |||
| ab6fc8ed90 |
@@ -1,6 +1,8 @@
|
|||||||
|
from typing_extensions import Self
|
||||||
import datetime
|
import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from pydantic import BaseModel, Field, model_validator
|
from pydantic import BaseModel, Field, model_validator
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
class ContactType(Enum):
|
class ContactType(Enum):
|
||||||
@@ -15,19 +17,58 @@ class AlienContact(BaseModel):
|
|||||||
timestamp: datetime.datetime
|
timestamp: datetime.datetime
|
||||||
location: str = Field(min_length=3, max_length=100)
|
location: str = Field(min_length=3, max_length=100)
|
||||||
contact_type: ContactType
|
contact_type: ContactType
|
||||||
signal_strength: float = Field(le=0.0, ge=10.0)
|
signal_strength: float = Field(ge=0.0, le=10.0)
|
||||||
duration_minutes: int = Field(le=1, ge=1440)
|
duration_minutes: int = Field(ge=1, le=1440)
|
||||||
witness_count: int = Field(le=1, ge=100)
|
witness_count: int = Field(ge=1, le=100)
|
||||||
message_received: str | None = Field(default=None, max_length=500)
|
message_received: str | None = Field(default=None, max_length=500)
|
||||||
is_verified: bool = Field(default=False)
|
is_verified: bool = Field(default=False)
|
||||||
|
|
||||||
@model_validator(mode="after")
|
@model_validator(mode="after")
|
||||||
def format_validation(cls):
|
def format_validation(self) -> Self:
|
||||||
pass
|
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:
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
97
09/ex2/space_crew.py
Normal file
97
09/ex2/space_crew.py
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
from enum import Enum
|
||||||
|
from typing_extensions import Self
|
||||||
|
from pydantic import BaseModel, Field, model_validator
|
||||||
|
from datetime import datetime
|
||||||
|
from json import load
|
||||||
|
|
||||||
|
|
||||||
|
class Rank(Enum):
|
||||||
|
CADET = "cadet"
|
||||||
|
OFFICER = "officer"
|
||||||
|
LIEUTENANT = "lieutenant"
|
||||||
|
CAPTAIN = "captain"
|
||||||
|
COMMANDER = "commander"
|
||||||
|
|
||||||
|
|
||||||
|
class CrewMember(BaseModel):
|
||||||
|
member_id: str = Field(min_length=3, max_length=10)
|
||||||
|
name: str = Field(min_length=2, max_length=50)
|
||||||
|
rank: Rank
|
||||||
|
age: int = Field(ge=18, le=80)
|
||||||
|
specialization: str = Field(min_length=3, max_length=30)
|
||||||
|
years_experience: int = Field(ge=0, le=50)
|
||||||
|
is_active: bool = Field(default=True)
|
||||||
|
|
||||||
|
|
||||||
|
class SpaceMission(BaseModel):
|
||||||
|
mission_id: str = Field(min_length=5, max_length=15)
|
||||||
|
mission_name: str = Field(min_length=3, max_length=100)
|
||||||
|
destination: str = Field(min_length=3, max_length=50)
|
||||||
|
launch_date: datetime
|
||||||
|
duration_days: int = Field(ge=1, le=3650)
|
||||||
|
crew: list[CrewMember] = Field(min_length=1, max_length=12)
|
||||||
|
mission_status: str = Field(default="planned")
|
||||||
|
budget_millions: float = Field(ge=1.0, le=10000.0)
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def validation(self) -> Self:
|
||||||
|
if not self.mission_id.startswith("M"):
|
||||||
|
raise ValueError('Mission ID must start with "M"')
|
||||||
|
try:
|
||||||
|
crew = {member.rank: member for member in self.crew}
|
||||||
|
crew[Rank.CAPTAIN]
|
||||||
|
crew[Rank.COMMANDER]
|
||||||
|
except KeyError:
|
||||||
|
raise ValueError("Must have at least one Commander or Captain")
|
||||||
|
if (
|
||||||
|
self.duration_days > 365
|
||||||
|
and len(
|
||||||
|
[
|
||||||
|
member
|
||||||
|
for member in self.crew
|
||||||
|
if member.years_experience >= 5
|
||||||
|
]
|
||||||
|
)
|
||||||
|
< len(self.crew) / 2
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
"Long missions (> 365 days) need 50% "
|
||||||
|
"experienced crew (5+ years)"
|
||||||
|
)
|
||||||
|
for member in self.crew:
|
||||||
|
if member.is_active == False:
|
||||||
|
raise ValueError("All crew members must be active")
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
try:
|
||||||
|
with open("../generated_data/space_missions.json") as file:
|
||||||
|
data = load(file)
|
||||||
|
for mission in data:
|
||||||
|
try:
|
||||||
|
print("=========================================")
|
||||||
|
sp = SpaceMission(**mission)
|
||||||
|
print(
|
||||||
|
"Valid mission created:\n"
|
||||||
|
f"Mission: {sp.mission_name}\n"
|
||||||
|
f"ID: {sp.mission_id}\n"
|
||||||
|
f"Destination: {sp.destination}\n"
|
||||||
|
f"Duration: {sp.duration_days} days\n"
|
||||||
|
f"Budget: ${sp.budget_millions}M\n"
|
||||||
|
f"Crew size: {len(sp.crew)}\n"
|
||||||
|
"Crew members:"
|
||||||
|
)
|
||||||
|
for member in sp.crew:
|
||||||
|
print(
|
||||||
|
f"- {member.name} ({member.rank}) - "
|
||||||
|
f"{member.specialization}"
|
||||||
|
)
|
||||||
|
except Exception as err:
|
||||||
|
print(err)
|
||||||
|
except Exception as err:
|
||||||
|
print(err)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
14
10/ex0/lambda_spells.py
Normal file
14
10/ex0/lambda_spells.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
def artifact_sorter(artifacts: list[dict]) -> list[dict]:
|
||||||
|
return sorted(artifacts, key=lambda artifact: artifact["power"])
|
||||||
|
|
||||||
|
|
||||||
|
def power_filter(mages: list[dict], min_power: int) -> list[dict]:
|
||||||
|
return list(filter(lambda x: x["power"] >= min_power, mages))
|
||||||
|
|
||||||
|
|
||||||
|
def spell_transformer(spells: list[str]) -> list[str]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def mage_stats(mages: list[dict]) -> dict:
|
||||||
|
pass
|
||||||
Reference in New Issue
Block a user