Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Passing Functions

Reply
Thread Tools

Passing Functions

 
 
yoro
Guest
Posts: n/a
 
      03-11-2011
Hi,

I am having an issue with passing values from one function to another
- I am trying to fill a list in one function using the values
contained in other functions as seen below:

infinity = 1000000
invalid_node = -1
startNode = 0

#Values to assign to each node
class Node:
distFromSource = infinity
previous = invalid_node
visited = False

#read in all network nodes
def network():
f = open ('network.txt', 'r')
theNetwork = [[int(node) for node in line.split(',')] for line in
f.readlines()]
print theNetwork

return theNetwork

#for each node assign default values
def populateNodeTable():
nodeTable = []
index = 0
f = open('network.txt', 'r')
for line in f:
node = map(int, line.split(','))
nodeTable.append(Node())

print "The previous node is " ,nodeTable[index].previous
print "The distance from source is
" ,nodeTable[index].distFromSource
index +=1
nodeTable[startNode].distFromSource = 0

return nodeTable

#find the nearest neighbour to a particular node
def nearestNeighbour(currentNode, theNetwork):
nearestNeighbour = []
nodeIndex = 0
for node in nodeTable:
if node != 0 and currentNode.visited == false:
nearestNeighbour.append(nodeIndex)
nodeIndex +=1

return nearestNeighbour

if __name__ == "__main__":
nodeTable = populateNodeTable()
theNetwork = network()
nearestNeighbour(currentNode, theNetwork, )

So, I am trying to look at the values provided by the network
function, set all nodes to 'visited = false' in populateNodeTable
function and then determine the nodes' nearest neighbour by looking at
the values provided in the previous function, though I get this error
message:

if node != 0 and currentNode.visited == false:
AttributeError: 'int' object has no attribute 'visited'

I'm not sure what to try next

 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      03-11-2011
On Mar 11, 11:13*am, yoro <(E-Mail Removed)> wrote:
> Hi,
>
> I am having an issue with passing values from one function to another
> - I am trying to fill a list in one function using the values
> contained in other functions as seen below:
>
> infinity = 1000000
> invalid_node = -1
> startNode = 0
>
> #Values to assign to each node
> class Node:
> * * *distFromSource = infinity
> * * *previous = invalid_node
> * * *visited = False
>
> #read in all network nodes
> def network():
> * * f = open ('network.txt', 'r')
> * * theNetwork = [[int(node) for node in line.split(',')] for line in
> f.readlines()]
> * * print theNetwork
>
> * * return theNetwork
>
> #for each node assign default values
> def populateNodeTable():
> * * nodeTable = []
> * * index = 0
> * * f = open('network.txt', 'r')
> * * for line in f:
> * * * node = map(int, line.split(','))
> * * * nodeTable.append(Node())
>
> * * * print "The previous node is " ,nodeTable[index].previous
> * * * print "The distance from source is
> " ,nodeTable[index].distFromSource
> * * * index +=1
> * * nodeTable[startNode].distFromSource = 0
>
> * * return nodeTable
>
> #find the nearest neighbour to a particular node
> def nearestNeighbour(currentNode, theNetwork):
> * * *nearestNeighbour = []
> * * *nodeIndex = 0
> * * *for node in nodeTable:
> * * * * * if node != 0 and currentNode.visited == false:
> * * * * * * *nearestNeighbour.append(nodeIndex)
> * * * * * * *nodeIndex +=1
>
> * * *return nearestNeighbour
>
> if __name__ == "__main__":
> * * nodeTable = populateNodeTable()
> * * theNetwork = network()
> * * nearestNeighbour(currentNode, theNetwork, )
>
> So, I am trying to look at the values provided by the network
> function, *set all nodes to 'visited = false' in populateNodeTable
> function and then determine the nodes' nearest neighbour by looking at
> the values provided in the previous function, though I get this error
> message:
>
> if node != 0 and currentNode.visited == false:
> AttributeError: 'int' object has no attribute 'visited'
>
> I'm not sure what to try next


