what's the difference between these two methods? (aka, why doesn't one of them work?)

J

JohnJSal

Can someone explain to me why the first version of this method works,
but the second one doesn't? All I've changed (I think) is how the
information is nested. The error I'm getting is that the call to
xrc.XRCCTRL is not working in the second example. Instead of getting
the appropriate widget, it's returning None. Is this a result of the
nesting, or the for loops perhaps?

Thanks.


def OnSaveRecord(self, event):
textfield_values = []
for tab in self.notebook.GetCurrentPage().GetChildren():
for table in self.get_textfield_ids():
table_values = []
for textfield_id in table:
table_values.append(xrc.XRCCTRL(tab,
textfield_id).GetValue())
textfield_values.append(table_values)
self.save_to_database(textfield_values)

def get_textfield_ids(self):
return (('firstName', 'middleName', 'lastName', 'birthMonth',
'birthDay', 'birthYear', 'country', 'state', 'city'),
('jobTitle', 'salary', 'labBuilding', 'labRoom',
'labPhone'),
('localAddress', 'foreignAddress', 'emailAddress',
'homePhone',
'foreignPhone', 'cellPhone'), ('university1',
'yearStart1',
'yearEnd1', 'degree1', 'university2', 'yearStart2',
'yearEnd2',
'degree2', 'university3', 'yearStart3', 'yearEnd3',
'degree3',
'university4', 'yearStart4', 'yearEnd4', 'degree4'),
('notes'))

-----------------------------------------

def OnSaveRecord(self, event):
textfield_values = []
for tab in self.notebook.GetCurrentPage().GetChildren():
for textfield_id in self.get_textfield_ids():
textfield_values.append(xrc.XRCCTRL(tab,
textfield_id).GetValue())
self.save_to_database(textfield_values)

def get_textfield_ids(self):
return ('firstName', 'middleName', 'lastName', 'birthMonth',
'birthDay', 'birthYear', 'country', 'state', 'city',
'jobTitle', 'salary', 'labBuilding', 'labRoom',
'labPhone',
'localAddress', 'foreignAddress', 'emailAddress',
'homePhone',
'foreignPhone', 'cellPhone', 'university1',
'yearStart1',
'yearEnd1', 'degree1', 'university2', 'yearStart2',
'yearEnd2',
'degree2', 'university3', 'yearStart3', 'yearEnd3',
'degree3',
'university4', 'yearStart4', 'yearEnd4', 'degree4',
'notes')

Traceback (most recent call last):
File "C:\Python25\myscripts\labdb\dbapp.py", line 91, in OnSaveRecord
table_values.append(xrc.XRCCTRL(tab, textfield_id).GetValue())
AttributeError: 'NoneType' object has no attribute 'GetValue'
 
J

JohnJSal

JohnJSal said:
Can someone explain to me why the first version of this method works,
but the second one doesn't?

Sorry, it's the first one that doesn't work. The second one does.
 
P

Peter Otten

JohnJSal said:
Can someone explain to me why the first version of this method works,
but the second one doesn't? All I've changed (I think) is how the
information is nested. The error I'm getting is that the call to
xrc.XRCCTRL is not working in the second example. Instead of getting
the appropriate widget, it's returning None. Is this a result of the
nesting, or the for loops perhaps?

Thanks.


def OnSaveRecord(self, event):
textfield_values = []
for tab in self.notebook.GetCurrentPage().GetChildren():
for table in self.get_textfield_ids():
table_values = []
for textfield_id in table:
Put in a
print textfield_id

here. You'll see an 'n'

before the exception occurs, because...
table_values.append(xrc.XRCCTRL(tab,
textfield_id).GetValue())
textfield_values.append(table_values)
self.save_to_database(textfield_values)

def get_textfield_ids(self):
return (('firstName', 'middleName', 'lastName', 'birthMonth',
'birthDay', 'birthYear', 'country', 'state', 'city'),
('jobTitle', 'salary', 'labBuilding', 'labRoom',
'labPhone'),
('localAddress', 'foreignAddress', 'emailAddress',
'homePhone',
'foreignPhone', 'cellPhone'), ('university1',
'yearStart1',
'yearEnd1', 'degree1', 'university2', 'yearStart2',
'yearEnd2',
'degree2', 'university3', 'yearStart3', 'yearEnd3',
'degree3',
'university4', 'yearStart4', 'yearEnd4', 'degree4'),
('notes'))

