/
trip.py
45 lines (35 loc) · 1.35 KB
/
trip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from datetime import datetime
from .departure import Departure
from .arrival import Arrival
from .connection import Connection
class Trip:
r"""
Result object from a trip request from one station to another including interchanges
Attributes
-----------
raw :class:`dict`
Raw dict received by the API.
connections List[:class:`Connection`]
List of connections the trip consists of.
duration :class:`int`
seconds the trip takes overall.
zones Optional[List[:class:`str`]]
List of zones this trip goes through.
fare Optional[:class:`dict`]
misc info about this trip, ticket prices, etc.
"""
def __init__(self, **kwargs):
self.connections = []
for connection in kwargs.get("legs", []):
self.connections.append(Connection(**connection))
self.duration = sum([x.duration for x in self.connections if x.duration])
try:
self.zones = kwargs.get("fare", {}).get("zones", [])[0].get("zones", [])
except IndexError:
self.zones = []
# inserted raw
self.raw = kwargs
self.fare = kwargs.get("fare")
def __str__(self):
return f"Connection ({int(self.duration / 60)} minutes):\n" \
+ '\n'.join([str(x) for x in self.connections])