Well, for starters, you're not actually instantiating any Nodes:

> theNetwork = [[int(node) for node in line.split(',')] for line in f.readlines()]


You're returning a list of integers, none of which have the visited
attribute.

At the very least, you need a constructor on your Node class:

class Node:
def __init__(self, distFromSource=infinity, previous=invalid_node,
visited=False):
self.distFromSource = distFromSource
self.previous = previous
self.visited = False

Now, I'm _assuming_ your network.txt file consists of 3 number
separated by commas, so in network() it should probably be something
like:

def network():
with open('network.txt', 'r') as f:
network_data = ([int(node) for node in line.split(',')] for line
in f.readlines())
theNetwork = [Node(*data) for data in network_data]
return theNetwork

The with statement ensures the file is closed at the end of the block.
'network_data' is a generator that reads each line and splits it into
a list of ints: '[x, y, z]'
'Node(*data)' takes a list of ints and creates a node from those
values.

Since both populateNodeTable and nearestNeighbour relay on nodeTable,
you might be better off making a NodeTable class.

None of this is tested, sorry, but hopefully it'll set you in the
right direction.
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      03-11-2011
On 11/03/2011 01:13, yoro wrote:
> Hi,
>
> I am having an issue with passing values from one function to another
> - I am trying to fill a list in one function using the values
> contained in other functions as seen below:
>
> infinity = 1000000
> invalid_node = -1
> startNode = 0
>
> #Values to assign to each node
> class Node:
> distFromSource = infinity
> previous = invalid_node
> visited = False
>
> #read in all network nodes
> def network():
> f = open ('network.txt', 'r')
> theNetwork = [[int(node) for node in line.split(',')] for line in
> f.readlines()]
> print theNetwork
>
> return theNetwork
>
> #for each node assign default values
> def populateNodeTable():
> nodeTable = []
> index = 0
> f = open('network.txt', 'r')
> for line in f:
> node = map(int, line.split(','))
> nodeTable.append(Node())
>
> print "The previous node is " ,nodeTable[index].previous
> print "The distance from source is
> " ,nodeTable[index].distFromSource
> index +=1
> nodeTable[startNode].distFromSource = 0
>
> return nodeTable
>
> #find the nearest neighbour to a particular node
> def nearestNeighbour(currentNode, theNetwork):
> nearestNeighbour = []
> nodeIndex = 0
> for node in nodeTable:
> if node != 0 and currentNode.visited == false:
> nearestNeighbour.append(nodeIndex)
> nodeIndex +=1
>
> return nearestNeighbour
>
> if __name__ == "__main__":
> nodeTable = populateNodeTable()
> theNetwork = network()
> nearestNeighbour(currentNode, theNetwork, )
>
> So, I am trying to look at the values provided by the network
> function, set all nodes to 'visited = false' in populateNodeTable
> function and then determine the nodes' nearest neighbour by looking at
> the values provided in the previous function, though I get this error
> message:
>
> if node != 0 and currentNode.visited == false:
> AttributeError: 'int' object has no attribute 'visited'
>
> I'm not sure what to try next
>

In nearestNeighbour, 'currentNode' is an int because that's what you're
passing in ... except that you aren't.

You're passing in the value of the global 'currentNode', which doesn't
exist. Perhaps you meant 'startNode'?

When I run the above program I get:

Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\Desktop\network.py",
line 49, in <module>
nearestNeighbour(currentNode, theNetwork, )
NameError: name 'currentNode' is not defined
 
Reply With Quote
 
yoro
Guest
Posts: n/a
 
      03-11-2011
On Mar 11, 2:00*am, MRAB <(E-Mail Removed)> wrote:
> On 11/03/2011 01:13, yoro wrote:
>
> > Hi,

