sptrans Package

v0 Module

Module for the v0 version of the SPTrans API.

The first thing you have to do, in order to use it, is to instantiate a client and authenticate to the API:

from sptrans.v0 import Client


client = Client()
client.authenticate('this is my token')

Then you can use the other methods to grab data from the API.

exception sptrans.v0.AuthenticationError[source]

Bases: exceptions.Exception

Raised when the authentication fails - for example, with a wrong token -.

class sptrans.v0.Client[source]

Bases: object

Main client class.

Warning

Any method (except authenticate()) may raise RequestError if the client is not authenticated anymore. So keep an eye on the authentication state.

Example:

from sptrans.v0 import Client


client = Client()
client.authenticate('this is my token')
authenticate(token)[source]

Authenticates to the webservice.

Parameters:token (str) – The API token string.
Raises :AuthenticationError when there’s an error during authentication.
get_forecast(stop_code=None, route_code=None)[source]

Gets the arrival forecast, provided a route code or a stop code or both.

You must provide at least one of the parameters. If you provide only a stop_code, it will return a forecast for all routes that attend a certain stop. If you provide only a route_code, it will return a forecast for all stops that a certain route attends to. If you provide both, it will return a forecast for the specific route attending to the specific stop provided.

Parameters:
  • stop_code (int) – The stop code to use for matching.
  • route_code (int) – The stop code to use for matching.
Returns:

A single ForecastWithStop object, when passing only stop_code or both.

Returns:

A single ForecastWithStops object, when passing only route_code.

Example:

from sptrans.v0 import Client


client = Client()
client.authenticate('this is my token')

forecast = client.get_forecast(stop_code=1234)
for route in forecast.stop.routes:
    for vehicle in route.vehicles:
        print(vehicle.prefix)

forecast = client.get_forecast(stop_code=1234, route_code=2345)
for route in forecast.stop.routes:
    for vehicle in route.vehicles:
        print(vehicle.prefix)

forecast = client.get_forecast(route_code=2345)
for stop in forecast.stops:
    for vehicle in stop.vehicles:
        print(vehicle.prefix)
get_positions(code)[source]

Gets the vehicles with their current positions, provided a route code.

Parameters:code (int) – The route code to use for matching.
Returns:A single Positions object.

Example:

from sptrans.v0 import Client


client = Client()
client.authenticate('this is my token')

positions = client.get_positions(1234)
print(positions.time)

for vehicle in positions.vehicles:
    print(vehicle.prefix)
list_lanes()[source]

Lists all the bus lanes in the city.

Returns:A generator that yields Lane objects.

Example:

from sptrans.v0 import Client


client = Client()
client.authenticate('this is my token')
for lane in client.list_lanes():
    print(lane.code, lane.name)
search_routes(keywords)[source]

Searches for routes that match the provided keywords.

Parameters:keywords (str) – The keywords, in a single string, to use for matching.
Returns:A generator that yields Route objects.

Example:

from sptrans.v0 import Client


client = Client()
client.authenticate('this is my token')
for route in client.search_routes('butanta'):
    print(route.code, route.sign)
search_stops(keywords)[source]

Searches for bus stops that match the provided keywords.

Parameters:keywords (str) – The keywords, in a single string, to use for matching.
Returns:A generator that yields Stop objects.

Example:

from sptrans.v0 import Client


client = Client()
client.authenticate('this is my token')
for stop in client.search_stops('butanta'):
    print(stop.code, stop.name)
search_stops_by_lane(code)[source]

Searches for bus stops that are contained in a lane specified by its code.

Parameters:code (int) – The lane code to use for matching.
Returns:A generator that yields Stop objects.

Example:

from sptrans.v0 import Client


client = Client()
client.authenticate('this is my token')
for stop in client.search_stops_by_lane(1234):
    print(stop.code, stop.name)
search_stops_by_route(code)[source]

Searches for bus stops that are passed by the route specified by its code.

Parameters:code (int) – The route code to use for matching.
Returns:A generator that yields Stop objects.

Example:

from sptrans.v0 import Client


client = Client()
client.authenticate('this is my token')
for stop in client.search_stops_by_route(1234):
    print(stop.code, stop.name)
class sptrans.v0.ForecastWithStop

Bases: sptrans.v0.ForecastWithStop, sptrans.v0.TupleMapMixin

A namedtuple representing a bus stop forecast with routes and the time when the information was retrieved.

Variables:
  • time – (datetime.datetime) The time when the information was retrieved.
  • stop – (StopWithRoutes) The bus stop with routes.
class sptrans.v0.ForecastWithStops

Bases: sptrans.v0.ForecastWithStops, sptrans.v0.TupleMapMixin

