Thanks a lot! that copy function is a really neat trick!<br><br>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.<br>
<br><div class="gmail_quote">On Fri, Mar 6, 2009 at 9:43 PM, andrew cooke <span dir="ltr"><<a href="mailto:andrew@acooke.org">andrew@acooke.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
odeits wrote:<br>
> I am looking to clean up this code... any help is much appreciated.<br>
> Note: It works just fine, I just think it could be done cleaner.<br>
><br>
> The result is a stack of dictionaries. the query returns up to<br>
> STACK_SIZE ads for a user. The check which i think is very ugly is<br>
> putting another contraint saying that all of the ni have to be the<br>
> same.<br>
><br>
> stack = []<br>
> rows = self.con.execute(adquerystring,(user,STACK_SIZE)).fetchall()<br>
> for row in  rows:<br>
>             ad = dict()<br>
>             ad['ni'] = row['ni']<br>
>             ad['adid'] = row['adid']<br>
>             ad['rundateid'] = row['rundateid']<br>
>             ad['rundate'] = row['rundate']<br>
>             if row['city'] is None:<br>
>                 ad['city'] = 'None'<br>
>             else:<br>
>                 ad['city'] = row['city']<br>
>             if row['state'] is None:<br>
>                 ad['state'] = 'None'<br>
>             else:<br>
>                 ad['state'] = row['state']<br>
>             ad['status'] = row['status']<br>
>             try:<br>
>                 if stack[0]['ni'] != ad['ni']:<br>
>                     break;<br>
>             except IndexError:<br>
>                 pass<br>
>             stack.append(ad)<br>
<br>
NI = 'ni'<br>
<br>
def copy(src, dst, name, quote_none=False):<br>
  value = src[name]<br>
  dst[name] = 'None' if quote_none and value is None else value<br>
<br>
stack = []<br>
for row in self.con.execute(adquerystring,(user,STACK_SIZE)).fetchall():<br>
  ad = dict()<br>
  for name in (NI, 'adid', 'rundateid', 'rundate', 'status'):<br>
    copy(row, ad, name)<br>
  for name in ('city', 'state'):<br>
    copy(row, ad, name, quote_none=True)<br>
  if stack and stack[0][NI] != ad[NI]:<br>
    break<br>
  stack.append(ad)<br>
<br>
but couldn't you change the query so that it only returns a single 'ni'?<br>
<br>
also, do you really want to break, or just skip the append?  maybe you want:<br>
<br>
  if not stack or stack[0]['ni'] == ad['ni']:<br>
    stack.append(ad)<br>
<br>
or perhaps the 'ni' are sorted (in which case what you are doing makes<br>
some sense).<br>
<br>
andrew<br>
<br>
<br>
> --<br>
> <a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
><br>
><br>
<br>
<br>
</blockquote></div><br>