>
> > I am having an issue with passing values from one function to another
> > - I am trying to fill a list in one function using the values
> > contained in other functions as seen below:

>
> > infinity = 1000000
> > invalid_node = -1
> > startNode = 0

>
> > #Values to assign to each node
> > class Node:
> > * * * distFromSource = infinity
> > * * * previous = invalid_node
> > * * * visited = False

>
> > #read in all network nodes
> > def network():
> > * * *f = open ('network.txt', 'r')
> > * * *theNetwork = [[int(node) for node in line.split(',')] for line in
> > f.readlines()]
> > * * *print theNetwork

>
> > * * *return theNetwork

>
> > #for each node assign default values
> > def populateNodeTable():
> > * * *nodeTable = []
> > * * *index = 0
> > * * *f = open('network.txt', 'r')
> > * * *for line in f:
> > * * * *node = map(int, line.split(','))
> > * * * *nodeTable.append(Node())

>
> > * * * *print "The previous node is " ,nodeTable[index].previous
> > * * * *print "The distance from source is
> > " ,nodeTable[index].distFromSource
> > * * * *index +=1
> > * * *nodeTable[startNode].distFromSource = 0

>
> > * * *return nodeTable

>
> > #find the nearest neighbour to a particular node
> > def nearestNeighbour(currentNode, theNetwork):
> > * * * nearestNeighbour = []
> > * * * nodeIndex = 0
> > * * * for node in nodeTable:
> > * * * * * *if node != 0 and currentNode.visited == false:
> > * * * * * * * nearestNeighbour.append(nodeIndex)
> > * * * * * * * nodeIndex +=1

>
> > * * * return nearestNeighbour

>
> > if __name__ == "__main__":
> > * * *nodeTable = populateNodeTable()
> > * * *theNetwork = network()
> > * * *nearestNeighbour(currentNode, theNetwork, )

>
> > So, I am trying to look at the values provided by the network
> > function, *set all nodes to 'visited = false' in populateNodeTable
> > function and then determine the nodes' nearest neighbour by looking at
> > the values provided in the previous function, though I get this error
> > message:

>
> > if node != 0 and currentNode.visited == false:
> > AttributeError: 'int' object has no attribute 'visited'

>
> > I'm not sure what to try next

>
> In nearestNeighbour, 'currentNode' is an int because that's what you're
> passing in ... except that you aren't.
>
> You're passing in the value of the global 'currentNode', which doesn't
> exist. Perhaps you meant 'startNode'?
>
> When I run the above program I get:
>
> Traceback (most recent call last):
> * *File "C:\Documents and Settings\Administrator\Desktop\network.py",
> line 49, in <module>
> * * *nearestNeighbour(currentNode, theNetwork, )
> NameError: name 'currentNode' is not defined


I've found the error, I had to type in:

for node in nodeTable:
if node != 0 and Node.visited == False:
 
Reply With Quote
 
John Nagle
Guest
Posts: n/a
 
      03-11-2011
On 3/11/2011 5:49 AM, yoro wrote:

> I've found the error, I had to type in:
>
> for node in nodeTable:
> if node != 0 and Node.visited == False:


That's just your first error.
(Also, you shouldn't have anything but Node items in
nodeTable, so you don't need the "node != 0".)

The biggest problem is at

#Values to assign to each node
> > class Node:
> > distFromSource = infinity
> > previous = invalid_node
> > visited = False


Those are variables of the entire class. Every
instance of Node shares the same variables. You
need

class Node:
def __init__(self) :
self.distFromSource = infinity
self.previous = invalid_node
self.visited = False

John Nagle

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
Passing member functions to C functions? Steven T. Hatton C++ 7 10-07-2004 02:12 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Help! Passing Templates functions to template functions ILLOGIC C++ 1 06-01-2004 10:51 PM
Exportable class functions as stand alone functions to .DLL or .SO Timothy Wong C++ 3 05-20-2004 01:44 PM



Advertisments