Skip to content

Commit 3ccfea9

Browse files
committed
Complete rewrite of config handling
1 parent 48a61ad commit 3ccfea9

44 files changed

Lines changed: 586 additions & 621 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"**/.pytest_cache": true,
88
"**/.mypy_cache": true
99
},
10-
"python.analysis.typeCheckingMode": "standard",
10+
"python.analysis.typeCheckingMode": "basic",
1111
"python.testing.pytestArgs": [
1212
"tests"
1313
],
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Config:
2+
def __init__(self, data: dict) -> None:
3+
self.extend_field_links([])
4+
for field_name, field_type in self.field_links:
5+
self.__setattr__(field_name, field_type(data[field_name]))
6+
7+
def serialize(self) -> dict:
8+
return {
9+
field_name: getattr(self, field_name) for field_name, _ in self.field_links
10+
}
11+
12+
def extend_field_links(self, new_field_links: list[tuple[str, type]]) -> None:
13+
existing_field_links = getattr(self, "field_links", None)
14+
if existing_field_links is None:
15+
self.field_links = []
16+
self.field_links += new_field_links

bears_flight_simulation/core/flight_simulation.py

Lines changed: 83 additions & 82 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
from abc import ABC, abstractmethod
22

3+
from bears_flight_simulation.core.config import Config
34

4-
class LibraryEntry(ABC):
5-
id: str
65

6+
class LibraryEntry(ABC, Config):
77
def __init__(self, data: dict) -> None:
8-
# Load identifier
9-
self.id = str(data["ID"])
8+
self.extend_field_links([("id", str)])
9+
super().__init__(data)
1010

1111
@classmethod
1212
@abstractmethod
1313
def new_default(cls, id: str) -> "LibraryEntry":
1414
raise NotImplementedError
15-
16-
@abstractmethod
17-
def serialize(self) -> dict:
18-
raise NotImplementedError

bears_flight_simulation/core/location_library.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
from bears_flight_simulation.core.library import Library
44
from bears_flight_simulation.core.library_entry import LibraryEntry
5-
from bears_flight_simulation.parsers.location import Location
5+
from bears_flight_simulation.parsers.location_config import LocationConfig
66

77

88
class LocationLibrary(Library):
99
def load_entry(self, data: dict) -> LibraryEntry:
10-
return Location(data)
10+
return LocationConfig(data)
1111

1212
def new_entry(self) -> LibraryEntry:
1313
id = str(uuid4())
14-
entry = Location.new_default(id)
14+
entry = LocationConfig.new_default(id)
1515
self.entries[id] = entry
1616
self.save()
1717
return entry

bears_flight_simulation/parsers/airbrake_config.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111

1212

1313
class AirbrakeConfig(LibraryEntry):
14-
sampling_rate_hz: float
15-
controller_name: str
1614
controller_function: t.Callable
17-
drag_curve_filename_without_folder: str
1815
drag_curve_filepath: Path
19-
drag_curve_standard_deviation_factor: float
2016

2117
def __init__(self, data: dict, airbrake_folder: Path) -> None:
18+
self.extend_field_links(
19+
[
20+
("sampling_rate_hz", float),
21+
("controller_function_name", str),
22+
("drag_curve_file", str),
23+
("drag_curve_standard_deviation_factor", float),
24+
]
25+
)
2226
super().__init__(data)
2327

24-
self.sampling_rate_hz = float(data["sampling_rate_hz"])
25-
26-
self.controller_name = str(data["controller_function_name"])
27-
28-
match self.controller_name:
28+
match self.controller_function_name: # type: ignore
2929
case "disabled_controller":
3030
self.controller_function = disabled_controller
3131
case "enabled_controller":
@@ -37,30 +37,17 @@ def __init__(self, data: dict, airbrake_folder: Path) -> None:
3737
case _:
3838
raise NotImplementedError
3939

40-
self.drag_curve_filename_without_folder = str(data["drag_curve_file"])
4140
self.drag_curve_filepath = airbrake_folder / str(data["drag_curve_file"])
42-
self.drag_curve_standard_deviation_factor = float(
43-
data["drag_curve_standard_deviation_factor"]
44-
)
4541

