Help cleaning up some code

oscar deits odeits at gmail.com
Sat Mar 7 06:52:30 CET 2009


Thanks a lot! that copy function is a really neat trick!

Yes the ni's are sorted, and yes the query SHOULD be the one to limit it to
just one set of ni's; however, i am having trouble implementing that in sql.
I am using sqlite3 atm and right now i have a very large select statment the
gets me what i want, i just dont know how to filter it again base on data
that is from the result itself. I THINK normally one would select into a
temporary table then filter it from there, but i am not sure.

On Fri, Mar 6, 2009 at 9:43 PM, andrew cooke <andrew at acooke.org> wrote:

> odeits wrote:
> > I am looking to clean up this code... any help is much appreciated.
> > Note: It works just fine, I just think it could be done cleaner.
> >
> > The result is a stack of dictionaries. the query returns up to
> > STACK_SIZE ads for a user. The check which i think is very ugly is
> > putting another contraint saying that all of the ni have to be the
> > same.
> >
> > stack = []
> > rows = self.con.execute(adquerystring,(user,STACK_SIZE)).fetchall()
> > for row in  rows:
> >             ad = dict()
> >             ad['ni'] = row['ni']
> >             ad['adid'] = row['adid']
> >             ad['rundateid'] = row['rundateid']
> >             ad['rundate'] = row['rundate']
> >             if row['city'] is None:
> >                 ad['city'] = 'None'
> >             else:
> >                 ad['city'] = row['city']
> >             if row['state'] is None:
> >                 ad['state'] = 'None'
> >             else:
> >                 ad['state'] = row['state']
> >             ad['status'] = row['status']
> >             try:
> >                 if stack[0]['ni'] != ad['ni']:
> >                     break;
> >             except IndexError:
> >                 pass
> >             stack.append(ad)
>
> NI = 'ni'
>
> def copy(src, dst, name, quote_none=False):
>  value = src[name]
>  dst[name] = 'None' if quote_none and value is None else value
>
> stack = []
> for row in self.con.execute(adquerystring,(user,STACK_SIZE)).fetchall():
>  ad = dict()
>  for name in (NI, 'adid', 'rundateid', 'rundate', 'status'):
>    copy(row, ad, name)
>  for name in ('city', 'state'):
>    copy(row, ad, name, quote_none=True)
>  if stack and stack[0][NI] != ad[NI]:
>    break
>  stack.append(ad)
>
> but couldn't you change the query so that it only returns a single 'ni'?
>
> also, do you really want to break, or just skip the append?  maybe you
> want:
>
>  if not stack or stack[0]['ni'] == ad['ni']:
>    stack.append(ad)
>
> or perhaps the 'ni' are sorted (in which case what you are doing makes
> some sense).
>
> andrew
>
>
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090306/2da3377d/attachment.html>


More information about the Python-list mailing list