Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

There's quite a lot of code so I'm going to comment only this small piece of code :

results = cur.fetchall();
n = len(results);
cols = self.table_columns(None, cur);
retval = [];
for i in range(0,n):
 aux = results[i];
 row = {};
 for j in range(0,len(cols)):
 row[cols[j]] = aux[j];
 if len(row):
 retval.append(row);
return retval
  1. You don't need ; at the end of the lines
  2. The best way to iterate on something in Python is via for element in something (or for element,index in enumerate(something) if you need the index).
  3. You can use the fact that the boolean evaluation of an empty container is false to write if row instead of if len(row).

Your code is now :

results = cur.fetchall();
cols = self.table_columns(None, cur);
retval = [];
for res in results:
 row = {};
 for col,j in enumerate(cols):
 row[col] = res[j];
 if row:
 retval.append(row);
return retval

Now, you can create your row dictionnary direclty from res and cols (other solutions can be found on SO on SO):

results = cur.fetchall();
cols = self.table_columns(None, cur);
retval = [];
for res in results:
 row = dict(zip(cols,res))
 if row:
 retval.append(row);
return retval

Now, there would be a way to make the whole thing a single expression using list comprehension but I am not quite sure this is a good idea.

There's quite a lot of code so I'm going to comment only this small piece of code :

results = cur.fetchall();
n = len(results);
cols = self.table_columns(None, cur);
retval = [];
for i in range(0,n):
 aux = results[i];
 row = {};
 for j in range(0,len(cols)):
 row[cols[j]] = aux[j];
 if len(row):
 retval.append(row);
return retval
  1. You don't need ; at the end of the lines
  2. The best way to iterate on something in Python is via for element in something (or for element,index in enumerate(something) if you need the index).
  3. You can use the fact that the boolean evaluation of an empty container is false to write if row instead of if len(row).

Your code is now :

results = cur.fetchall();
cols = self.table_columns(None, cur);
retval = [];
for res in results:
 row = {};
 for col,j in enumerate(cols):
 row[col] = res[j];
 if row:
 retval.append(row);
return retval

Now, you can create your row dictionnary direclty from res and cols (other solutions can be found on SO):

results = cur.fetchall();
cols = self.table_columns(None, cur);
retval = [];
for res in results:
 row = dict(zip(cols,res))
 if row:
 retval.append(row);
return retval

Now, there would be a way to make the whole thing a single expression using list comprehension but I am not quite sure this is a good idea.

There's quite a lot of code so I'm going to comment only this small piece of code :

results = cur.fetchall();
n = len(results);
cols = self.table_columns(None, cur);
retval = [];
for i in range(0,n):
 aux = results[i];
 row = {};
 for j in range(0,len(cols)):
 row[cols[j]] = aux[j];
 if len(row):
 retval.append(row);
return retval
  1. You don't need ; at the end of the lines
  2. The best way to iterate on something in Python is via for element in something (or for element,index in enumerate(something) if you need the index).
  3. You can use the fact that the boolean evaluation of an empty container is false to write if row instead of if len(row).

Your code is now :

results = cur.fetchall();
cols = self.table_columns(None, cur);
retval = [];
for res in results:
 row = {};
 for col,j in enumerate(cols):
 row[col] = res[j];
 if row:
 retval.append(row);
return retval

Now, you can create your row dictionnary direclty from res and cols (other solutions can be found on SO):

results = cur.fetchall();
cols = self.table_columns(None, cur);
retval = [];
for res in results:
 row = dict(zip(cols,res))
 if row:
 retval.append(row);
return retval

Now, there would be a way to make the whole thing a single expression using list comprehension but I am not quite sure this is a good idea.

Source Link
SylvainD
  • 29.7k
  • 1
  • 49
  • 93

There's quite a lot of code so I'm going to comment only this small piece of code :

results = cur.fetchall();
n = len(results);
cols = self.table_columns(None, cur);
retval = [];
for i in range(0,n):
 aux = results[i];
 row = {};
 for j in range(0,len(cols)):
 row[cols[j]] = aux[j];
 if len(row):
 retval.append(row);
return retval
  1. You don't need ; at the end of the lines
  2. The best way to iterate on something in Python is via for element in something (or for element,index in enumerate(something) if you need the index).
  3. You can use the fact that the boolean evaluation of an empty container is false to write if row instead of if len(row).

Your code is now :

results = cur.fetchall();
cols = self.table_columns(None, cur);
retval = [];
for res in results:
 row = {};
 for col,j in enumerate(cols):
 row[col] = res[j];
 if row:
 retval.append(row);
return retval

Now, you can create your row dictionnary direclty from res and cols (other solutions can be found on SO):

results = cur.fetchall();
cols = self.table_columns(None, cur);
retval = [];
for res in results:
 row = dict(zip(cols,res))
 if row:
 retval.append(row);
return retval

Now, there would be a way to make the whole thing a single expression using list comprehension but I am not quite sure this is a good idea.

lang-py

AltStyle によって変換されたページ (->オリジナル) /