Split classes into three files

This commit is contained in:
Klaus-Uwe Mitterer 2017-09-25 22:41:51 +02:00
parent 766c930d9c
commit f9526a70a0
4 changed files with 329 additions and 324 deletions

View file

@ -1,324 +1,3 @@
import datetime
class Station:
def __init__(self, name, sttype, extid = None, xcoord = None, ycoord = None, prodclass = None):
self.name = name
self.sttype = sttype
self.extid = extid
self.xcoord = float(xcoord)/1000000
self.ycoord = float(ycoord)/1000000
self.prodclass = prodclass
def useId(self):
return self.extid or self.name
def lat(self):
return self.ycoord
def lon(self):
return self.xcoord
def json(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False, distance = False):
out = " " * indent + "{\n"
out += (" " * indent + " \"name\": \"%s\",\n" % self.name) if name else ""
out += (" " * indent + " \"id\": \"%s\",\n" % self.useId()) if extid else ""
out += (" " * indent + " \"distance\": %i,\n" % int(self.distance)) if distance else ""
out += (" " * indent + " \"type\": \"%s\",\n" % self.sttype) if sttype else ""
if coords:
out += " " * indent + " \"coords\": {\n"
out += " " * indent + " \"lon\": %f,\n" % self.xcoord
out += " " * indent + " \"lat\": %f\n" % self.ycoord
out += " " * indent + " },\n"
out += (" " * indent + " \"prodclass\": \"%s\",\n" % self.prodclass) if prodclass else ""
out = "".join(out.rsplit(",", 1))
out += " " * indent + "}"
return out
def xml(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False, distance = False):
out = " " * indent + "<station>\n"
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
out += (" " * indent + " <id>%s</id>\n" % self.useId()) if extid else ""
out += (" " * indent + " <distance>%i</distance>\n" % int(self.distance)) if distance else ""
out += (" " * indent + " <type>%s</type>\n" % self.sttype) if sttype else ""
if coords:
out += " " * indent + " <coords>\n"
out += " " * indent + " <lon>%f</lon>\n" % self.xcoord
out += " " * indent + " <lat>%f</lat>\n" % self.ycoord
out += " " * indent + " </coords>\n"
out += (" " * indent + " <prodclass>%s</prodclass>\n" % self.prodclass) if prodclass else ""
out += " " * indent + "</station>"
return out
class Service:
def __init__(self, name, depst, deptime, arrst, arrtime, dest = None, deppf = None, currdep = None, arrpf = None, curarr = None):
self.name = name
self.dest = dest
self.depst = depst
self.deptime = deptime
self.arrst = arrst
self.arrtime = arrtime
self.deppf = deppf
self.currdep = currdep
self.arrpf = arrpf
self.curarr = curarr
def duration(self):
return self.arrtime - self.deptime
def xml(self, indent = 0, iid = False, name = True, depst = True, deptime = True, arrst = True, arrtime = True, deppf = True, currdep = True, arrpf = True, curarr = True, duration = True, dest = True, stationkwargs = {}):
out = " " * indent + "<service%s>\n" % ("" if iid is False else " id=\"%i\"" % iid)
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
if dest and self.dest:
out += " " * indent + " <destination>\n"
out += self.dest.xml(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " </destination>\n"
if depst or deptime or deppf or currdep:
out += " " * indent + " <departure>\n"
out += (self.depst.xml(indent + 2, **stationkwargs) + "\n") if depst else ""
if deptime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.deptime, "%H:%M")
out += (" " * indent + " <current>%s</current>\n" % self.currdep) if currdep and self.currdep else ""
out += (" " * indent + " <platform>%s</platform>\n" % self.deppf) if deppf and self.deppf else ""
out += " " * indent + " </departure>\n"
if arrst or arrtime or arrpf or curarr:
out += " " * indent + " <arrival>\n"
out += (self.arrst.xml(indent + 2, **stationkwargs) + "\n") if arrst else ""
if arrtime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.arrtime, "%H:%M")
out += (" " * indent + " <current>%s</current>\n" % self.curarr) if curarr and self.curarr else ""
out += (" " * indent + " <platform>%s</platform>\n" % self.arrpf) if arrpf and self.arrpf else ""
out += " " * indent + " </arrival>\n"
out += " " * indent + "</service>"
return out
def json(self, indent = 0, iid = False, name = True, depst = True, deptime = True, arrst = True, arrtime = True, deppf = True, currdep = True, arrpf = True, curarr = True, duration = True, dest = True, stationkwargs = {}):
out = " " * indent + "{\n"
out += (" " * indent + " \"@id\": %i,\n" % iid) if iid is not False else ""
out += (" " * indent + " \"name\": \"%s\",\n" % self.name) if name else ""
if dest and self.dest:
out += " " * indent + " \"destination\":\n"
out += self.dest.json(indent + 2, **stationkwargs) + ",\n"
if depst or deptime or deppf or currdep:
dep = " " * indent + " \"departure\": {\n"
if depst:
dep += " " * indent + " \"station\":\n"
dep += self.depst.json(indent + 3, **stationkwargs) + ",\n"
if deptime:
dep += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y")
dep += " " * indent + " \"time\": \"%s\",\n" % datetime.datetime.strftime(self.deptime, "%H:%M")
dep += (" " * indent + " \"current\": \"%s\",\n" % self.currdep) if currdep and self.currdep else ""
dep += (" " * indent + " \"platform\": \"%s\",\n" % self.deppf) if deppf and self.deppf else ""
dep = "".join(dep.rsplit(",", 1))
out += dep + " " * indent + " },\n"
if arrst or arrtime or arrpf or curarr:
arr = " " * indent + " \"arrival\": {\n"
if arrst:
arr += " " * indent + " \"station\":\n"
arr += self.arrst.json(indent + 3, **stationkwargs) + ",\n"
if arrtime:
arr += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y")
arr += " " * indent + " \"time\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime, "%H:%M")
arr += (" " * indent + " \"current\": \"%s\",\n" % self.curarr) if curarr and self.curarr else ""
arr += (" " * indent + " \"platform\": \"%s\",\n" % self.arrpf) if arrpf and self.arrpf else ""
arr = "".join(arr.rsplit(",", 1))
out += arr + " " * indent + " },\n"
out += " " * indent + "}"
out = "".join(out.rsplit(",", 1))
return out
class Connection:
def __init__(self, details = False):
self.services = []
self.details = details
def addService(self, service):
self.services.append(service)
def depst(self):
try:
return self.services[0].depst
except:
return None
def arrst(self):
try:
return self.services[-1].arrst
except:
return None
def deptime(self):
try:
return self.services[0].deptime
except:
return None
def arrtime(self):
try:
return self.services[-1].arrtime
except:
return None
def currdep(self):
try:
return self.services[0].currdep
except:
return None
def currarr(self):
try:
return self.services[-1].currarr
except:
return None
def duration(self):
try:
return self.services[-1].arrtime - self.services[0].deptime
except:
return None
def durationString(self):
hrs, scs = divmod(self.duration().total_seconds(), 3600)
mns, rmd = divmod(scs, 60)
return "%i:%s" % (hrs, str(int(mns)).zfill(2))
def changes(self):
return max(len([service for service in self.services if service.name != "Walk"]) - 1, 0) if self.details else max(len([service for service in self.services[0].name.split("/") if service != "Walk"]) - 1, 0)
def xml(self, indent = 0, cid = False, frm = True, to = True, deptime = True, arrtime = True, duration = True, changes = True, services = True, servicekwargs = {}, stationkwargs = {}):
out = " " * indent + "<connection%s>\n" % ("" if cid is False else " id=\"%i\"" % cid)
if frm:
out += " " * indent + " <from>\n"
out += self.depst().xml(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " </from>\n"
if to:
out += " " * indent + " <to>\n"
out += self.arrst().xml(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " </to>\n"
if deptime or arrtime or duration or changes:
out += " " * indent + " <details>\n"
if deptime:
out += " " * indent + " <departure>\n"
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.deptime(), "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.deptime(), "%H:%M")
out += " " * indent + " </departure>\n"
if arrtime:
out += " " * indent + " <arrival>\n"
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.arrtime(), "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.arrtime(), "%H:%M")
out += " " * indent + " </arrival>\n"
out += (" " * indent + " <duration>%s</duration>\n" % self.durationString()) if duration else ""
out += (" " * indent + " <changes>%i</changes>\n" % self.changes()) if changes else ""
out += " " * indent + " </details>\n"
if services:
out += " " * indent + " <services>\n"
for i in range(len(self.services)):
out += self.services[i].xml(indent + 2, i, **servicekwargs) + "\n"
out += " " * indent + " </services>\n"
out += " " * indent + "</connection>"
return out
def json(self, indent = 0, cid = False, frm = True, to = True, deptime = True, arrtime = True, duration = True, changes = True, services = True, servicekwargs = {}, stationkwargs = {}):
out = " " * indent + "{\n"
out += (" " * indent + " \"@id\": %i,\n" % cid) if cid is not False else ""
if frm:
out += " " * indent + " \"from\":\n"
out += self.depst().json(indent + 2, **stationkwargs) + ",\n"
if to:
out += " " * indent + " \"to\":\n"
out += self.arrst().json(indent + 2, **stationkwargs) + ",\n"
if deptime or arrtime or duration or changes:
det = ""
det += " " * indent + " \"details\": {\n"
if deptime:
det += " " * indent + " \"departure\": {\n"
det += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.deptime(), "%d.%m.%Y")
det += " " * indent + " \"time\": \"%s\"\n" % datetime.datetime.strftime(self.deptime(), "%H:%M")
det += " " * indent + " },\n"
if arrtime:
det += " " * indent + " \"arrival\": {\n"
det += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime(), "%d.%m.%Y")
det += " " * indent + " \"time\": \"%s\"\n"% datetime.datetime.strftime(self.arrtime(), "%H:%M")
det += " " * indent + " },\n"
det += (" " * indent + " \"duration\": \"%s\",\n" % self.durationString()) if duration else ""
det += (" " * indent + " \"changes\": %i,\n" % self.changes()) if changes else ""
det = "".join(det.rsplit(",", 1))
out += det
out += " " * indent + " },\n"
if services:
out += " " * indent + " \"services\": [\n"
for i in range(len(self.services)):
out += self.services[i].json(indent + 2, i, **servicekwargs) + (",\n" if not i == len(self.services) - 1 else "\n")
out += " " * indent + " ],\n"
out += " " * indent + "}"
out = "".join(out.rsplit(",", 1))
return out
from classes.connection import Connection
from classes.station import Station
from classes.service import Service

