471,601 Members | 1,164 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,601 software developers and data experts.

Function returns none

I'm trying to write a website updating script, but when I run the
script, my function to search the DOM tree returns None instead of what
it should.

I have this program:
--------
import sys
from xml.dom.minidom import parse
# search the tree for an element with a particular class

def findelement(current, classtofind, topnode = None):
if topnode == None: topnode = current

# if it's an xml element...
if current.nodeType == 1:
print current.nodeName, ':', current.getAttribute('class')
if current.getAttribute('class') == classtofind:
print 'Returning node:', current
return current
elif current.hasChildNodes():
findelement(current.firstChild, classtofind, topnode)
elif current.nextSibling:
findelement(current.nextSibling, classtofind, topnode)

elif (current.parentNode != topnode) \

and (current.parentNode.nextSibling != None):

findelement(current.parentNode.nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# others (text, comment, etc)

else:

if current.nextSibling:

findelement(current.nextSibling, classtofind, topnode)

elif (current.parentNode != topnode) \

and (current.parentNode.nextSibling != None):

findelement(current.parentNode.nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# parse the document

blog = parse('/home/noah/dev/blog/template.html')

# find a post

postexample = findelement(blog.documentElement, 'post')

print 'Got node: ', postexample

-----

My output is this:

-----
html :
head :
title :
body :
h1 :
ul :
li :
h2 :
ol :
li : post
Returning node: <DOM Element: li at -0x48599c74>
Got node: None
-----

The function finds the right element fine, and says it will return <DOM
Element: li at -0x48599c74>, but the program gets None instead. What's
happening here? Any suggestions?

Oct 31 '05 #1
3 1944
On Mon, 2005-10-31 at 14:12, no****@gmail.com wrote:
I'm trying to write a website updating script, but when I run the
script, my function to search the DOM tree returns None instead of what
it should.

I have this program:
--------
import sys
from xml.dom.minidom import parse
# search the tree for an element with a particular class

def findelement(current, classtofind, topnode = None):
if topnode == None: topnode = current

# if it's an xml element...
if current.nodeType == 1:
print current.nodeName, ':', current.getAttribute('class')
if current.getAttribute('class') == classtofind:
print 'Returning node:', current
return current
elif current.hasChildNodes():
findelement(current.firstChild, classtofind, topnode)
elif current.nextSibling:
findelement(current.nextSibling, classtofind, topnode)

elif (current.parentNode != topnode) \

and (current.parentNode.nextSibling != None):

findelement(current.parentNode.nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# others (text, comment, etc)

else:

if current.nextSibling:

findelement(current.nextSibling, classtofind, topnode)

elif (current.parentNode != topnode) \

and (current.parentNode.nextSibling != None):

findelement(current.parentNode.nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# parse the document

blog = parse('/home/noah/dev/blog/template.html')

# find a post

postexample = findelement(blog.documentElement, 'post')

print 'Got node: ', postexample

-----

My output is this:

-----
html :
head :
title :
body :
h1 :
ul :
li :
h2 :
ol :
li : post
Returning node: <DOM Element: li at -0x48599c74>
Got node: None
-----

The function finds the right element fine, and says it will return <DOM
Element: li at -0x48599c74>, but the program gets None instead. What's
happening here? Any suggestions?


You have a lot of cases where findelement is called recursively and then
its return value is discarded instead of being turned into a return
value to the caller. In those cases, execution simply falls off the end
of the function and None is returned implicitly (and silently, since you
don't have a print "Returning None" at the end of the function).

HTH,

Carsten.
Oct 31 '05 #2
no****@gmail.com wrote:
I'm trying to write a website updating script, but when I run the
script, my function to search the DOM tree returns None instead of what
it should.
When you call findelement() recursively you have to return the value from the recursive call to the next caller up. See example below.

Kent

I have this program:
--------
import sys
from xml.dom.minidom import parse
# search the tree for an element with a particular class

def findelement(current, classtofind, topnode = None):
if topnode == None: topnode = current

# if it's an xml element...
if current.nodeType == 1:
print current.nodeName, ':', current.getAttribute('class')
if current.getAttribute('class') == classtofind:
print 'Returning node:', current
return current
elif current.hasChildNodes():
findelement(current.firstChild, classtofind, topnode)
Should be
return findelement(current.firstChild, classtofind, topnode)
and similarly wherever you call findelement().
elif current.nextSibling:
findelement(current.nextSibling, classtofind, topnode)

elif (current.parentNode != topnode) \

and (current.parentNode.nextSibling != None):

findelement(current.parentNode.nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# others (text, comment, etc)

else:

if current.nextSibling:

findelement(current.nextSibling, classtofind, topnode)

elif (current.parentNode != topnode) \

and (current.parentNode.nextSibling != None):

findelement(current.parentNode.nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# parse the document

blog = parse('/home/noah/dev/blog/template.html')

# find a post

postexample = findelement(blog.documentElement, 'post')

print 'Got node: ', postexample

-----

My output is this:

-----
html :
head :
title :
body :
h1 :
ul :
li :
h2 :
ol :
li : post
Returning node: <DOM Element: li at -0x48599c74>
Got node: None
-----

The function finds the right element fine, and says it will return <DOM
Element: li at -0x48599c74>, but the program gets None instead. What's
happening here? Any suggestions?

Oct 31 '05 #3
Thanks, that worked!

Oct 31 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Penn Markham | last post: by
8 posts views Thread by Birgit Rahm | last post: by
12 posts views Thread by Gaurav Veda | last post: by
10 posts views Thread by Brad Tilley | last post: by
5 posts views Thread by viscroad | last post: by
8 posts views Thread by Viktor | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.