import workers.val class Train: def __init__(self, name, tid, dest, xcoord, ycoord, prodclass): self.name = name self.tid = tid self.dest = dest self.xcoord = float(xcoord)/1000000 self.ycoord = float(ycoord)/1000000 self.prodclass = prodclass def lat(self): return self.ycoord def lon(self): return self.xcoord def destStation(self): return list(workers.val.validateName(self.name))[0] def json(self, indent = 0, name = True, tid = True, dest = True, coords = True, prodclass = False, stationkwargs = {}): out = " " * indent + "{\n" out += (" " * indent + " \"name\": \"%s\",\n" % self.name) if name else "" out += (" " * indent + " \"id\": \"%s\",\n" % self.tid) if tid else "" if dest: # out += " " * indent + " \"destination\":\n" # out += self.dest.json(indent + 2, **stationkwargs) + "\n" out += " " * indent + " \"destination\": \"%s\",\n" % self.dest 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, tid = True, dest = True, coords = True, prodclass = False, stationkwargs = {}): out = " " * indent + "\n" out += (" " * indent + " %s\n" % self.name) if name else "" out += (" " * indent + " %s\n" % self.tid) if tid else "" if dest: # out += " " * indent + " \n" # out += self.dest.xml(indent + 2, **stationkwargs) + "\n" # out += " " * indent + " \n" out += " " * indent + " %s\n" % self.dest if coords: out += " " * indent + " \n" out += " " * indent + " %f\n" % self.xcoord out += " " * indent + " %f\n" % self.ycoord out += " " * indent + " \n" out += (" " * indent + " %s\n" % self.prodclass) if prodclass else "" out += " " * indent + "" return out