156
classes/connection.py Normal file
View file

@ -0,0 +1,156 @@
import datetime
class Connection:
def __init__(self, details = False):
self.services = []
self.details = details
def addService(self, service):
self.services.append(service)
def depst(self):
try:
return self.services[0].depst
except:
return None
def arrst(self):
try:
return self.services[-1].arrst
except:
return None
def deptime(self):
try:
return self.services[0].deptime
except:
return None
def arrtime(self):
try:
return self.services[-1].arrtime
except:
return None
def currdep(self):
try:
return self.services[0].currdep
except:
return None
def currarr(self):
try:
return self.services[-1].currarr
except:
return None
def duration(self):
try:
return self.services[-1].arrtime - self.services[0].deptime
except:
return None
def durationString(self):
hrs, scs = divmod(self.duration().total_seconds(), 3600)
mns, rmd = divmod(scs, 60)
return "%i:%s" % (hrs, str(int(mns)).zfill(2))
def changes(self):
return max(len([service for service in self.services if service.name != "Walk"]) - 1, 0) if self.details else max(len([service for service in self.services[0].name.split("/") if service != "Walk"]) - 1, 0)
def xml(self, indent = 0, cid = False, frm = True, to = True, deptime = True, arrtime = True, duration = True, changes = True, services = True, servicekwargs = {}, stationkwargs = {}):
out = " " * indent + "<connection%s>\n" % ("" if cid is False else " id=\"%i\"" % cid)
if frm:
out += " " * indent + " <from>\n"
out += self.depst().xml(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " </from>\n"
if to:
out += " " * indent + " <to>\n"
out += self.arrst().xml(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " </to>\n"
if deptime or arrtime or duration or changes:
out += " " * indent + " <details>\n"
if deptime:
out += " " * indent + " <departure>\n"
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.deptime(), "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.deptime(), "%H:%M")
out += " " * indent + " </departure>\n"
if arrtime:
out += " " * indent + " <arrival>\n"
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.arrtime(), "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.arrtime(), "%H:%M")
out += " " * indent + " </arrival>\n"
out += (" " * indent + " <duration>%s</duration>\n" % self.durationString()) if duration else ""
out += (" " * indent + " <changes>%i</changes>\n" % self.changes()) if changes else ""
out += " " * indent + " </details>\n"
if services:
out += " " * indent + " <services>\n"
for i in range(len(self.services)):
out += self.services[i].xml(indent + 2, i, **servicekwargs) + "\n"
out += " " * indent + " </services>\n"
out += " " * indent + "</connection>"
return out
def json(self, indent = 0, cid = False, frm = True, to = True, deptime = True, arrtime = True, duration = True, changes = True, services = True, servicekwargs = {}, stationkwargs = {}):
out = " " * indent + "{\n"
out += (" " * indent + " \"@id\": %i,\n" % cid) if cid is not False else ""
if frm:
out += " " * indent + " \"from\":\n"
out += self.depst().json(indent + 2, **stationkwargs) + ",\n"
if to:
out += " " * indent + " \"to\":\n"
out += self.arrst().json(indent + 2, **stationkwargs) + ",\n"
if deptime or arrtime or duration or changes:
det = ""
det += " " * indent + " \"details\": {\n"
if deptime:
det += " " * indent + " \"departure\": {\n"
det += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.deptime(), "%d.%m.%Y")
det += " " * indent + " \"time\": \"%s\"\n" % datetime.datetime.strftime(self.deptime(), "%H:%M")
det += " " * indent + " },\n"
if arrtime:
det += " " * indent + " \"arrival\": {\n"
det += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime(), "%d.%m.%Y")
det += " " * indent + " \"time\": \"%s\"\n"% datetime.datetime.strftime(self.arrtime(), "%H:%M")
det += " " * indent + " },\n"
det += (" " * indent + " \"duration\": \"%s\",\n" % self.durationString()) if duration else ""
det += (" " * indent + " \"changes\": %i,\n" % self.changes()) if changes else ""
det = "".join(det.rsplit(",", 1))
out += det
out += " " * indent + " },\n"
if services:
out += " " * indent + " \"services\": [\n"
for i in range(len(self.services)):
out += self.services[i].json(indent + 2, i, **servicekwargs) + (",\n" if not i == len(self.services) - 1 else "\n")
out += " " * indent + " ],\n"
out += " " * indent + "}"
out = "".join(out.rsplit(",", 1))
return out

