473,473 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2015
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
8
by: Birgit Rahm | last post by:
Hallo Newsgroup, I have the following problem: I work with Python 2.2 and invoke functions via CORBA ( I use onmiORB/omniORBpy) on a server. The server provides me a function, where the 3...
12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
10
by: Brad Tilley | last post by:
Is there an easier way to do this: def print_whats_returned(function): print function print type(function)
21
by: Dmitry Anikin | last post by:
I mean, it's very convenient when default parameters can be in any position, like def a_func(x = 2, y = 1, z): ... (that defaults must go last is really a C++ quirk which is needed for overload...
4
by: hanseymoon | last post by:
Dear newsgroup: I've got this long function, which works good overall to spell check words from a dictionary and I am not in a position to replace it. Can someone please see where or how it...
5
by: viscroad | last post by:
I have a confusion when I do some practice, the code and output are as following, print 'In fun()....' In fun().... None <function fun at 0x00CC1270> In fun()....
8
by: Viktor | last post by:
Can somebody give me an explanation what happened here (or point me to some docs)? Code: HMMM = None def w(fn): print 'fn:', id(fn) HMMM = fn
2
by: .rhavin grobert | last post by:
hello;-) i have that following little template that defines some type of vector. it works with structs and i want to use it also for simple pointers. the problem is, in following function......
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.