A namedtuple representing a list of bus stops forecast with vehicles and the time when the information was retrieved.

Variables:
  • time – (datetime.datetime) The time when the information was retrieved.
  • stops – (list of StopWithVehicles) The bus stops.
class sptrans.v0.Lane

Bases: sptrans.v0.Lane, sptrans.v0.TupleMapMixin

A namedtuple representing a bus lane.

Variables:
  • code – (int) The lane code.
  • cot – (int) The lane “cot” (?).
  • name – (str) The lane name.
class sptrans.v0.Positions

Bases: sptrans.v0.Positions, sptrans.v0.TupleMapMixin

A namedtuple representing a sequence of vehicles positions, with the time when the information was retrieved.

Variables:
  • time – (datetime.datetime) The time when the information was retrieved.
  • vehicles – (list) The list of vehicles.
exception sptrans.v0.RequestError[source]

Bases: exceptions.Exception

Raised when the request failes to be accomplished.

Normally this is due to the client not being authenticated anymore. In this case, just authenticate again, and it should be back at work.

class sptrans.v0.Route

Bases: sptrans.v0.Route, sptrans.v0.TupleMapMixin

A namedtuple representing a route.

Variables:
  • code – (int) The route code.
  • circular – (bool) Wether it’s a circular route or not (without a secondary terminal).
  • sign – (str) The first part of the route sign.
  • direction – (int) The route direction. “1” means “main to secondary terminal”, “2” means “secondary to main terminal”.
  • type – (int) The route type.
  • main_to_sec – (str) The name of the route when moving from the main terminal to the second one.
  • sec_to_main – (str) The name of the route when moving from the second terminal to the main one.
  • info – (str) Extra information about the route.
class sptrans.v0.RouteWithVehicles

Bases: sptrans.v0.RouteWithVehicles, sptrans.v0.TupleMapMixin

A namedtuple representing a route with a sequence of vehicles with their current positions.

Variables:
  • sign – (str) The first part of the route sign.
  • code – (int) The route code.
  • direction – (int) The route direction. “1” means “main to secondary terminal”, “2” means “secondary to main terminal”.
  • main_to_sec – (str) The name of the route when moving from the main terminal to the second one.
  • sec_to_main – (str) The name of the route when moving from the second terminal to the main one.
  • quantity – (int) The quantity of vehicles.
  • vehicles – (list) The list of vehicles.
class sptrans.v0.Stop

Bases: sptrans.v0.Stop, sptrans.v0.TupleMapMixin

A namedtuple representing a bus stop.

Variables:
  • code – (int) The stop code.
  • name – (str) The stop name.
  • address – (str) The stop address.
  • latitude – (float) The stop latitude.
  • longitude – (float) The stop longitude.
class sptrans.v0.StopWithRoutes

Bases: sptrans.v0.StopWithRoutes, sptrans.v0.TupleMapMixin

A namedtuple representing a bus stop with a list of routes that pass through this stop.

Variables:
  • code – (int) The stop code.
  • name – (str) The stop name.
  • latitude – (float) The stop latitude.
  • longitude – (float) The stop longitude.
  • routes – (list) The list of routes that pass through this stop.
class sptrans.v0.StopWithVehicles

Bases: sptrans.v0.StopWithVehicles, sptrans.v0.TupleMapMixin

A namedtuple representing a bus stop with a list of vehicles that pass through this stop.

Variables:
  • code – (int) The stop code.
  • name – (str) The stop name.
  • latitude – (float) The stop latitude.
  • longitude – (float) The stop longitude.
  • vehicles – (list) The list of vehicles.
class sptrans.v0.Vehicle

Bases: sptrans.v0.Vehicle, sptrans.v0.TupleMapMixin

A namedtuple representing a vehicle (bus) with its position.

Variables:
  • prefix – (str) The vehicle prefix painted in the bus.
  • accessible – (bool) Wether the vehicle is accessible or not.
  • latitude – (float) The vehicle latitude.
  • longitude – (float) The vehicle longitude.
class sptrans.v0.VehicleForecast

Bases: sptrans.v0.VehicleForecast, sptrans.v0.TupleMapMixin

A namedtuple representing a vehicle (bus) with its position and forecast to arrive at a certain stop.

Variables:
  • prefix – (str) The vehicle prefix painted in the bus.
  • accessible – (bool) Wether the vehicle is accessible or not.
  • arriving_at – (datetime.datetime) The time that the vehicle is expected to arrive.
  • latitude – (float) The vehicle latitude.
  • longitude – (float) The vehicle longitude.

Table Of Contents

Previous topic

Changelog

Next topic

Contributing

This Page