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

Discussion in 'Python' started by JohnJSal, Nov 2, 2006.

  1. JohnJSal

    JohnJSal Guest

    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'
     
    JohnJSal, Nov 2, 2006
    #1
    1. Advertising

  2. JohnJSal

    JohnJSal Guest

    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.
     
    JohnJSal, Nov 2, 2006
    #2
    1. Advertising

  3. JohnJSal

    Peter Otten Guest

    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
     
    Peter Otten, Nov 2, 2006
    #3
  4. Re: what's the difference between these two methods? (aka, whydoesn't one of them work?)

    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
     
    Carsten Haese, Nov 2, 2006
    #4
  5. JohnJSal

    JohnJSal Guest

    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!
     
    JohnJSal, Nov 2, 2006
    #5
  6. JohnJSal

    JohnJSal Guest

    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?
     
    JohnJSal, Nov 2, 2006
    #6
  7. JohnJSal

    JohnJSal Guest

    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
     
    JohnJSal, Nov 2, 2006
    #7
  8. JohnJSal

    Rainy Guest

    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.
     
    Rainy, Nov 2, 2006
    #8
  9. Re: what's the difference between these two methods? (aka, whydoesn't one of them work?)

    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
     
    Carsten Haese, Nov 2, 2006
    #9
  10. JohnJSal

    JohnJSal Guest

    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.
     
    JohnJSal, Nov 2, 2006
    #10
  11. Re: what's the difference between these two methods? (aka, whydoesn't one of them work?)

    JohnJSal wrote:

    > 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>
     
    Fredrik Lundh, Nov 2, 2006
    #11
  12. JohnJSal

    John Salerno Guest

    Re: what's the difference between these two methods? (aka, whydoesn't one of them work?)

    Fredrik Lundh wrote:
    > JohnJSal wrote:
    >
    >> 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>
    >


    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.
     
    John Salerno, Nov 3, 2006
    #12
  13. JohnJSal

    Steve Holden Guest

    Re: what's the difference between these two methods? (aka, whydoesn't one of them work?)

    John Salerno wrote:
    > Fredrik Lundh wrote:
    >
    >>JohnJSal wrote:
    >>
    >>
    >>>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>
    >>

    >
    > 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
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Nov 3, 2006
    #13
  14. JohnJSal

    John Salerno Guest

    Re: what's the difference between these two methods? (aka, whydoesn't one of them work?)

    Steve Holden wrote:

    > 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". :)
     
    John Salerno, Nov 4, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rach
    Replies:
    2
    Views:
    454
    Andy Fish
    Jun 9, 2004
  2. Guybrush Threepwood
    Replies:
    7
    Views:
    544
    Nick Hounsome
    Feb 26, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,216
    Smokey Grindel
    Dec 2, 2006
  4. oo@@oo
    Replies:
    1
    Views:
    302
    Hallvard B Furuseth
    Aug 4, 2006
  5. Manoj
    Replies:
    4
    Views:
    471
    Tomasz Krol
    May 10, 2010
Loading...

Share This Page