Skip to content

Commit cb317e9

Browse files
committed
Make config xp optional. Fix typing
1 parent 940471b commit cb317e9

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

drone_models/utils/constants.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
import xml.etree.ElementTree as ET
66
from pathlib import Path
7-
from typing import TYPE_CHECKING, NamedTuple, Type
7+
from typing import TYPE_CHECKING, NamedTuple
8+
9+
from array_api_compat import numpy as np
810

911
if TYPE_CHECKING:
12+
from types import ModuleType
13+
1014
from array_api_typing import Array
1115

1216
# Configs (used in testing)
@@ -59,12 +63,13 @@ class Constants(NamedTuple):
5963
DI_DD_PARAMS: Array
6064
DI_DD_ACC: Array
6165

62-
@classmethod
63-
def from_file(cls, path: str, xp: Type[Array]) -> Constants:
66+
@staticmethod
67+
def from_file(path: Path, xp: ModuleType | None = None) -> Constants:
6468
"""Creates constants based on the xml file at the given location.
6569
6670
The constants are supposed to be under the costum/numeric category.
6771
"""
72+
xp = np if xp is None else xp
6873
# Constants
6974
drone_path = Path(__file__).parents[1] / path
7075
# read in all parameters from xml
@@ -116,7 +121,7 @@ def from_file(cls, path: str, xp: Type[Array]) -> Constants:
116121
DI_DD_PARAMS = xp.stack((DI_DD_ROLL, DI_DD_PITCH, DI_DD_YAW), axis=0)
117122
DI_DD_ACC = params["DI_DD_acc"]
118123

119-
return cls(
124+
return Constants(
120125
GRAVITY,
121126
GRAVITY_VEC,
122127
MASS,
@@ -153,12 +158,13 @@ def from_file(cls, path: str, xp: Type[Array]) -> Constants:
153158
DI_DD_ACC,
154159
)
155160

156-
@classmethod
157-
def from_config(cls, config: str, xp: Type[Array]) -> Constants:
161+
@staticmethod
162+
def from_config(config: str, xp: ModuleType | None = None) -> Constants:
158163
"""Creates constants based on the give configuration.
159164
160165
For available configs see Constants.available_configs
161166
"""
167+
xp = np if xp is None else xp
162168
match config:
163169
case "cf2x_L250":
164170
return Constants.from_file("models/data/cf2x_L250.xml", xp)

0 commit comments

Comments
 (0)