Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port the lib to Python 3 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions irail/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import urllib2
from model import *
from format import *
from exception import *
import urllib.request, urllib.error, urllib.parse
from .model import *
from .format import *
from .exception import *

#curl 'api.irail.be/liveboard/?station=gentbruggee&format=json'

Expand Down Expand Up @@ -65,11 +65,11 @@ def do_request(self, method, args=None):
url += "?format=" + str(self.format())
url += "&lang=" + self.lang()
if args:
for key in args.keys():
for key in list(args.keys()):
url += "&" + key + "=" + args[key]
try:
return urllib2.urlopen(url)
except urllib2.HTTPError as e:
return urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
if e.code >= 400 and e.code < 500:
raise ClientError(e)
elif e.code >= 500 and e.code < 500:
Expand Down
6 changes: 3 additions & 3 deletions irail/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import json

from model import *
from .model import *

class JsonFormat:

Expand Down Expand Up @@ -55,7 +55,7 @@ def __convert_schedule_list(self, dict):

def __convert_schedule(self, dict):
departure = self.__convert_departure(dict['departure'])
if dict.has_key('vias'):
if 'vias' in dict:
vias = self.__convert_vias(dict['vias'])
else:
vias = None
Expand All @@ -71,7 +71,7 @@ def __convert_connection_event(self, dict, clazz):
time = dict['time']
delay = dict['delay']
vehicle = dict['vehicle']
direction = self.__convert_station(dict['direction'])
direction = Station(dict['direction']['name'], *[None]*4) # Direction is not a real station
return clazz(station, platform, time, delay, vehicle, direction)

def __convert_arrival(self, dict):
Expand Down
4 changes: 2 additions & 2 deletions irail/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def __new__(cls, d):
return [ObjectFactory(e) for e in d] #TODO add tuple, set, frozenset
elif isinstance(d, dict):
obj = object.__new__(cls)
for k, v in d.iteritems():
for k, v in d.items():
setattr(obj, k, ObjectFactory(v))
return obj
else:
return d

def __repr__(self):
return '<%s>' % str('\n '.join('%s : %s' %
(k, repr(v)) for (k, v) in self.__dict__.iteritems()))
(k, repr(v)) for (k, v) in self.__dict__.items()))

class ResultList:
def __init__(self, timestamp, version):
Expand Down
16 changes: 8 additions & 8 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,35 @@
def test_getstations():
api = iRailAPI()
r = api.get_stations()
print "Version: " + r.version()
print "Timestamp: " + r.timestamp()
print("Version: " + r.version())
print("Timestamp: " + r.timestamp())
for station in r.stations():
print station
print(station)

def test_searchstations():
api = iRailAPI()
stations = api.search_stations("bru")
print "--- Stations starting with bru ---"
print("--- Stations starting with bru ---")
for station in stations:
print station
print(station)

def test_getschedules():
api = iRailAPI()
schedules = api.get_schedules_by_names("Courtrai", "Leuven")
for schedule in schedules.connections():
print schedule
print(schedule)

def test_getliveboard():
api = iRailAPI()
schedule = api.get_liveboard_by_name("Gentbrugge")
print schedule
print(schedule)
#schedule = api.get_liveboard_by_id("BE.NMBS.008893179")
#print schedule

def test_getvehicle():
api = iRailAPI()
schedule = api.get_vehicle_by_id("BE.NMBS.L584")
print schedule
print(schedule)

if __name__=="__main__":
# test_getstations()
Expand Down