....the above is not a 1-tuple, but an ordinary string. You forgot the
trailing comma:

('notes',)

Peter
 
C

Carsten Haese

Can someone explain to me why the first version of this method works,
but the second one doesn't? All I've changed (I think) is how the
information is nested. The error I'm getting is that the call to
xrc.XRCCTRL is not working in the second example. Instead of getting
the appropriate widget, it's returning None. Is this a result of the
nesting, or the for loops perhaps?
[...]
Traceback (most recent call last):
File "C:\Python25\myscripts\labdb\dbapp.py", line 91, in OnSaveRecord
table_values.append(xrc.XRCCTRL(tab, textfield_id).GetValue())
AttributeError: 'NoneType' object has no attribute 'GetValue'

You might find it helpful to inspect (e.g. print) textfield_id before
the line that causes the exception.

-Carsten
 
J

JohnJSal

Peter Otten wrote:

...the above is not a 1-tuple, but an ordinary string. You forgot the
trailing comma:

('notes',)

Right you are! Now it works! :)

Thanks!
 
J

JohnJSal

JohnJSal said:
Peter Otten wrote:



Right you are! Now it works! :)

Thanks!

Oh great, now I've moved on to another issue. It seems that the list
appending isn't working right. All that gets added to a list is the
last value, not everything. Am I doing something wrong with the append
method?
 
J

JohnJSal

JohnJSal said:
Oh great, now I've moved on to another issue. It seems that the list
appending isn't working right. All that gets added to a list is the
last value, not everything. Am I doing something wrong with the append
method?

Ah, got it! I was reinitializing the table_values list in the wrong
place
 
R

Rainy

JohnJSal said:
Oh great, now I've moved on to another issue. It seems that the list
appending isn't working right. All that gets added to a list is the
last value, not everything. Am I doing something wrong with the append
method?

Well, append method is for appending a value to a list. A single value.
You can use extend method (iirc) to extend a list with another list.
 
C

Carsten Haese

Ah, got it! I was reinitializing the table_values list in the wrong
place

The fact that you were able to answer your own question only a few
minutes later indicates to me that you should set your "I give up and
ask the list" threshold a tad higher.

-Carsten
 
J

JohnJSal

Carsten said:
The fact that you were able to answer your own question only a few
minutes later indicates to me that you should set your "I give up and
ask the list" threshold a tad higher.

That's a perfectly valid comment, but in this case just not applicable.
I spent a lot of time working through my original question before
posting, but I just couldn't get it. It's not like I didn't try
anything at all before posting the follow-up, either. I just happened
to notice one more thing after posting.
 
F

Fredrik Lundh

JohnJSal said:
That's a perfectly valid comment, but in this case just not applicable.
I spent a lot of time working through my original question before
posting, but I just couldn't get it.

how do you fit "a lot of time" into 18 minutes?

</F>
 
J

John Salerno

Fredrik said:
how do you fit "a lot of time" into 18 minutes?

</F>

Hmmm, I had tried to cancel sending that post but I guess it didn't
work. As I was sending it, I said to myself, "well, maybe I didn't spend
enough time before asking."

But I guess rather than "a lot of time", I'm thinking I tried "a lot of
things," or at least all I could think of.
 
S

Steve Holden

John said:
Hmmm, I had tried to cancel sending that post but I guess it didn't
work. As I was sending it, I said to myself, "well, maybe I didn't spend
enough time before asking."

But I guess rather than "a lot of time", I'm thinking I tried "a lot of
things," or at least all I could think of.

Don't worry. It's sometimes difficult for the effbot to remember we
aren't all as fearsomely intelligent as it is. I think it does a
remarkably complete emulation of a human being:

http://www.flickr.com/photos/30842681@N00/152495923/

For what it's worth it's also amazingly helpful if you can ignore to
sometimes acerbic wit.

regards
Steve
 
J

John Salerno

Steve said:
Don't worry. It's sometimes difficult for the effbot to remember we
aren't all as fearsomely intelligent as it is. I think it does a
remarkably complete emulation of a human being:

http://www.flickr.com/photos/30842681@N00/152495923/

For what it's worth it's also amazingly helpful if you can ignore to
sometimes acerbic wit.

regards
Steve

Heh heh. Things wouldn't be the same without him...I mean "it". :)
 

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top