Skip to content
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
6 changes: 4 additions & 2 deletions Wrangler/Factor.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
__all__ = ['Factor']
__all__ = ["Factor"]


class Factor(dict):
"""
Transit config (or "factor")
All attributes are stored in the dictionary
"""

def __init__(self):
dict.__init__(self)
self.comment=''
self.comment = ""

def __repr__(self):
s = "FACTOR "
Expand Down
38 changes: 25 additions & 13 deletions Wrangler/Faresystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .Logger import WranglerLogger


class Faresystem(collections.OrderedDict):
"""
Faresystem definition. A faresystem is a Cube Public Transport construct representing a single faresystem.
Expand All @@ -12,12 +13,14 @@ class Faresystem(collections.OrderedDict):
def __init__(self):
collections.OrderedDict.__init__(self)

self.fare_zone_mat = {} # origin fare zone (int) => dest fare zone (int) => fare (float)
self.fare_zone_mat = (
{}
) # origin fare zone (int) => dest fare zone (int) => fare (float)

def __repr__(self):
s = "FARESYSTEM "

fields = ['%s=%s' % (k,v) for k,v in self.items()]
fields = ["%s=%s" % (k, v) for k, v in self.items()]
s += ", ".join(fields)

return s
Expand All @@ -28,7 +31,7 @@ def getFareMatrixId(self):
"""
if "FAREMATRIX" in self.keys():
faremat = self["FAREMATRIX"]
return faremat[faremat.rfind(".")+1:]
return faremat[faremat.rfind(".") + 1 :]
return None

def getId(self):
Expand All @@ -50,11 +53,17 @@ def getFareZoneMatrixLines(self):
Returns farezone to farezone string for writing.
"""
s = ""
if len(self.fare_zone_mat) == 0: return s
if len(self.fare_zone_mat) == 0:
return s

for farezone_i in sorted(self.fare_zone_mat.keys()):
for farezone_j in sorted(self.fare_zone_mat[farezone_i].keys()):
s += "{} {} {} {:.4f}\n".format(self.getId(), farezone_i, farezone_j, self.fare_zone_mat[farezone_i][farezone_j])
s += "{} {} {} {:.4f}\n".format(
self.getId(),
farezone_i,
farezone_j,
self.fare_zone_mat[farezone_i][farezone_j],
)
return s

@staticmethod
Expand All @@ -64,24 +73,27 @@ def readFareZoneMatrixFile(farezonematrix_file, faresystems_dict):
and updates the given dictionary of faresystems.
"""
WranglerLogger.debug("Reading {}".format(farezonematrix_file))
f = open(farezonematrix_file, 'r')
f = open(farezonematrix_file, "r")
while True:
line = f.readline().strip()
if not line: break
if not line:
break

row = re.split("[\s+\,]", line) # split on whitespace or comma
row = re.split("[\s+\,]", line) # split on whitespace or comma

farematid = row[0]
farezone_i = int(row[1])
farezone_j = int(row[2])
farematid = row[0]
farezone_i = int(row[1])
farezone_j = int(row[2])
for fare_str in row[3:]:
fare_val = float(fare_str)
# not efficient but it's ok
for faresystem_id in faresystems_dict.keys():
if faresystems_dict[faresystem_id].getFareMatrixId() == farematid:
faresystems_dict[faresystem_id].setFarezoneODPair(farezone_i, farezone_j, fare_val)
faresystems_dict[faresystem_id].setFarezoneODPair(
farezone_i, farezone_j, fare_val
)
# print("faresystem {} farematid {} i={} j={} fare={}".format(
# faresystem_id, farematid, farezone_i, farezone_j, fare_val))

farezone_j += 1
f.close()
f.close()
Loading