Skip to content
This repository has been archived by the owner on Jul 25, 2020. It is now read-only.

Setters

david edited this page Apr 28, 2019 · 1 revision

The following guidelines define how the Getter functions must be coded to be compatible with PyBuses.

Stop Setters

  • Stop Setters save an existing Stop to a local or controlled storage, such as a database (that can be used as an Offline Stop Getter).
  • Setters receive as first parameter a Stop object "stop" of PyBuses, with all the available information of the stop.
  • Setters receive as second parameter a bool "update" which will update a currently saved Stop with the new data provided.
  • Stop Setters return nothing when an operation was successful.
  • If some error happened when saving the stop using a Setter, or the resource is not available, the setter must throw the StopSetterUnavailable() exception.
  • The objectives of Stop Setters (and therefore Offline Stop Getters) are:
    • Avoid sending requests to an external API if not neccesary.
    • Allow users to search stops by name or location, when the remote API does not provide this feature.

Example: Save on a file

On this example, we save one file per Stop, with all the info on it, using #### as separator.

from pybuses import Stop, StopSetterUnavailable

def save_stop_to_file(stop: Stop):
    try:
        with open(f"{stop.stopid}.csv", "w") as file:
            if stop.has_location():
                file.write(f"{stop.stopid}####{stop.name}####{stop.lat}####{stop.lon}")
            else:
                file.write(f"{stop.stopid}####{stop.name}")

    except IOError as ex:
        raise StopSetterUnavailable(ex)

Bus Setters

Not implemented yet.

Clone this wiki locally