Add artifact.001/src/artifact_001/core.py
This commit is contained in:
commit
d22fd971d9
1 changed files with 72 additions and 0 deletions
72
artifact.001/src/artifact_001/core.py
Normal file
72
artifact.001/src/artifact_001/core.py
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from datetime import datetime
|
||||||
|
from dataclasses import dataclass, asdict
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MeteorEvent:
|
||||||
|
"""Repräsentiert ein einzelnes Meteorereignis."""
|
||||||
|
timestamp: datetime
|
||||||
|
intensity: float
|
||||||
|
exposure_time: float
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
assert isinstance(self.timestamp, datetime), "timestamp muss datetime sein"
|
||||||
|
assert isinstance(self.intensity, (float, int)), "intensity muss eine Zahl sein"
|
||||||
|
assert isinstance(self.exposure_time, (float, int)), "exposure_time muss eine Zahl sein"
|
||||||
|
|
||||||
|
|
||||||
|
def capture_meteors(exposure_time: float) -> List[MeteorEvent]:
|
||||||
|
"""Erfasst Meteorergebnisse mit der angegebenen Belichtungszeit.
|
||||||
|
|
||||||
|
Dies ist eine Simulation, die in einer realen Anwendung die Sensoren ansprechen würde.
|
||||||
|
"""
|
||||||
|
if not isinstance(exposure_time, (float, int)):
|
||||||
|
raise TypeError("exposure_time muss float oder int sein")
|
||||||
|
if exposure_time <= 0:
|
||||||
|
raise ValueError("exposure_time muss positiv sein")
|
||||||
|
|
||||||
|
# Simulierter Sensor-Input: Erzeuge einige zufällige Intensitätswerte
|
||||||
|
import random
|
||||||
|
events = []
|
||||||
|
for _ in range(random.randint(1, 3)):
|
||||||
|
intensity = random.uniform(0.1, 1.0)
|
||||||
|
events.append(
|
||||||
|
MeteorEvent(
|
||||||
|
timestamp=datetime.now(),
|
||||||
|
intensity=intensity,
|
||||||
|
exposure_time=float(exposure_time),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return events
|
||||||
|
|
||||||
|
|
||||||
|
def log_data(data: List[MeteorEvent]) -> None:
|
||||||
|
"""Protokolliert erfasste Meteordaten in einer JSON-Datei."""
|
||||||
|
if not isinstance(data, list):
|
||||||
|
raise TypeError("data muss eine Liste sein")
|
||||||
|
for item in data:
|
||||||
|
if not isinstance(item, MeteorEvent):
|
||||||
|
raise TypeError("Alle Elemente in data müssen MeteorEvent-Objekte sein")
|
||||||
|
|
||||||
|
output_dir = Path("output")
|
||||||
|
output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
output_path = output_dir / "meteor_events.json"
|
||||||
|
|
||||||
|
try:
|
||||||
|
json_data = [
|
||||||
|
{
|
||||||
|
**asdict(event),
|
||||||
|
"timestamp": event.timestamp.isoformat(),
|
||||||
|
}
|
||||||
|
for event in data
|
||||||
|
]
|
||||||
|
with open(output_path, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(json_data, f, ensure_ascii=False, indent=2)
|
||||||
|
except OSError as e:
|
||||||
|
raise IOError(f"Fehler beim Schreiben der Datei {output_path}: {e}")
|
||||||
|
|
||||||
|
return None
|
||||||
Loading…
Reference in a new issue