def main(start, destination, file, delai_correspondance, delai_stations): """The Main Controller of the program, the functions are called here""" #Opening the file data = get_data(file) if not data: print('Fichier de description introuvable !') exit() #Building the linking map for each stations corresp = get_corresp(data, start, destination) if not corresp: return'Format du fichier de description invalide !' inexistant = 0 if clean_name(start) not in corresp: inexistant = start elif clean_name(destination) not in corresp: inexistant = destination if inexistant: return 'Station "%s" introuvable dans ce fichier de description' % inexistant #Finding the shortest path path_done = path(start, destination, corresp, delai_correspondance, delai_stations) #Show results return display(path_done)
def path (start, destination, corresp, delai_correspondance, delai_stations): to_do = [[0, clean_name(start), [], '']] done = list() while to_do: to_do.sort() station = to_do[0] cost = station[0] name = station[1] line = station[3] path_done = station[2] links = corresp[name] if clean_name(name) == clean_name(destination): print("Le trajet dure %s minutes" % secondes_to_higher(cost)) return path_done for link in links: if link[0] not in done: next_cost = cost if link[1] != line and line: next_cost += delai_correspondance next_cost += delai_stations next_path = path_done + [link] to_do.append([next_cost, link[0], next_path, link[1]]) done.append(name) to_do.pop(0) return 0
def __init__(self, bzz, name, account): self.name = clean_name(name) topic = new_topic_mask(self.name, "", "\x02") self.feed_room = Feed(bzz, account, topic) self.bzz = bzz self.participants = {} self.hsh_room = ""
def get_corresp(data, start, destination): #The dict in which we store the corresps map corresp = {} """Fetching the file for building a corresp dict looking like: corresp = {"La Défense":['Neuilly',"ligne 3",'Argenteuil'], ... ['CCial','ligne 3', 'Bercy'], ['Nanterre', 'ligne 4', 'Saint-Denis']}""" while data: #If the current line of text is a metro line if findall('\[(.+)\]',data[0]): line_name = findall('\[(.+)\]',data[0])[0] data.pop(0) stations = []#We store the ordered stations of the line in a list #Get the stations #While we stay on the same metro line while not findall('\[(.+)\]',data[0]): station = findall(r'\b.+',data[0])[0] if clean_name(station) == clean_name(start): station = clean_name(station) elif clean_name(station) == clean_name(destination): station = clean_name(station) stations.append(station) data.pop(0) #If there's nothing else to examine if not data: break #Infos about the line first_station = stations[0] last_station = stations[-1] #Creation of the corresp table for this line for d, station in enumerate(stations): linked_with = list() if d < (len(stations) - 1): linked_with.append([stations[d + 1], line_name, last_station]) if d > 0: linked_with.append([stations[d - 1], line_name, first_station]) if station not in corresp: corresp[station] = linked_with else: corresp[station].extend(linked_with) else: data.pop(0) return corresp
def __init__(self, bzz, name, acc): roomname = clean_name(name) self.name = copy.copy(roomname) for i in range(32 - len(roomname)): roomname += "\x00" roomnamelist = list(roomname) roomnamelist[31] = "\x02" roomname = "".join(roomnamelist) self.feed_room = Feed(bzz.agent, acc, roomname, False) self.agent = bzz.agent self.bzz = bzz self.participants = {} self.feedcollection_in = FeedCollection() self.hsh_out = zerohsh.decode("hex")
def load(self, hsh, owneraccount=None): savedJson = self.bzz.get(hsh.encode("hex")) print "savedj " + repr(savedJson) self.hsh_room = hsh r = json.loads(savedJson) self.name = clean_name(r['name']) for pubkeyhx in r['participants']: acc = Account() acc.set_public_key(clean_pubkey(pubkeyhx).decode("hex")) nick = acc.address.encode("hex") p = Participant(nick, "04" + acc.publickeybytes.encode("hex"), acc.address.encode("hex"), "") self.add(nick, p) # outgoing feed user is room publisher if owneraccount == None: owneraccount = self.feed_room.account self.feed_out = Feed(self.agent, owneraccount, self.name, True) hd = self.feed_out.head() if len(hd) == 64: self.hsh_out = hd.decode("hex")
def load(self, hsh, owneraccount=None): savedJson = self.bzz.get(hsh.encode("hex")) #sys.stderr.write("savedj " + repr(savedJson) + " from hash " + hsh.encode("hex") + "\n") self.hsh_room = hsh r = json.loads(savedJson) self.name = clean_name(r['name']) # outgoing feed user is room publisher if owneraccount == None: owneraccount = self.feed_room.account senderfeed = Feed(self.bzz, owneraccount, new_topic_mask(self.name, "", "\x06")) self.feedcollection = FeedCollection("room:" + self.name, senderfeed) for pubkeyhx in r['participants']: pubkey = clean_pubkey(pubkeyhx).decode("hex") nick = publickey_to_address(pubkey) p = Participant(nick.encode("hex"), None) p.set_public_key(pubkey) try: self.add(nick, p, False) except Exception as e: sys.stderr.write("skipping already added feed: '" + pubkey.encode("hex"))