Package: sgs-pycycle
sgs-pycyclesgs-pycycle is a pure Python 3 client and object-mapper for Oslo Bysykkel.It requires no additional libraries.GeneralThe main reason for writing this library was a simple academic task.Since the namepycyclewas already taken, I decided to namespace it.I hope that somebody will find it useful.RequirementsPython >=3.6 with correspondingsetuptoolsInstallationIf one wants to install the package in a convenient way as an unprivileged
user the easiest way is to usevirtualenvthat is part of Python 3.python-mvenvvenvSGSPycyclesourcevenvSGSPycycle/bin/activateor if using MS WindowsC:\Users\'Username'\path\to\venvSGSPycycle\Scripts\activate.batThat will install and load the virtual environment
Now one can choose one of the methods described bellow.pip (pypi)The easiest way is to install the package directly from pypi.orgpipinstallsgs-pycycleManualgitclonehttps://github.com/sgs-test/sgs-pycycle.gitOne can simply install in a virtual-environment or as a privileged user by:cdsgs-pycycle
pythonsetup.pyinstallor one can add a symlink or copysgs-pycycle/sgs_pycycleinto any directory
that is included in the PYTHONPATH environmental variable or create own
directory.mkdirmypythonpathdirexportPYTHONPATH=/full/path/to/mypythonpathdir
cp-Rsgs-pycycle/sgs_pycycle/full/path/to/mypythonpathdir/This latest method also eliminates the need forsetuptools, but will not add
the dedicated executablesgs_pycycle. Still the CLI interface can be accessed
as shown bellow.Check if everything works by typing:python-msgs_pycycle-hOverview and technical designThe main goals when considering the technical design were:PortabilityAlthough there are several Python libraries that simplify the HTTP request as well
as handling the responses likerequestsandpydanticfor object mapping,
I decided to use only the standard Python modules to make installation and
usage as simple as possible.SimplicityThere are many possible improvements (some of them listed bellow), but I made a
conscious decision to only stick with the provided task.Object-oriented design and extensibilityMaking HTTP requests and processing data is a relatively simple task.
I wanted to provide a small set of objects (Station and StationCollection) in
order to make the use of this client library easier to extend.
Instead of using the provided CLI interface, it is now quite easy to use the
client library in f.i. web application or GUI application.
In addition using a simple object mapper is more elegant and intuitive than
simply manipulating JSON structures.ExamplesCLIThe CLI interface can be accessed in two ways.python-msgs_pycycle-hor if sgs-pycycle was installed usingsetuptools, there will also be a
binary calledsgs_pycycle. Hencesgs_pycycle-hwill yield the same result and display a summery of all available CLI options.python-msgs_pycycle-l
sgs_pycycle-lwill display all stations with id, name, number of docks available, number
of bikes available.python-msgs_pycycle-l-kname
sgs_pycycle-l-knamewill produce the same content, only order the stations by name.All station attributes can be used for sorting, given that string attributes
will be sorted as strings (f.i. '98' > '1654').python-msgs_pycycle-l-cuser-testapp
sgs_pycycle-l-cuser-testappwill call the API using 'user-testapp' as Client-Identifier.Using the library directlyUsing the library directly provides more possibilities and makes it possible
to embed the client into a web-application or even a GUI application.importsgs_pycycle# Auto discovery URL and identityA_D_URL='https://gbfs.urbansharing.com/oslobysykkel.no/gbfs.json'CLIENT_IDENTITY='user-testapp'# create a Client objectclient=sgs_pycycle.Client(A_D_URL,CLIENT_IDENTITY)# fetch the entire collectiontry:collection=client.get_station_collection()exceptsgs_pycycle.ClientConnectionErrorase:# ... unable to connect routines and messagespasslen(collection)# amount of stations# print the last_reported value for each stationforstationincollection:print(f'{station.name}-{station.last_reported}')# sort the stations by namecollection.sort_by_key('name')forstationincollection:# prints id, name, number of docks available, number of bikes availableprint(station)TodoIncomplete list of possible future improvements:unit testscaching by using thelast_updatedandlast_reportedattributes for better performanceextended StationCollection API, fetching a single Station, advanced sorting..translation / locale supportAPI documentation with SphinxBetter error reporting and sanity checkingFormatting output for CLI (header, even spacing, etc...)More CLI parameters (socket timeout, SSL context, etc...)AuthorSimeon Simeonov - sgs @ FreenodeLicenseCopyright (c) 2020, Simeon Simeonov
All rights reserved.Licensedunder the BSD 2-clause.
SPDX-License-Identifier: BSD-2-Clause-FreeBSD