-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
99 lines (80 loc) · 2.67 KB
/
models.py
File metadata and controls
99 lines (80 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Pydantic Models for testing module."""
from __future__ import annotations
import json
from functools import cache
from pathlib import Path
from pydantic import BaseModel, Field
def to_camel(snake_str):
"""Function to convert snake_case to camelCase."""
first, *others = snake_str.split("_")
return "".join([first.lower(), *map(str.title, others)])
class CamelModel(BaseModel):
"""
A model which converts to/from camelCase.
This allows models to be defined in snake_case but ingest json data
which uses camelCase.
"""
class Config:
"""Configuration for model."""
alias_generator = to_camel
allow_population_by_field_name = True
class Planet(CamelModel):
"""A model for a planet (or former planet)."""
id: int
name: str
mass: float
diameter: float
density: float
gravity: float
escape_velocity: float
rotation_period: float
length_of_day: float
distance_from_sun: float
perihelion: float
aphelion: float
orbital_period: float
orbital_velocity: float
orbital_inclination: float
orbital_eccentricity: float
obliquity_to_orbit: float
mean_temperature: float
surface_pressure: float | None
number_of_moons: int
has_ring_system: bool
has_global_magnetic_field: bool
satellites: list[Satellite] = []
class Satellite(CamelModel):
"""A model for satellites of planets."""
id: int
planet_id: int
name: str
gm: float
radius: float = Field(ge=0)
density: float | None = None
magnitude: float | None = None
albedo: float | None = Field(ge=0, le=2.0, default=None)
# Note albedo is geometric albedo; it can be gt 1 (shorturl.at/akv07)
@cache
def load_data(data_path=None):
"""Load the data into pydantic models."""
# Get default data path in non specified.
if data_path is None:
data_path = Path(__file__).absolute().parent / "data"
# Load planet and satellite data
planet_path = data_path / "planets.json"
satellite_path = data_path / "satellites.json"
with planet_path.open("r") as fi:
planet_data = json.load(fi)
planets = [Planet(**x) for x in planet_data]
with satellite_path.open("r") as fi:
satellite_data = json.load(fi)
satellites = [Satellite(**x) for x in satellite_data]
# Put planets into dict and connect satellites
planet_dict = {x.id: x for x in planets}
for satellite in satellites:
planet_dict[satellite.planet_id].satellites.append(satellite)
return planets
if __name__ == "__main__":
# test that models can ingest data.
data_path = Path(__file__).absolute().parent / "data"
planets = load_data(data_path)