latecomers/latecomers/serializer.py

42 lines
1.1 KiB
Python
Raw Normal View History

2022-08-24 12:43:32 +02:00
# -*- 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",
2022-09-07 23:44:53 +02:00
"fr24_landing_time": "Ora atterraggio (FlightRadar24)",
2022-08-24 12:43:32 +02:00
}
df = pd.DataFrame(data, columns=mapping)
2022-08-25 21:35:37 +02:00
df["status"] = df["status"].map(lambda x: x.value)
df.set_index("th_arrival", inplace=True)
2022-08-25 23:05:07 +02:00
df.rename(columns=mapping, inplace=True)
df = df.rename_axis(index=mapping["th_arrival"])
2022-08-24 12:43:32 +02:00
with NamedTemporaryFile() as tmp:
df.to_excel(tmp)
tmp.seek(0)
content = tmp.read()
return content