app.coordinates_setup module#
Validated orchestration helpers for coordinate persistence and retrieval.
This module provides a thin, validated wrapper around coordinate seeding,
insertion, and listing. It centralizes argument checks, enforces geographic
bounds, and adds structured logging around database calls. The goal is to give
higher-level services a safe, predictable API that delegates the actual
persistence and generation logic to app.coordinates_manager.
See Also#
app.coordinates_manager.seed_coordinates_if_needed
app.coordinates_manager.get_coordinates
app.coordinates_manager.set_coordinate
app.coordinates_utils.calculate_destination_point
Notes#
Primary role: validate inputs (types and ranges) and orchestrate calls to lower-level coordinate operations (seed, list, insert).
Key dependencies: a live SQLAlchemy
Sessionand the helpers inapp.coordinates_managerfor persistence and generation.Invariants: callers own the
Sessionlifecycle. Latitude must be within[-90, 90]and longitude within[-180, 180].
Examples#
>>> # Minimal validation example
>>> from app.coordinates_setup import validate_range
>>> validate_range(0.0, "x", -1.0, 1.0)
- app.coordinates_setup.add_coordinate(session: sqlalchemy.orm.Session, latitude: float, longitude: float, label: str | None = None, is_central: bool = False) None[source]#
Add a new coordinate to the database.
- Parameters:
- session
sqlalchemy.orm.Session Database session controlling the transaction.
- latitude
float Latitude of the coordinate (decimal degrees).
- longitude
float Longitude of the coordinate (decimal degrees).
- label
str|None,optional Optional label for the coordinate.
- is_centralbool,
optional Whether the coordinate should be marked as central.
- session
- Raises:
SQLAlchemyErrorIf a database error occurs while inserting or committing.
- app.coordinates_setup.retrieve_all_coordinates(session: sqlalchemy.orm.Session) list[CoordinateSchema][source]#
Retrieve all coordinates from the database.
- Parameters:
- session
sqlalchemy.orm.Session Open database session.
- session
- Returns:
list[app.schemas.CoordinateSchema]All coordinates represented as Pydantic schemas.
- Raises:
SQLAlchemyErrorIf a database error occurs when querying.
Examples
>>> # Using a real DB session in application code >>> # coords = retrieve_all_coordinates(session) >>> # isinstance(coords, list) True
- app.coordinates_setup.seed_coordinates_if_needed(session: sqlalchemy.orm.Session, central_latitude: float, central_longitude: float) None[source]#
Seed or validate central and surrounding coordinates.
Seeds the table with a central point plus surrounding points if empty; when not empty, validates that the persisted central point matches the provided values.
- Parameters:
- session
sqlalchemy.orm.Session Database session controlling the transaction.
- central_latitude
float Latitude of the central coordinate (decimal degrees).
- central_longitude
float Longitude of the central coordinate (decimal degrees).
- session
- Raises:
RuntimeErrorIf the stored central coordinate mismatches the provided values.
SQLAlchemyErrorIf a database error occurs during persistence or queries.
See also
Examples
>>> # Typical application code (requires a real DB) >>> # from app.database import SessionLocal >>> # session = SessionLocal() >>> # seed_coordinates_if_needed(session, 59.3, 18.06)
- app.coordinates_setup.validate_range(value: float, name: str, min_value: float, max_value: float) None[source]#
Validate that a numeric value lies within an inclusive range.
- Parameters:
- Raises:
AssertionErrorIf
valueis outside[min_value, max_value].
See also
Examples
>>> validate_range(0.0, "x", -1.0, 1.0) >>> validate_range(2.0, "x", -1.0, 1.0) Traceback (most recent call last): ... AssertionError: x must be between -1.0 and 1.0, but was 2.0.