Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
175 lines (131 loc) · 4.78 KB
/
Copy pathmodel.py
File metadata and controls
175 lines (131 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-
# Copyright (c) 2011, Wouter Horré
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# all the sequence types
_seqs = tuple, list, set, frozenset
# The iRail api returns all ints and timestamps as strings
# so the python api makes the same mistake not my fault!
class ObjectFactory(object):
"""Creates an object from a dict so you can use foo.bar instead of foo['bar']."""
def __new__(cls, d):
if isinstance(d, _seqs):
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.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__.items()))
class ResultList:
def __init__(self, timestamp, version):
self.__timestamp = timestamp
self.__version = version
def timestamp(self):
return self.__timestamp
def version(self):
return self.__version
class StationList(ResultList):
def __init__(self, timestamp, version, stations):
self.__stations = stations
ResultList.__init__(self, timestamp, version)
def stations(self):
return self.__stations
class Station:
def __init__(self, name, standardname, id, locationX, locationY):
self.__name = name
self.__standardname = standardname
self.__id = id
self.__locationX = locationX
self.__locationY = locationY
def name(self):
return self.__name
def standardname(self):
return self.__standardname
def id(self):
return self.__id
def locationX(self):
return self.__locationX
def locationY(self):
return self.__locationY
def __str__(self):
return "Station " + self.id() + " | " + self.name() + " @ (" + self.locationY() + "," + self.locationX() + ")"
class ConnectionList(ResultList):
def __init__(self, timestamp, version, connections):
self.__connections = connections
ResultList.__init__(self, timestamp, version)
def connections(self):
return self.__connections
class Connection:
def __init__(self, id, departure, vias, arrival, duration):
self.__id = id
self.__departure = departure
self.__vias = vias
self.__arrival = arrival
self.__duration = duration
def id(self):
return self.__id
def departure(self):
return self.__departure
def vias(self):
return self.__vias
def arrival(self):
return self.__arrival
def duration(self):
return self.__duration
def __str__(self):
s = """Connection ({0}):
departure: {1}
arrival: {2}
vias: {3}
duration: {4}"""
return s.format(self.id(), self.departure(), self.arrival(), self.vias(), self.duration())
class ConnectionEvent:
def __init__(self, station, platform, time, delay, vehicle, direction):
self.__station = station
self.__platform = platform
self.__time = time
self.__delay = delay
self.__vehicle = vehicle
self.__direction = direction
def station(self):
return self.__station
def platform(self):
return self.__platform
def time(self):
return self.__time
def delay(self):
return self.__delay
def vehicle(self):
return self.__vehicle
def direction(self):
return self.__direction
def __str__(self):
s = """@{0} (+{1}) -> {2}"""
return s.format(self.time(), self.delay(), self.station())
class Arrival(ConnectionEvent):
def __init__(self, station, platform, time, delay, vehicle, direction):
ConnectionEvent.__init__(self, station, platform, time, delay, vehicle, direction)
class Departure(ConnectionEvent):
def __init__(self, station, platform, time, delay, vehicle, direction):
ConnectionEvent.__init__(self, station, platform, time, delay, vehicle, direction)