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)
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)

I would write it (untested) something like this:

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
--
mph
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to