creating new objects with references to them
It seems like what I want to do is something that programmers deal with everyday, but I just can't think of a way to do it. Basically, I am writing a DB front-end and I want a new "Researcher" object to be created whenever the user presses the "New Record" button. He can open as many new records at a time as he wants and fill in the information. What I don't know how to do is handle this arbitrary number of objects. When it comes time to save the data to the DB, how do I access each object (to get the text fields associated with each)? Won't I have to have given each instance some name? Or is there some other way to do this? Simply creating a new instance each time the user presses "New Record" won't be good enough, because how do I retain a reference to that instance over the course of the program, even after a second or third instance has been created? Hope that makes sense. It seems like such a common task. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: creating new objects with references to them
Ant wrote: > It all depends on what UI you are using (Web frontend? GUI such as > Tkinter?) and what your use case is. Making it myself with wxPython. > > What is it exactly that you want to do? Create a bunch of Researcher > objects and then save them in a single hit? Create a list of > Researchers and then choose which to save to the db? Populate a list of > researchers from the db, and have the option of editing or adding new > ones? Should the new Researcher objects be committed to the db as soon > as they are saved? There are two options: save current record and save all records, both of which put the info in the DB immediately. > Anyway, a simple list of Researchers should suffice for any of these > purposes, and assuming you want to commit them all in one hit, you have > a list of objects ready to iterate over. Ok, so in making a list does this mean that I won't have a name for each instance? I just have to iterate over the list when I need them? I'm not sure how I would do this if the user chooses just to save a single record and not all of them. -- http://mail.python.org/mailman/listinfo/python-list
Re: creating new objects with references to them
Steve Holden wrote: > del rec[7] Hmmm, but what if the record can remain open, changes can be made, and then saved again to the same object? I suppose it isn't necessary to delete them, right? Man, this stuff gets complicated -- http://mail.python.org/mailman/listinfo/python-list
Re: creating new objects with references to them
JohnJSal wrote: > Hope that makes sense. It seems like such a common task. Ok, I'm thinking about the suggestion to make a list, but I'm still confused. Even with a list, how do I access each instance. Would I have to do it by index? I assume I'd do something like this: self.records = []# list for storing instances Then when "New Record" is clicked: def OnNewRecord(self, event): self.records.append(Researchers()) But now what? There is still no reference to that instance? The only thing I can think of is that each record will be a tab in a wxPython notebook control, and each tab can be referenced by index. So whichever tab I'm on, I can use that index to get that particular instance in the list. But this solution seems too tied to my particular implementation. How would you normally access these instances in the list? Let's say there are three objects in the list. Someone fills out the text fields for the second one and clicks "Save". I know how to read all the information in the fields, but how do I then associate it with that second instance? (Or I wonder if I really even need to, since upon saving, the information gets stored in the database immediately. Maybe I don't even need a Researcher class at all.) -- http://mail.python.org/mailman/listinfo/python-list
what's the difference between these two methods? (aka, why doesn't one of them work?)
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' -- http://mail.python.org/mailman/listinfo/python-list
Re: what's the difference between these two methods? (aka, why doesn't one of them work?)
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: what's the difference between these two methods? (aka, why doesn't one of them work?)
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! -- http://mail.python.org/mailman/listinfo/python-list
Re: what's the difference between these two methods? (aka, why doesn't one of them work?)
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? -- http://mail.python.org/mailman/listinfo/python-list
Re: what's the difference between these two methods? (aka, why doesn't one of them work?)
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: what's the difference between these two methods? (aka, why doesn't one of them work?)
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. -- http://mail.python.org/mailman/listinfo/python-list