1

I am trying to calculate the minimum time between 2 points, the following code I am using to grab the speed from the csv file based on whether the value is W or L and then taking the previously calculated costing or distance to calculate teh time.

CODE:

def minTime(self, a, b):
        self.readData(self)
        if line[5] == "W":
            speed = 16000
        else:
            speed = 6000


        return self.dict(a,b, [], {}, {}, [])[0] // speed

However I keep getting the error displayed below. I am relatively new to this and I am unsure where to move forward from here any help is appreciated.

Error: TypeError: readData() takes 1 positional argument but 2 were given

EDIT: here is the full code: `

size=100
class CrocMonitor:
    import csv

    def __init__(self, size):
        self.locationList = {}

        # Create unweighted adjacecency list
        self.readData()
        
        # Calculate weights
        self.storeDistance()

    def readData(self):
        with open('/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip/Locations.csv') as f:
            csv_reader = csv.reader(f, delimiter=',')
            
            # Skip heading line
            next(csv_reader)

            for line in csv_reader:
                
                # Read each of the CSV columns
                pointName=line[0]
                x= int(line[1])
                y= int(line[2])
                number= line[3]
                edge=line[4]
                water=False

                if line[5] == "W":
                    water=True

                    if line[5] == 'W':
                        speed = 16
                    else:
                        speed = 6

                # Add node to adjacency list
                if pointName not in self.locationList:
                    self.locationList[pointName] = [[x, y, number], {}]

                # Create unweighted edge
                if edge != "":
                    self.locationList[pointName][1][edge] = [water, 0]
                
    def computeDistance(self, a, b):
        x1 = a[0]
        x2 = b[0]
        y1 = a[1]
        y2 = b[1]

        # Calculated distance using formular
        distance = math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2) * 1.0)
        return distance

    def storeDistance(self):
        # Update weighting of each node
        for entry in self.locationList.items():
            neighbours = entry[1][1]
            for name, (water, distance) in neighbours.items():
                # Set weighting of neighbour
                neighbours[name] = self.computeDistance(entry[1][0], self.locationList[name][0])

    def bfs_shortest_path(self, start, goal):
    # keep track of explored nodes
        explored = []
    # keep track of all the paths to be checked
        queue = [[start]]
        test = []
    # return path if start is goal
        if start == goal:
            return "That was easy! Start = goal"
 
    # keeps looping until all possible paths have been checked
        while queue:
        # pop the first path from the queue
            path = queue.pop(0)
        # get the last node from the path
            node = path[-1]
            test.append(node)
            if node not in explored:
                neighbours = self.locationList[node][1]
            # go through all neighbour nodes, construct a new path and
            # push it into the queue
                for neighbour in neighbours:
                    test.append(neighbour)
                    new_path = list(path)
                    new_path.append(neighbour)
                    queue.append(new_path)
                # return path if neighbour is goal
                    if neighbour == goal:
                        return test
 
            # mark node as explored
                explored.append(node)

    # in case there's no path between the 2 nodes
        return "invalid"

    def computePathDistance (self,path):
        start = self.locationList(path[0])
        end = self.locationList(path[-1])
        distance = math.sqrt(math.pow(end[0] - start[0], 2) + math.pow(end[1] - start, 2) * 1.0)
        return distance

    def dic(self,a ,b , visited, distances, predecessors, spanningTree):
        if len(visited) == 0:
            distances[a] = 0
        nodea = self.locationList[a]
        nodeb = self.locationList[b]
        spanningTree.append(a)
        print(a)
        if a == b:
            # visited is actually the output of findScope
            visited.append(a)
            path = []
            while b != None:
                path.append(b)
                b = predecessors.get(b, None)
            return distances[a], path[::-1], spanningTree

        for neighbor in nodea[1].keys():
            if neighbor not in visited:
                # 500000 should be replaced with max int
                neighbourdistance = distances.get(neighbor, 500000)
                tentativedistance = distances[a] + nodea[1][neighbor]
                if tentativedistance < neighbourdistance:
                    distances[neighbor] = tentativedistance
                    predecessors[neighbor] = a
        visited.append(a)
        unvistedNodes = dict((k, distances.get(k, 500000)) for k in self.locationList.keys() if k not in visited)
        closestnode = min(unvistedNodes, key=unvistedNodes.get)
        return self.dic(closestnode, b, visited, distances, predecessors, spanningTree)

    def findPath(self, a, b):
        return self.dic(a,b, [], {}, {}, [])[1]

    def computeCosting(self, a, b):
    # unit costs for scanning all points on all paths between two locations and give exhaustive path for rangers to follow, returned as an list
        return self.dic(a,b, [], {}, {}, [])[0]
    


    def minTime(self, a, b):
        self.readData()[2]
        if line[5] == "W":
            speed = 16000
        else:
            speed = 6000


        return self.dic(a,b, [], {}, {}, [])[0] // speed
        

    def findScope(self, a, b):
        return self.dic(a,b, [], {}, {}, [])[2]

if __name__ == '__main__':
   
    cm=CrocMonitor(size)
   
    print(cm.computeCosting("15", "18"))
 
    print(cm.bfs_shortest_path('15', '18'))  

    
    

    print(cm.minTime("15", "18"))`
  • 3
    Show the definition of `readData`. It probably doesn't need `self` as an argument, that looks quite strange. – Passerby Nov 01 '21 at 01:32
  • Does this answer your question? [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – DaveyDaveDave Nov 03 '21 at 08:22

1 Answers1

1

You do not need to pass self to call a method.

self.readData()