110
classes/service.py Normal file
View file

@ -0,0 +1,110 @@
import datetime
class Service:
def __init__(self, name, depst, deptime, arrst, arrtime, dest = None, deppf = None, currdep = None, arrpf = None, curarr = None):
self.name = name
self.dest = dest
self.depst = depst
self.deptime = deptime
self.arrst = arrst
self.arrtime = arrtime
self.deppf = deppf
self.currdep = currdep
self.arrpf = arrpf
self.curarr = curarr
def duration(self):
return self.arrtime - self.deptime
def xml(self, indent = 0, iid = False, name = True, depst = True, deptime = True, arrst = True, arrtime = True, deppf = True, currdep = True, arrpf = True, curarr = True, duration = True, dest = True, stationkwargs = {}):
out = " " * indent + "<service%s>\n" % ("" if iid is False else " id=\"%i\"" % iid)
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
if dest and self.dest:
out += " " * indent + " <destination>\n"
out += self.dest.xml(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " </destination>\n"
if depst or deptime or deppf or currdep:
out += " " * indent + " <departure>\n"
out += (self.depst.xml(indent + 2, **stationkwargs) + "\n") if depst else ""
if deptime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.deptime, "%H:%M")
out += (" " * indent + " <current>%s</current>\n" % self.currdep) if currdep and self.currdep else ""
out += (" " * indent + " <platform>%s</platform>\n" % self.deppf) if deppf and self.deppf else ""
out += " " * indent + " </departure>\n"
if arrst or arrtime or arrpf or curarr:
out += " " * indent + " <arrival>\n"
out += (self.arrst.xml(indent + 2, **stationkwargs) + "\n") if arrst else ""
if arrtime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.arrtime, "%H:%M")
out += (" " * indent + " <current>%s</current>\n" % self.curarr) if curarr and self.curarr else ""
out += (" " * indent + " <platform>%s</platform>\n" % self.arrpf) if arrpf and self.arrpf else ""
out += " " * indent + " </arrival>\n"
out += " " * indent + "</service>"
return out
def json(self, indent = 0, iid = False, name = True, depst = True, deptime = True, arrst = True, arrtime = True, deppf = True, currdep = True, arrpf = True, curarr = True, duration = True, dest = True, stationkwargs = {}):
out = " " * indent + "{\n"
out += (" " * indent + " \"@id\": %i,\n" % iid) if iid is not False else ""
out += (" " * indent + " \"name\": \"%s\",\n" % self.name) if name else ""
if dest and self.dest:
out += " " * indent + " \"destination\":\n"
out += self.dest.json(indent + 2, **stationkwargs) + ",\n"
if depst or deptime or deppf or currdep:
dep = " " * indent + " \"departure\": {\n"
if depst:
dep += " " * indent + " \"station\":\n"
dep += self.depst.json(indent + 3, **stationkwargs) + ",\n"
if deptime:
dep += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y")
dep += " " * indent + " \"time\": \"%s\",\n" % datetime.datetime.strftime(self.deptime, "%H:%M")
dep += (" " * indent + " \"current\": \"%s\",\n" % self.currdep) if currdep and self.currdep else ""
dep += (" " * indent + " \"platform\": \"%s\",\n" % self.deppf) if deppf and self.deppf else ""
dep = "".join(dep.rsplit(",", 1))
out += dep + " " * indent + " },\n"
if arrst or arrtime or arrpf or curarr:
arr = " " * indent + " \"arrival\": {\n"
if arrst:
arr += " " * indent + " \"station\":\n"
arr += self.arrst.json(indent + 3, **stationkwargs) + ",\n"
if arrtime:
arr += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y")
arr += " " * indent + " \"time\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime, "%H:%M")
arr += (" " * indent + " \"current\": \"%s\",\n" % self.curarr) if curarr and self.curarr else ""
arr += (" " * indent + " \"platform\": \"%s\",\n" % self.arrpf) if arrpf and self.arrpf else ""
arr = "".join(arr.rsplit(",", 1))
out += arr + " " * indent + " },\n"
out += " " * indent + "}"
out = "".join(out.rsplit(",", 1))
return out

