# -*- encoding: utf-8 -*- import logging from tempfile import NamedTemporaryFile import typing as T from latecomers.parse import Details from latecomers.helpers import logit import pandas as pd logger = logging.getLogger(__name__) @logit(logger) def to_excel(data: T.List[Details]) -> bytes: """ This function takes the list of parsed rows as input and returns the bytes corresponding to the excel file derived from such lines. """ mapping = { "th_arrival": "Arrivo teorico", "real_arrival": "Arrivo reale", "code": "Codice volo", "origin": "Aeroporto di partenza", "status": "Stato", "fr24_landing_time": "Ora atterraggio (FlightRadar24)", } df = pd.DataFrame(data, columns=mapping) df["status"] = df["status"].map(lambda x: x.value) df.set_index("th_arrival", inplace=True) df.rename(columns=mapping, inplace=True) df = df.rename_axis(index=mapping["th_arrival"]) with NamedTemporaryFile() as tmp: df.to_excel(tmp) tmp.seek(0) content = tmp.read() return content