473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

question about python "casting"

Hi all - Just started learning Python this past week and had a question
about using the lists in python with objects. (code attached below).

basically, what i want to do is read an xml file, and load a list of Objects
(in this case, TTSProject objects). the problem i see when i try to run it,
is the following:

name :
Traceback (most recent call last):
File "ProjectParser.py", line 63, in ?
print "name : ", p.name
AttributeError: 'NoneType' object has no attribute 'name'

i was wondering what the proper way to accomplish this would be. any help
would be appreciated.

thanks,

sang

from xml.dom import minidom

print "Starting"
xmldoc = minidom.parse('projects.xml')
print "Getting the root node"
root = xmldoc.childNodes
projectlist = root[0].childNodes
print "project list node"
class TTSProject:
def __init__(self):
self.tags = {};

projects = []

def getText(nodes):
for node in nodes:
if node.nodeType == node.TEXT_NODE:
return node.data

def getProject (nodes):
project = TTSProject()
if nodes.nodeType == nodes.ELEMENT_NODE:
datanodes = info.childNodes
dataStr = getText(datanodes)
# print info.nodeName, ":", dataStr
if dataStr == "name":
project.name = dataStr
if dataStr == "email":
project.email = dataStr
if dataStr == "admin":
project.admin = dataStr
if dataStr == "description":
project.description = dataStr
return project

for p in projectlist:
projectInfo = p.childNodes
for info in projectInfo:
project = getProject(info)
projects.append(project)

for proj in projects:
print "name : ", proj.name
print "email : ", proj.email
print "admin : ", proj.admin
print "description : ", proj.description
Jul 18 '05 #1
1 2264
sang park wrote:
name :
Traceback (most recent call last):
File "ProjectParser.py", line 63, in ?
print "name : ", p.name
AttributeError: 'NoneType' object has no attribute 'name'

i was wondering what the proper way to accomplish this would be. any help
would be appreciated.
Could it be that you have been executing a code different from
the one you were posting? In your posted code, you have a variable
called "proj", whereis from your exception, it appears the variable
is called "p".
if nodes.nodeType == nodes.ELEMENT_NODE:
datanodes = info.childNodes
dataStr = getText(datanodes)
# print info.nodeName, ":", dataStr
if dataStr == "name":
project.name = dataStr
if dataStr == "email":
project.email = dataStr
if dataStr == "admin":
project.admin = dataStr
if dataStr == "description":
project.description = dataStr


It is unlikely that this does what you want: Most likely, you
have elements "name", "email", etc in your XML document.
You need to look at some node's nodeName, to find out whether
it is a "name" node - looking at the text inside the node
won't help.

E.g. in your code above, if dataStr equals "email", you do

project.email = dataStr

Since dataStr is email, this is the same as

project.email = "email"

which is not what you want.

Regards,
Martin

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: maxim vexler | last post by:
in a book i am ready now : O'Reilly - Web Database Application with PHP and MySQL, 2nd ed. by David Lane, Hugh E. Williams on chapter 9 the author give an example for age validation :...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
13
by: dee | last post by:
Hi My code complies the following line: Session("passed") = 1 but puts wiggly error line under the second Session("passed") in the following expression: Session("passed") = Session("passed") +...
3
by: JimM | last post by:
I am trying to create a method in VS 2003 that validates an object argument is of the proper type and within a range of values. I am trying to use a Type to define the casting and object type for...
4
by: Vincent | last post by:
Regarding the dropdetect function, defined as follows: Sub dropdetect(DropFrm As Form, DropCtrl As Control, Button As Integer, Shift As Integer, X As Single, Y As Single) Is it possible to...
6
by: Bob | last post by:
Hello folks. I use this formating but if the value in dr is a null it generates an error "Object cannot be cast from DBNull to other types." Anybody know how I can deal with nulls? ...
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
7
by: Robert | last post by:
Thanks George, I really am grateful for attempts to be helpful, but this really doesn't answer the question in my OP. What I am looking for is an explanation of WHY things are this way (I was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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,...
0
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...
0
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...

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.