Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17534
| From | HoneyMonster <someone@someplace.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Newbie Question: Obtain element from list of tuples |
| Date | 2011-12-20 00:59 +0000 |
| Organization | albasani.net |
| Message-ID | <jcomlk$6m9$1@news.albasani.net> (permalink) |
| References | <jclflh$u45$1@news.albasani.net> <mailman.3807.1324241477.27778.python-list@python.org> <jclr1a$kjn$2@news.albasani.net> <mailman.3808.1324251610.27778.python-list@python.org> |
On Mon, 19 Dec 2011 10:40:06 +1100, Chris Angelico wrote:
> On Mon, Dec 19, 2011 at 9:55 AM, HoneyMonster
> <someone@someplace.invalid> wrote:
>> When the user selects a row and clicks a button, I am using:
>> pos = self.grid_1.GetGridCursorRow() to establish which tuple in recs
>> is involved, and then pid = recs[pos][4] to determine the key value (I
>> suspected that accessing recs directly would be more efficient that
>> trying to determine the wxGrid column value, and in any case the pid is
>> in recs but not in the wxGrid).
>
> Yep, this looks like a sensible way to do it!
>
> I would recommend, though, that you avoid "magic numbers" - the process
> ID might happen to be in cell 4, but what if your column list changes?
> This is especially problematic when you use "select * from table",
> because changes outside your code can break things.
>
> There's two solutions. One is to carefully match your SELECT statement
> to a set of constants:
>
> exec("SELECT FOO,BAR,QUUX,ASDF,PID,WHATEVER FROM table")
> FOO,BAR,QUUX,ASDF,PID,WHATEVER,*_=range(20) # Cool trick to avoid having
> to count the columns
>
> But a better way is to let the database handle it. Check your interface
> library to see if you can have it return dictionaries or objects instead
> of tuples - then you could use:
>
> pid = recs[pos]["pid"]
> # or pid = recs[pos]->pid
>
> which avoids the need to count columns at all.
>
> Other than that, though, yep - your code concept looks fine.
Thanks again, Chris. I'm completely new to Python, but am an old had at
databases. I *never* use "select *" in a production environment; rather
an explicit list of columns. Thus it doesn't matter if a column is added
to the table, nor if the column order in the table is changed.
But I have used this at the start if my code to make it more readable
anyway:
# Define constants (position in tuple)
Title = 0
Episode = 1
Categories = 2
Channel = 3
PID = 4
Index = 5
so that in the bit where I populate the grid, the code is now:
cnt = 0
for tup in recs:
self.grid_1.SetCellValue(cnt, Title, tup[Title])
self.grid_1.SetCellValue(cnt, Episode, tup[Episode])
self.grid_1.SetCellValue(cnt, Categories, tup[Categories])
self.grid_1.SetCellValue(cnt, Channel, tup[Channel])
cnt = cnt + 1
just so that is more legible.
Cheers,
HM
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Newbie Question: Obtain element from list of tuples HoneyMonster <someone@someplace.invalid> - 2011-12-18 19:41 +0000
Re: Newbie Question: Obtain element from list of tuples Arnaud Delobelle <arnodel@gmail.com> - 2011-12-18 19:49 +0000
Re: Newbie Question: Obtain element from list of tuples Roy Smith <roy@panix.com> - 2011-12-18 15:04 -0500
Re: Newbie Question: Obtain element from list of tuples HoneyMonster <someone@someplace.invalid> - 2011-12-18 20:22 +0000
Re: Newbie Question: Obtain element from list of tuples Roy Smith <roy@panix.com> - 2011-12-18 15:25 -0500
Re: Newbie Question: Obtain element from list of tuples alex23 <wuwei23@gmail.com> - 2011-12-18 18:35 -0800
Re: Newbie Question: Obtain element from list of tuples Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-19 03:00 +0000
Re: Newbie Question: Obtain element from list of tuples "Frank Millman" <frank@chagford.com> - 2011-12-19 08:46 +0200
Re: Newbie Question: Obtain element from list of tuples DevPlayer <devplayer@gmail.com> - 2011-12-19 07:37 -0800
Re: Newbie Question: Obtain element from list of tuples alex23 <wuwei23@gmail.com> - 2011-12-19 19:53 -0800
Re: Newbie Question: Obtain element from list of tuples Chris Angelico <rosuav@gmail.com> - 2011-12-19 07:51 +1100
Re: Newbie Question: Obtain element from list of tuples Roy Smith <roy@panix.com> - 2011-12-18 16:10 -0500
Re: Newbie Question: Obtain element from list of tuples HoneyMonster <someone@someplace.invalid> - 2011-12-18 22:55 +0000
Re: Newbie Question: Obtain element from list of tuples Chris Angelico <rosuav@gmail.com> - 2011-12-19 10:40 +1100
Re: Newbie Question: Obtain element from list of tuples HoneyMonster <someone@someplace.invalid> - 2011-12-20 00:59 +0000
csiph-web