Help cleaning up some code

Martin P. Hellwig xng at xs4all.nl
Sat Mar 7 18:12:39 CET 2009


Martin P. Hellwig wrote:
<cut>
> def query_parser(QUERY, USER, STACK_SIZE):
>     indexes = ['ni','adid','rundateid','rundate','city','state','status']
>     empty = 'None'
> 
>     stack = []
>     query_result = self.con.execute(QUERY,(USER,STACK_SIZE)).fetchall()
>     ni = indexes[0]
> 
>     for row in query_result:
>         temp_dict = dict()
> 
>         if len(stack) > 0:
>             if stack[0][ni] != row[ni]:
>                 # Break if the current ni is not equal to the first
>                 break
> 
>         else:
>             for item in indexes:
>                 try:
>                     temp_dict[item] = row[item]
>                 except:
>                     temp_dict[item] = empty
> 
>                 stack.append(temp_dict)
> 
>     return(stack)
> 
> hth

Hold my horses :-), I made logical errors in the break, else and for 
item parts (thats what you get from rewriting instead of rethinking ;-) )

This should be more like it:

def query_parser(QUERY, USER, STACK_SIZE):
     indexes = ['ni','adid','rundateid','rundate','city','state','status']
     empty = 'None'

     stack = []
     query_result = self.con.execute(QUERY,(USER,STACK_SIZE)).fetchall()
     ni = indexes[0]

     for row in query_result:
         temp_dict = dict()
         ignore = False

         if len(stack) > 0:
             if stack[0][ni] != row[ni]:
                 ignore = True

         if ignore == False:
             for item in indexes:
                 try:
                     temp_dict[item] = row[item]
                 except:
                     temp_dict[item] = empty

             stack.append(temp_dict)

     return(stack)

Though it is likely I made another stupid mistake.

-- 
mph



More information about the Python-list mailing list