Document each functions and add doctests. - lefrecce - Retrieve information about next trains and stations via lefrecce.it
(HTM) hg clone https://bitbucket.org/iamleot/lefrecce
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) changeset 90bf83ead1c0948ec8bf855a7620dcd77985d946
(DIR) parent 4e3b85bec737283570628a0344059dd1a2f693bd
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Sun, 13 May 2018 16:26:01
Document each functions and add doctests.
Diffstat:
lefrecce.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 67 insertions(+), 0 deletions(-)
---
diff -r 4e3b85bec737 -r 90bf83ead1c0 lefrecce.py
--- a/lefrecce.py Sun May 13 15:42:03 2018 +0200
+++ b/lefrecce.py Sun May 13 16:26:01 2018 +0200
@@ -43,6 +43,8 @@
def print_solution(solution):
+ """Pretty print each solution returned by solutions()."""
+
print('{departure} ({departure_datetime:%H:%M %d/%m/%Y}) - '
'{arrival} ({arrival_datetime:%H:%M %d/%m/%Y}) '
'({duration})'.format(**solution))
@@ -59,6 +61,59 @@
def solutions(origin, destination, adate, atime, verbose=True):
+ """Given origin, destination, departure date and departure time return
+ next 5 solutions via a generator.
+
+ Needs origin station (origin), destination station (destination),
+ departure date (in `%d/%m/%Y' format) (adate),
+ departure time (in `%H' format) (atime) and an optional boolean verbose
+ flag.
+
+ Each solution is a dict with the following keys:
+ - departure: departure station
+ - departure_datetime: datetime.datetime() of the departure time
+ - arrival: arrival station
+ - arrival_datetime: datetime.datetime() of the arrival time
+ - trains: a list of dicts with the details of the train(s):
+ + acronym: acronym (REG, RV, ...)
+ + id: identifier of the train
+ + departure_station: departure station
+ + departure_datetime: datetime.datetime() of the departure time
+ + arrival_datetime: datetime.datetime() of the arrival time
+ + arrival_station: departure station
+
+ >>> sols = list(solutions(
+ ... 'milano centrale',
+ ... 'roma termini',
+ ... datetime.today().strftime('%d/%m/%Y'),
+ ... datetime.today().strftime('%H'),
+ ... verbose=True,
+ ... ))
+ >>> 0 < len(sols) <= 5
+ True
+ >>> sols[0]['departure']
+ 'Milano Centrale'
+ >>> sols[0]['arrival']
+ 'Roma Termini'
+ >>> sols[0]['trains'][0]['acronym'] != ''
+ True
+ >>> sols[0]['trains'][0]['id'] != ''
+ True
+ >>> sols[0]['trains'][0]['departure_station'] != ''
+ True
+ >>> sols[0]['trains'][0]['arrival_station'] != ''
+ True
+
+ >>> sols = list(solutions(
+ ... 'roma termini',
+ ... 'milano centrale',
+ ... datetime.today().strftime('%d/%m/%Y'),
+ ... datetime.today().strftime('%H'),
+ ... verbose=False,
+ ... ))
+ >>> sols[0]['trains'] == []
+ True
+ """
url = 'https://www.lefrecce.it/msite/api/solutions?' \
+ 'origin=' + origin + '&' \
+ 'destination=' + destination + '&' \
@@ -115,6 +170,13 @@
def search_stations(name):
+ """Given a station name return a list of stations matching the name.
+
+ Please note that the API is limited to a maximum of 5 matching stations.
+
+ >>> search_stations('macerata')
+ ['MACERATA', 'MACERATA FONTESCODELLA']
+ """
url = 'https://www.lefrecce.it/msite/api/geolocations/locations?name=' + \
name
@@ -131,6 +193,11 @@
def list_stations():
+ """List all known stations.
+
+ >>> len(list_stations()) > 0
+ True
+ """
url = 'http://www.trenitalia.com/cms-file/common/js/themes/trenitalia_2014/001/list_json.js'
sess = requests.Session()