Skip to content
eskerda edited this page Sep 14, 2016 · 5 revisions

PyBikes package is an instance factory of different bike sharing systems. All systems subclass the base classes BikeShareSystem and BikeShareStation. A BikeShareSystem contains BikeShareStations.

As a result of the incoherence on available data in each bike sharing scheme all classes have mandatory fields and flexible fields.

BikeShareSystem

This class represents a bike sharing system (not to mistake with a bike sharing scheme). Different companies have different systems, but usually these companies install more than one scheme, and hopefully use the same syntax between schemes.

For example, PBSC has a system called Bixi, with schemes like Capital BikeShare (Washington, DC) or Bixi (Montreal). In this case, BixiSystem subclasses BikeShareSystem, and Capital BikeShare would be an instance of a BixiSystem. It's important to note how we handle the metadata here:

Bixi Information
================
'system': 'Bixi',
'company': 'PBSC'

Capital BikeShare information
=============================
'name': 'Capital BikeShare',
'city': 'Washington, DC - Arlington, VA',
'country': 'USA',
'latitude': 38.8951118,
'longitude': -77.0363658

As a result of merging these two ideas, we get:

Capital BikeShare (instance of BixiSystem)
==========================================
'name': 'Capital BikeShare',
'city': 'Washington, DC - Arlington, VA',
'country': 'USA',
'latitude': 38.8951118,
'longitude': -77.0363658,
'system': 'Bixi',
'company': 'PBSC'

The more information available, the better. There's no point in developing an incomplete resource.

The base class BikeShareSystem takes 2 parameters on instantiation: str tag, dict meta, and any subclass of it can reimplement the init method to take any needed parameters.

Using the same Bixi example, what differentiates its different schemes is a feed url:

class BixiSystem(BikeShareSystem):

    feed_url = "{root_url}bikeStations.xml"
    sync = True

    meta = { 
        'system': 'Bixi',
        'company': 'PBSC' 
    }

    def __init__(self, tag, root_url, meta):
        super( BixiSystem, self).__init__(tag, meta)
        self.feed_url = BixiSystem.feed_url.format(root_url = root_url)

With this definition, we could get an instance in two different ways:

Manually

>>> from pybikes import BixiSystem
>>> capital_bikeshare = BixiSystem( tag = 'cabi', 
                                    root_url = 'http://capitalbikeshare.com/data/stations/', 
                                    meta = { "name": "Capital BikeShare",
                                             "city": "Washington, DC - Arlington, VA",
                                             "country": "USA",
                                             "latitude": 38.8951118,
                                             "longitude": -77.0363658 
                                    }
                                  )

Using the instance factory

# data/bixi.json
{
    "system": "bixi",
    "class": "BixiSystem",
    "instances": [
        {
            "tag": "montreal",
            "root_url": "https://montreal.bixi.com/data/",
            "meta": {
                "name": "Bixi",
                "city": "Montreal",
                "country": "CAN",
                "latitude": 45.5086699,
                "longitude": -73.5539925
            }
        }
        ,{
            "tag": "cabi",
            "root_url": "http://capitalbikeshare.com/data/stations/",
            "meta": {
                "name": "Capital BikeShare",
                "city": "Washington, DC - Arlington, VA",
                "country": "USA",
                "latitude": 38.8951118,
                "longitude": -77.0363658
            }
        }
    ]
}
>>> import pybikes
>>> capital_bikeshare = pybikes.get('cabi')
Clone this wiki locally