Reputation: 630
So I'm working on a program which involves representing airline flights as a graph. I have a GraphImplementation.py file here, which implements the graph:
class Node:
def __init__(self, city, code, country, continent, tZone, lat, long, pop, region):
self.city = city
self.code = code
self.country = country
self.continent = continent
self.tZone = tZone
self.lat = lat
self.long = long
self.pop = pop
self.region = region
self.connections = {}
def addNeighbor(self, neighborNode, length=0):
self.connections[neighborNode] = length
def __str__(self):
return str(self.city) + ' connections: ' + str([x.city for x in self.connections])
def getConnections(self):
return self.connections.keys()
def getCity(self):
return self.city
def getWeight(self,node):
return self.connections[node]
class Graph:
def __init__(self):
self.nodeList = {}
self.nodeCnt = 0
def addNode(self, newNode):
self.nodeCnt = self.nodeCnt + 1
self.nodeList[newNode.city] = newNode
return newNode
def getNode(self,city):
if city in self.nodeList:
return self.nodeList[city]
else:
return None
def __contains__(self,n):
return n in self.nodeList
def addEdge(self,f, fNode, t, tNode, cost=0):
if f not in self.nodeList:
nv = self.addNode(fNode)
if t not in self.nodeList:
nv = self.addNode(tNode)
self.nodeList[f].addNeighbor(self.nodeList[t], cost)
def getNodes(self):
return self.nodeList.keys()
def longestFlight(self):
print "enter"
longestFlight = None
maxCost = None
for nodeCity, node in nodeList.iteritems():
for connection, cost in node.connections.iteritems():
print cost
if cost > maxCost:
maxCost = cost
longestFlight = node.city + " to " + connection.city
return longestFlight
def __iter__(self):
return iter(self.nodeList.values())
As you can see, the method "longestFlight()", which returns a string containing the longest flight, is clearly defined.
Below is the main program where I try to invoke the longestFlight() method (prior to this i load the graph data from a json file).
import json
import GraphImplementation as G
from ParseJson import parseJSONToGraph
def main():
jsonFile = open("map_data.json")
jsonData = json.load(jsonFile)
graph = G.Graph()
graph = parseJSONToGraph(graph, jsonData)
cont = 'y'
#Enter main loop
while cont == 'y':
print " Menu options:"
print " 1: get longest flight "
menuChoice = raw_input("Enter menu choice: ")
print menuChoice
if int(menuChoice) == 1:
print graph.longestFlight() + "\n"
cont = raw_input("Do you want to continue? (y/n)")
if __name__ == "__main__":
main()
When I run my main program and I try to invoke the longestFlight() method on my graph however (on line 24), I get the following error from python: Traceback (most recent call last):
File "main.py", line 32, in <module>
main()
File "main.py", line 24, in main
print graph.longestFlight() + "\n"
AttributeError: Graph instance has no attribute 'longestFlight'
Upvotes: 0
Views: 328
Reputation: 808
Your indentation is not correct on your classes. When compiled the compiler doesnt think the longestFlight()
function belongs to graph
therefor giving you AttributeError: Graph instance has no attribute 'longestFlight'
. Tab in your methods below your class headings like this:
class Graph:
# other functions indented like this also
def longestFlight():
# do whatever
Upvotes: 1