473,834 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ 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(cur rent, classtofind, topnode = None):
if topnode == None: topnode = current

# if it's an xml element...
if current.nodeTyp e == 1:
print current.nodeNam e, ':', current.getAttr ibute('class')
if current.getAttr ibute('class') == classtofind:
print 'Returning node:', current
return current
elif current.hasChil dNodes():
findelement(cur rent.firstChild , classtofind, topnode)
elif current.nextSib ling:
findelement(cur rent.nextSiblin g, classtofind, topnode)

elif (current.parent Node != topnode) \

and (current.parent Node.nextSiblin g != None):

findelement(cur rent.parentNode .nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# others (text, comment, etc)

else:

if current.nextSib ling:

findelement(cur rent.nextSiblin g, classtofind, topnode)

elif (current.parent Node != topnode) \

and (current.parent Node.nextSiblin g != None):

findelement(cur rent.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(blo g.documentEleme nt, '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 2035
On Mon, 2005-10-31 at 14:12, no****@gmail.co m 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(cur rent, classtofind, topnode = None):
if topnode == None: topnode = current

# if it's an xml element...
if current.nodeTyp e == 1:
print current.nodeNam e, ':', current.getAttr ibute('class')
if current.getAttr ibute('class') == classtofind:
print 'Returning node:', current
return current
elif current.hasChil dNodes():
findelement(cur rent.firstChild , classtofind, topnode)
elif current.nextSib ling:
findelement(cur rent.nextSiblin g, classtofind, topnode)

elif (current.parent Node != topnode) \

and (current.parent Node.nextSiblin g != None):

findelement(cur rent.parentNode .nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# others (text, comment, etc)

else:

if current.nextSib ling:

findelement(cur rent.nextSiblin g, classtofind, topnode)

elif (current.parent Node != topnode) \

and (current.parent Node.nextSiblin g != None):

findelement(cur rent.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(blo g.documentEleme nt, '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.co m 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(cur rent, classtofind, topnode = None):
if topnode == None: topnode = current

# if it's an xml element...
if current.nodeTyp e == 1:
print current.nodeNam e, ':', current.getAttr ibute('class')
if current.getAttr ibute('class') == classtofind:
print 'Returning node:', current
return current
elif current.hasChil dNodes():
findelement(cur rent.firstChild , classtofind, topnode)
Should be
return findelement(cur rent.firstChild , classtofind, topnode)
and similarly wherever you call findelement().
elif current.nextSib ling:
findelement(cur rent.nextSiblin g, classtofind, topnode)

elif (current.parent Node != topnode) \

and (current.parent Node.nextSiblin g != None):

findelement(cur rent.parentNode .nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# others (text, comment, etc)

else:

if current.nextSib ling:

findelement(cur rent.nextSiblin g, classtofind, topnode)

elif (current.parent Node != topnode) \

and (current.parent Node.nextSiblin g != None):

findelement(cur rent.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(blo g.documentEleme nt, '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
4969
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
8
4549
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 arguments are out-arguments and the return is void / None. How can I get these out arguments? I have read that every argument is seen as local to the function and can not be used as return value.
12
10046
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 class and a function in Python ??? Consider the following code fragments : # Fragment 1 begins a = 1
10
1711
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
2343
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 resolution, isn't it?) and when calling, just omit parameter when you want to use defaults: a_func(, , 3)
4
1456
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 might be chopping up words like: don't. It brings them back as: 't, chopping off the "don" before the apostrophe. I've looked over the whole situation and ran many $string tests....and it appears to be narrowed it down to this. I may be wrong and...
5
1155
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
1540
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
1409
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... ______________________________________ template <class T, typename I>
0
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9643
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10786
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10503
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10544
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9326
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7754
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.