Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > what's the difference between these two methods? (aka, why doesn't one of them work?)

Reply
Thread Tools

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

 
 
JohnJSal
Guest
Posts: n/a
 
      11-02-2006
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'

 
Reply With Quote
 
 
 
 
JohnJSal
Guest
Posts: n/a
 
      11-02-2006

JohnJSal wrote:
> 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.

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      11-02-2006
JohnJSal wrote:

> 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

 
Reply With Quote
 
Carsten Haese
Guest
Posts: n/a
 
      11-02-2006
On Thu, 2006-11-02 at 12:28 -0800, JohnJSal wrote:
> 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


 
Reply With Quote
 
JohnJSal
Guest
Posts: n/a
 
      11-02-2006

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!

 
Reply With Quote
 
JohnJSal
Guest
Posts: n/a
 
      11-02-2006
JohnJSal wrote:
> 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!


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?

 
Reply With Quote
 
JohnJSal
Guest
Posts: n/a
 
      11-02-2006

JohnJSal wrote:
> JohnJSal wrote:
> > 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!

>
> 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

 
Reply With Quote
 
Rainy
Guest
Posts: n/a
 
      11-02-2006

JohnJSal wrote:
> JohnJSal wrote:
> > 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!

>
> 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.

 
Reply With Quote
 
Carsten Haese
Guest
Posts: n/a
 
      11-02-2006
On Thu, 2006-11-02 at 13:14 -0800, JohnJSal wrote:
> JohnJSal wrote:
> > JohnJSal wrote:
> > > 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!

> >
> > 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


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


 
Reply With Quote
 
JohnJSal
Guest
Posts: n/a
 
      11-02-2006
Carsten Haese wrote:

> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
difference between these two struct definitions oo@@oo C Programming 1 08-04-2006 02:19 PM
Difference between these two cameras? Tim Chmielewski Digital Photography 8 11-22-2005 11:59 PM
Difference between these two class declaration Rach Java 2 06-09-2004 11:50 AM
what is the difference between these two declarations Guybrush Threepwood C++ 7 02-26-2004 11:24 PM



Advertisments