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
- You don't need
;
at the end of the lines - The best way to iterate on
something
in Python is viafor element in something
(orfor element,index in enumerate(something)
if you need the index). - You can use the fact that the boolean evaluation of an empty container is false to write
if row
instead ofif 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
- You don't need
;
at the end of the lines - The best way to iterate on
something
in Python is viafor element in something
(orfor element,index in enumerate(something)
if you need the index). - You can use the fact that the boolean evaluation of an empty container is false to write
if row
instead ofif 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
- You don't need
;
at the end of the lines - The best way to iterate on
something
in Python is viafor element in something
(orfor element,index in enumerate(something)
if you need the index). - You can use the fact that the boolean evaluation of an empty container is false to write
if row
instead ofif 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
- You don't need
;
at the end of the lines - The best way to iterate on
something
in Python is viafor element in something
(orfor element,index in enumerate(something)
if you need the index). - You can use the fact that the boolean evaluation of an empty container is false to write
if row
instead ofif 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.