4642
@classmethod
4743
def new_default(cls, id: str) -> "AirbrakeConfig":
4844
return AirbrakeConfig(
4945
{
50-
"ID": id,
46+
"id": id,
5147
"sampling_rate_hz": 10.0,
5248
"controller_function_name": "disabled_controller",
5349
"drag_curve_file": "stargaze_airbrake_drag_curve.csv",
5450
"drag_curve_standard_deviation_factor": 0.1,
5551
},
5652
airbrake_folder=Path(""),
5753
)
58-
59-
def serialize(self) -> dict:
60-
return {
61-
"ID": self.id,
62-
"sampling_rate_hz": self.sampling_rate_hz,
63-
"controller_function_name": self.controller_name,
64-
"drag_curve_file": self.drag_curve_filename_without_folder,
65-
"drag_curve_standard_deviation_factor": self.drag_curve_standard_deviation_factor,
66-
}

bears_flight_simulation/parsers/fins_config.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,47 @@
22

33
import yaml
44

5+
from bears_flight_simulation.core.library_entry import LibraryEntry
56

6-
class FinsConfig:
7-
n: int
8-
root_chord: float
9-
tip_chord: float
10-
span: float
11-
position: float
12-
cant_angle: float
7+
8+
class FinsConfig(LibraryEntry):
139
sweep_length: t.Union[float, None]
1410
sweep_angle: t.Union[float, None]
1511
radius: t.Union[float, None]
1612

17-
def __init__(self, fins_config_file: t.TextIO) -> None:
18-
# Load yaml file
19-
data = yaml.safe_load(fins_config_file)
13+
def __init__(self, data: dict) -> None:
14+
self.extend_field_links(
15+
[
16+
("n", int),
17+
("root_chord", float),
18+
("tip_chord", float),
19+
("span", float),
20+
("position", float),
21+
("cant_angle", float),
22+
]
23+
)
24+
super().__init__(data)
2025

21-
# Load fins data
22-
self.n = int(data["n"])
23-
self.root_chord = float(data["root_chord"])
24-
self.tip_chord = float(data["tip_chord"])
25-
self.span = float(data["span"])
26-
self.position = float(data["position"])
27-
self.cant_angle = float(data["cant_angle"])
2826
self.sweep_length = data["sweep_length"]
29-
self.sweep_angle = data["sweep_angle"]
30-
self.radius = data["radius"]
31-
32-
# Ensure types for multi-type attributes
3327
assert type(self.sweep_length) in [float, type(None)]
28+
self.sweep_angle = data["sweep_angle"]
3429
assert type(self.sweep_angle) in [float, type(None)]
30+
self.radius = data["radius"]
3531
assert type(self.radius) in [float, type(None)]
32+
33+
@classmethod
34+
def new_default(cls, id: str) -> LibraryEntry:
35+
return FinsConfig(
36+
{
37+
"id": id,
38+
"n": 3,
39+
"root_chord": 0.236,
40+
"tip_chord": 0.136,
41+
"span": 0.091,
42+
"position": 0.246,
43+
"cant_angle": 0.0,
44+
"sweep_length": 0.0498,
45+
"sweep_angle": None,
46+
"radius": None,
47+
}
48+
)

bears_flight_simulation/parsers/location.py

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from bears_flight_simulation.core.library_entry import LibraryEntry
2+
3+
4+
class LocationConfig(LibraryEntry):
5+
def __init__(self, data: dict) -> None:
6+
self.extend_field_links(
7+
[
8+
("latitude", float),
9+
("longitude", float),
10+
("elevation", float),
11+
]
12+
)
13+
super().__init__(data)
14+
15+
@classmethod
16+
def new_default(cls, id: str) -> "LocationConfig":
17+
return LocationConfig(
18+
{
19+
"id": id,
20+
"latitude": 0.0,
21+
"longitude": 0.0,
22+
"elevation": 0.0,
23+
}
24+
)

0 commit comments

Comments
 (0)