style(nav): add type hints
This commit is contained in:
parent
3d44015183
commit
3da120fd28
1 changed files with 7 additions and 6 deletions
13
nav.py
13
nav.py
|
|
@ -6,6 +6,7 @@ Usage: python3 nav.py DESTINATION
|
|||
"""
|
||||
|
||||
import sys
|
||||
from collections.abc import Iterator
|
||||
|
||||
# Distances between neighboring waypoints, in million km.
|
||||
DISTANCES = {
|
||||
|
|
@ -74,20 +75,20 @@ FUEL_PER_MKM = 0.142 # tonnes of fuel per million km
|
|||
LAUNCH_FUEL = 380.0 # tonnes of fuel to escape Earth's gravity
|
||||
|
||||
|
||||
def distance(a, b):
|
||||
def distance(a: str, b: str) -> float:
|
||||
"""Return the distance between two neighboring waypoints."""
|
||||
if (a, b) in DISTANCES:
|
||||
return DISTANCES[(a, b)]
|
||||
return DISTANCES[(b, a)]
|
||||
|
||||
|
||||
def legs(route):
|
||||
def legs(route: list[str]) -> Iterator[tuple[str, str]]:
|
||||
"""Yield the legs (pairs of consecutive waypoints) of a route."""
|
||||
for i in range(1, len(route) - 1):
|
||||
yield route[i], route[i + 1]
|
||||
|
||||
|
||||
def plan(destination):
|
||||
def plan(destination: str) -> tuple[list[str], float, float, float]:
|
||||
"""Return route, distance, travel time (days) and fuel (t)."""
|
||||
route = ROUTES[destination]
|
||||
total = 0.0
|
||||
|
|
@ -101,12 +102,12 @@ def plan(destination):
|
|||
return route, total, total / CRUISE_SPEED, fuel
|
||||
|
||||
|
||||
def asteroid_route(belt):
|
||||
def asteroid_route(belt: str) -> list[str]:
|
||||
"""Return a route through the asteroid belt (currently: via Ceres)."""
|
||||
return ROUTES["ceres"] + [belt]
|
||||
|
||||
|
||||
def print_plan(destination):
|
||||
def print_plan(destination: str) -> None:
|
||||
"""Print the flight plan for a destination."""
|
||||
route, total, days, fuel = plan(destination)
|
||||
print("Route: " + " -> ".join(NAMES[w] for w in route))
|
||||
|
|
@ -115,7 +116,7 @@ def print_plan(destination):
|
|||
print(f"Fuel required: {round(fuel)} t")
|
||||
|
||||
|
||||
def main():
|
||||
def main() -> None:
|
||||
if len(sys.argv) != 2:
|
||||
sys.exit(__doc__)
|
||||
if sys.argv[1] == "--list":
|
||||
|
|
|
|||
Loading…
Reference in a new issue