Skip to content
Snippets Groups Projects
Verified Commit 9c82264e authored by Marco Aceti's avatar Marco Aceti
Browse files

Add _fetched attribute to Train and repr() support

parent 99675a3a
No related branches found
No related tags found
No related merge requests found
Pipeline #2625 failed
from src.scraper.api import BadRequestException, ViaggiaTrenoAPI from src.scraper.api import BadRequestException, ViaggiaTrenoAPI
# from src.scraper.station import Station
# from src.scraper.train import Train
...@@ -26,3 +26,28 @@ def test_fetch(kind, station_code): ...@@ -26,3 +26,28 @@ def test_fetch(kind, station_code):
) )
for train in trains: for train in trains:
train.fetch() train.fetch()
def test_unfetched_repr_1():
milan: Station = Station.by_code("S01700")
train: Train = Train(10911, milan)
assert (
repr(train)
== "Treno [?] ??? 10911 : Stazione di Milano Centrale [S01700@1] -> ???"
)
def test_unfetched_repr_2():
train: Train = Train._from_station_departures_arrivals(
{
"numeroTreno": 10911,
"codOrigine": "S01700",
"categoriaDescrizione": "REG",
"nonPartito": False,
"provvedimento": 0,
}
)
assert (
repr(train)
== "Treno [D] REG 10911 : Stazione di Milano Centrale [S01700@1] -> ???"
)
...@@ -23,7 +23,10 @@ class Train: ...@@ -23,7 +23,10 @@ class Train:
delay (int | None): instantaneous delay of the train, based on last detection delay (int | None): instantaneous delay of the train, based on last detection
last_detection_place (str | None): place of last detection, it can be a station (or a stop) last_detection_place (str | None): place of last detection, it can be a station (or a stop)
last_detection_time (datetime | None): time of last detection last_detection_time (datetime | None): time of last detection
Meta attributes:
_phantom (bool): true if no more data can be fetched (e.g. train is cancelled) _phantom (bool): true if no more data can be fetched (e.g. train is cancelled)
_fetched (datetime | None): the last time the data has been fetched successfully
""" """
def __init__(self, number: int, origin: st.Station) -> None: def __init__(self, number: int, origin: st.Station) -> None:
...@@ -50,6 +53,7 @@ class Train: ...@@ -50,6 +53,7 @@ class Train:
self.last_detection_time: datetime | None = None self.last_detection_time: datetime | None = None
self._phantom: bool = False self._phantom: bool = False
self._fetched: datetime | None = None
@classmethod @classmethod
def _from_station_departures_arrivals(cls, train_data: dict) -> "Train": def _from_station_departures_arrivals(cls, train_data: dict) -> "Train":
...@@ -130,12 +134,19 @@ class Train: ...@@ -130,12 +134,19 @@ class Train:
# There should always be at least two stops: the first and the last. # There should always be at least two stops: the first and the last.
assert len(self.stops) >= 2 assert len(self.stops) >= 2
self._fetched = datetime.now()
def __repr__(self) -> str: def __repr__(self) -> str:
if not self._fetched:
if self.departed and self.category:
return f"Treno [{'D' if self.departed else 'S'}] {self.category} {self.number} : {self.origin} -> ???"
else:
return f"Treno [?] ??? {self.number} : {self.origin} -> ???"
if self._phantom: if self._phantom:
return f"Treno [?] {self.category} {self.number} : {self.origin} -> ?" return f"Treno [?] {self.category} {self.number} : {self.origin} -> ?"
assert isinstance(self.stops, list) assert isinstance(self.stops, list)
return ( return (
f"Treno [{'D' if self.departed else 'S'}{'X' if self.cancelled else ''}] " f"Treno [{'D' if self.departed else 'S'}{'X' if self.cancelled else ''}] "
f"{self.category} {self.number} : {self.origin} -> {self.destination}" f"{self.category} {self.number} : {self.origin} -> {self.destination}"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment