Files
kabellaengen/lib/model.py
T

66 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from dataclasses import dataclass, asdict, fields
from dacite import from_dict
from typing import List
import json
@dataclass
class Sensor:
id: str
x: float
y: float
@dataclass
class Aktor:
id: str
x: float
y: float
@dataclass
class Pritsche:
id: str
x: float
y: float
@dataclass
class Anlage:
name: str
sensoren: List[Sensor]
aktoren: List[Aktor]
kabelpritschen: List[Pritsche]
def to_json(self, pretty: bool = True) -> str:
d = self.to_dict()
return json.dumps(d, indent=2 if pretty else None, default=str)
def to_dict(self) -> dict:
"""Konvertiert das Objekt in ein dict datetime wird als ISO-String dargestellt."""
result = asdict(self)
# hier um irgendwas ergänzen
# result["startdatum"] = self.startdatum.isoformat()
return result
if __name__ == '__main__':
json_string = '''{
"name": "H&M",
"sensoren": [
{"id": "AP4321", "x": 14, "y": 50},
{"id": "AP4322", "x": 22, "y": 100}
],
"aktoren": [
{"id": "AP4321", "x": 14, "y": 50},
{"id": "AP4322", "x": 22, "y": 100}
],
"kabelpritschen": [
{"id": "p1", "x": 1, "y": 0},
{"id": "p2", "x": 22, "y": 10}
]
}
'''
data = json.loads(json_string)
anlage = from_dict(
data_class=Anlage,
data=data
)
print(anlage)