Spaces:
Sleeping
Sleeping
Keshavj-13 commited on
Commit ·
73dbcfd
1
Parent(s): 2d25aa3
refactor: consolidate import patterns (Issue #26)
Browse files- engine.py +11 -3
- imports.py +51 -0
engine.py
CHANGED
|
@@ -7,6 +7,7 @@ from dataclasses import dataclass
|
|
| 7 |
from statistics import pstdev
|
| 8 |
from typing import Dict, Iterable, List, Tuple
|
| 9 |
|
|
|
|
| 10 |
try:
|
| 11 |
from .models import (
|
| 12 |
FlightRecord,
|
|
@@ -16,6 +17,15 @@ try:
|
|
| 16 |
TaskDefinition,
|
| 17 |
TaskMetrics,
|
| 18 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except ImportError:
|
| 20 |
from models import (
|
| 21 |
FlightRecord,
|
|
@@ -25,9 +35,7 @@ except ImportError:
|
|
| 25 |
TaskDefinition,
|
| 26 |
TaskMetrics,
|
| 27 |
)
|
| 28 |
-
|
| 29 |
-
try:
|
| 30 |
-
from .constants import (
|
| 31 |
SEPARATION_BY_WAKE,
|
| 32 |
SCORE_WEIGHTS,
|
| 33 |
METRIC_PRECISION,
|
|
|
|
| 7 |
from statistics import pstdev
|
| 8 |
from typing import Dict, Iterable, List, Tuple
|
| 9 |
|
| 10 |
+
# Import models with automatic relative/absolute fallback
|
| 11 |
try:
|
| 12 |
from .models import (
|
| 13 |
FlightRecord,
|
|
|
|
| 17 |
TaskDefinition,
|
| 18 |
TaskMetrics,
|
| 19 |
)
|
| 20 |
+
from .constants import (
|
| 21 |
+
SEPARATION_BY_WAKE,
|
| 22 |
+
SCORE_WEIGHTS,
|
| 23 |
+
METRIC_PRECISION,
|
| 24 |
+
FUEL_PRECISION,
|
| 25 |
+
AIRLINE_DELAY_PRECISION,
|
| 26 |
+
MAX_DIAGNOSTICS,
|
| 27 |
+
MAX_RECOMMENDATIONS,
|
| 28 |
+
)
|
| 29 |
except ImportError:
|
| 30 |
from models import (
|
| 31 |
FlightRecord,
|
|
|
|
| 35 |
TaskDefinition,
|
| 36 |
TaskMetrics,
|
| 37 |
)
|
| 38 |
+
from constants import (
|
|
|
|
|
|
|
| 39 |
SEPARATION_BY_WAKE,
|
| 40 |
SCORE_WEIGHTS,
|
| 41 |
METRIC_PRECISION,
|
imports.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Import utilities for relative/absolute import compatibility."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import Any
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def safe_import(relative_import_func, absolute_import_func) -> Any:
|
| 9 |
+
"""
|
| 10 |
+
Attempt relative import, fall back to absolute import.
|
| 11 |
+
|
| 12 |
+
This eliminates boilerplate try/except blocks across modules.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
relative_import_func: Callable that performs relative import (e.g., from .module import ...)
|
| 16 |
+
absolute_import_func: Callable that performs absolute import (e.g., from module import ...)
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
The imported module or object
|
| 20 |
+
|
| 21 |
+
Example:
|
| 22 |
+
models = safe_import(
|
| 23 |
+
lambda: __import__('.models', fromlist=['FlightRecord']).FlightRecord,
|
| 24 |
+
lambda: __import__('models', fromlist=['FlightRecord']).FlightRecord,
|
| 25 |
+
)
|
| 26 |
+
"""
|
| 27 |
+
try:
|
| 28 |
+
return relative_import_func()
|
| 29 |
+
except ImportError:
|
| 30 |
+
return absolute_import_func()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Convenience exports for models module
|
| 34 |
+
def get_models():
|
| 35 |
+
"""Get models module with automatic relative/absolute import fallback."""
|
| 36 |
+
try:
|
| 37 |
+
from . import models as _models
|
| 38 |
+
return _models
|
| 39 |
+
except ImportError:
|
| 40 |
+
import models as _models
|
| 41 |
+
return _models
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def get_constants():
|
| 45 |
+
"""Get constants module with automatic relative/absolute import fallback."""
|
| 46 |
+
try:
|
| 47 |
+
from . import constants as _constants
|
| 48 |
+
return _constants
|
| 49 |
+
except ImportError:
|
| 50 |
+
import constants as _constants
|
| 51 |
+
return _constants
|