60
classes/station.py Normal file
View file

@ -0,0 +1,60 @@
class Station:
def __init__(self, name, sttype, extid = None, xcoord = None, ycoord = None, prodclass = None):
self.name = name
self.sttype = sttype
self.extid = extid
self.xcoord = float(xcoord)/1000000
self.ycoord = float(ycoord)/1000000
self.prodclass = prodclass
def useId(self):
return self.extid or self.name
def lat(self):
return self.ycoord
def lon(self):
return self.xcoord
def json(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False, distance = False):
out = " " * indent + "{\n"
out += (" " * indent + " \"name\": \"%s\",\n" % self.name) if name else ""
out += (" " * indent + " \"id\": \"%s\",\n" % self.useId()) if extid else ""
out += (" " * indent + " \"distance\": %i,\n" % int(self.distance)) if distance else ""
out += (" " * indent + " \"type\": \"%s\",\n" % self.sttype) if sttype else ""
if coords:
out += " " * indent + " \"coords\": {\n"
out += " " * indent + " \"lon\": %f,\n" % self.xcoord
out += " " * indent + " \"lat\": %f\n" % self.ycoord
out += " " * indent + " },\n"
out += (" " * indent + " \"prodclass\": \"%s\",\n" % self.prodclass) if prodclass else ""
out = "".join(out.rsplit(",", 1))
out += " " * indent + "}"
return out
def xml(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False, distance = False):
out = " " * indent + "<station>\n"
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
out += (" " * indent + " <id>%s</id>\n" % self.useId()) if extid else ""
out += (" " * indent + " <distance>%i</distance>\n" % int(self.distance)) if distance else ""
out += (" " * indent + " <type>%s</type>\n" % self.sttype) if sttype else ""
if coords:
out += " " * indent + " <coords>\n"
out += " " * indent + " <lon>%f</lon>\n" % self.xcoord
out += " " * indent + " <lat>%f</lat>\n" % self.ycoord
out += " " * indent + " </coords>\n"
out += (" " * indent + " <prodclass>%s</prodclass>\n" % self.prodclass) if prodclass else ""
out += " " * indent + "</station>"
return out