Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17483

Re: Newbie Question: Obtain element from list of tuples

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'value,': 0.04; 'pos': 0.09; 'tuple': 0.09; 'am,': 0.12; 'things.': 0.12; 'library': 0.13; 'received:209.85.210.174': 0.13; 'received:mail- iy0-f174.google.com': 0.13; 'skip:f 30': 0.13; 'columns': 0.16; 'constants:': 0.16; 'dictionaries': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'row': 0.16; 'selects': 0.16; 'subject:Newbie': 0.16; 'mon,': 0.16; 'wrote:': 0.18; 'trying': 0.21; 'subject:list': 0.21; 'subject:Question': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'statement': 0.23; 'interface': 0.23; 'code': 0.25; 'column': 0.28; 'message- id:@mail.gmail.com': 0.28; 'fine.': 0.29; 'looks': 0.29; 'button,': 0.30; 'tuples': 0.30; 'list': 0.32; 'changes': 0.32; 'break': 0.32; 'objects': 0.32; 'instead': 0.33; 'that,': 0.33; 'match': 0.34; 'to:addr:python-list': 0.34; 'it.': 0.34; 'all.': 0.34; 'especially': 0.35; 'solutions.': 0.37; 'two': 0.37; 'but': 0.37; 'received:google.com': 0.37; "there's": 0.37; 'could': 0.37; 'received:209.85': 0.38; 'subject:from': 0.38; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'might': 0.40; 'more': 0.61; '2011': 0.61; 'your': 0.61; 'happen': 0.61; 'efficient': 0.62; '19,': 0.68; 'carefully': 0.68; 'concept': 0.74; 'problematic': 0.84; 'suspected': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=87ig49u6CKGAZnSQU2Xto7juOpQj4oXkL/mKscFNB/Q=; b=iiCqt9W6uQiqMljVvXMObxZPmRQQKMNALnQGvw7P11mV8CYYmIo5jQC10o57CYuhLo CI49hHe3OtBJPlKYNoqY5IS7wLECN8omSRvG6ZNmr+gaTVi/pP95ym0Jm6raoEEQpUDB A+KwATKdGOUo//hZGGULznQ33LGmoB1IulQW0=
MIME-Version 1.0
In-Reply-To <jclr1a$kjn$2@news.albasani.net>
References <jclflh$u45$1@news.albasani.net> <mailman.3807.1324241477.27778.python-list@python.org> <jclr1a$kjn$2@news.albasani.net>
Date Mon, 19 Dec 2011 10:40:06 +1100
Subject Re: Newbie Question: Obtain element from list of tuples
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3808.1324251610.27778.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1324251610 news.xs4all.nl 6970 [2001:888:2000:d::a6]:45886
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17483

Show key headers only | View raw


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.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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