Skip to content

Commit

Permalink
For loops to list comprehension
Browse files Browse the repository at this point in the history
  • Loading branch information
dericke committed Jan 24, 2021
1 parent a84f2a2 commit 4b50c1a
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 64 deletions.
20 changes: 10 additions & 10 deletions python/plugins/db_manager/db_plugins/connector.py
Expand Up @@ -188,11 +188,11 @@ def _get_cursor_columns(self, c):
@classmethod
def quoteId(self, identifier):
if hasattr(identifier, '__iter__') and not isinstance(identifier, str):
ids = list()
for i in identifier:
if i is None or i == "":
continue
ids.append(self.quoteId(i))
ids = [
self.quoteId(i)
for i in identifier
if i is not None and i != ""
]
return u'.'.join(ids)

identifier = str(
Expand All @@ -203,11 +203,11 @@ def quoteId(self, identifier):
def quoteString(self, txt):
""" make the string safe - replace ' with '' """
if hasattr(txt, '__iter__') and not isinstance(txt, str):
txts = list()
for i in txt:
if i is None:
continue
txts.append(self.quoteString(i))
txts = [
self.quoteString(i)
for i in txt
if i is not None
]
return u'.'.join(txts)

txt = str(txt) if txt is not None else str() # make sure it's python unicode string
Expand Down
24 changes: 12 additions & 12 deletions python/plugins/db_manager/db_plugins/data_model.py
Expand Up @@ -305,10 +305,10 @@ def getObject(self, row):
return fld

def getFields(self):
flds = []
for fld in self.getObjectIter():
flds.append(fld)
return flds
return [
fld
for fld in self.getObjectIter()
]


class TableConstraintsModel(SimpleTableModel):
Expand Down Expand Up @@ -342,10 +342,10 @@ def getObject(self, row):
return constr

def getConstraints(self):
constrs = []
for constr in self.getObjectIter():
constrs.append(constr)
return constrs
return [
constr
for constr in self.getObjectIter()
]


class TableIndexesModel(SimpleTableModel):
Expand Down Expand Up @@ -376,7 +376,7 @@ def getObject(self, row):
return idx

def getIndexes(self):
idxs = []
for idx in self.getObjectIter():
idxs.append(idx)
return idxs
return [
idx
for idx in self.getObjectIter()
]
7 changes: 4 additions & 3 deletions python/plugins/db_manager/db_plugins/gpkg/connector.py
Expand Up @@ -401,9 +401,10 @@ def getTableIndexes(self, table):
sql = u"PRAGMA index_info(%s)" % (self.quoteId(name))

idx = [num, name, unique]
cols = []
for seq, cid, cname in self._fetchAll(sql):
cols.append(cid)
cols = [
cid
for seq, cid, cname in self._fetchAll(sql)
]
idx.append(cols)
indexes[i] = idx

Expand Down
9 changes: 4 additions & 5 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -571,11 +571,10 @@ def rasterTablesFactory(self, row, db, schema=None):
def tables(self, schema=None, sys_tables=False):
tables = self.connector.getTables(schema.name if schema else None, sys_tables)
if tables is not None:
ret = []
for t in tables:
table = self.tablesFactory(t, self, schema)
ret.append(table)

ret = [
self.tablesFactory(t, self, schema)
for t in tables
]
return ret

def createTable(self, table, fields, schema=None):
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/db_manager/db_plugins/postgis/connector.py
Expand Up @@ -1240,13 +1240,14 @@ def getSqlDictionary(self):
sql_dict = getSqlDictionary()

# get schemas, tables and field names
items = []
sql = u"""SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_' AND nspname != 'information_schema'
UNION SELECT relname FROM pg_class WHERE relkind IN ('v', 'r', 'm', 'p')
UNION SELECT attname FROM pg_attribute WHERE attnum > 0"""
c = self._execute(None, sql)
for row in self._fetchall(c):
items.append(row[0])
items = [
row[0]
for row in self._fetchall(c)
]
self._close_cursor(c)

sql_dict["identifier"] = items
Expand Down
Expand Up @@ -191,7 +191,7 @@ def sql_currentView(self):
def sql_functions(self):
cols = ",".join(self.columns)
all_cols = self.colPkey + "," + ",".join(self.columns)
old_cols = ",".join([u"OLD." + x for x in self.columns])
old_cols = ",".join(u"OLD." + x for x in self.columns)

sql = u"""
CREATE OR REPLACE FUNCTION %(func_at_time)s(timestamp)
Expand Down Expand Up @@ -253,8 +253,8 @@ def sql_triggers(self):
def sql_updatesView(self):
cols = ",".join(self.columns)
return_cols = self.colPkey + "," + ",".join(self.columns)
new_cols = ",".join([u"NEW." + x for x in self.columns])
assign_cols = ",".join([u"%s = NEW.%s" % (x, x) for x in self.columns])
new_cols = ",".join(u"NEW." + x for x in self.columns)
assign_cols = ",".join(u"%s = NEW.%s" % (x, x) for x in self.columns)

return u"""
CREATE OR REPLACE RULE "_DELETE" AS ON DELETE TO %(view)s DO INSTEAD
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/db_manager/db_plugins/spatialite/connector.py
Expand Up @@ -343,9 +343,10 @@ def getTableIndexes(self, table):
self._execute(c, sql)

idx = [num, name, unique]
cols = []
for seq, cid, cname in c.fetchall():
cols.append(cid)
cols = [
cid
for seq, cid, cname in c.fetchall()
]
idx.append(cols)
indexes[i] = idx

Expand Down
20 changes: 11 additions & 9 deletions python/plugins/db_manager/dlg_query_builder.py
Expand Up @@ -349,15 +349,17 @@ def validate(self):
return
self.query = query

saveParameter = {}
saveParameter["coltables"] = self.coltables
saveParameter["col_col"] = self.col_col
saveParameter["col_where"] = self.col_where
saveParameter["col"] = query_col
saveParameter["tab"] = query_table
saveParameter["where"] = query_where
saveParameter["group"] = query_group
saveParameter["order"] = query_order
saveParameter = {
"coltables": self.coltables,
"col_col": self.col_col,
"col_where": self.col_where,
"col": query_col,
"tab": query_table,
"where": query_where,
"group": query_group,
"order": query_order,
}

QueryBuilderDlg.saveParameter = saveParameter

def restoreLastQuery(self):
Expand Down
10 changes: 6 additions & 4 deletions python/plugins/db_manager/dlg_sql_layer_window.py
Expand Up @@ -538,10 +538,12 @@ def uniqueChanged(self):

def uniqueTextChanged(self, text):
# Whenever there is new text displayed in the combobox, check if it is the correct one and if not, display the correct one.
checkedItems = []
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard):
if item.checkState() == Qt.Checked:
checkedItems.append(item.text())
checkedItems = [
item.text()
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard)
if item.checkState() == Qt.Checked
]

label = ", ".join(checkedItems)
if text != label:
self.uniqueCombo.setEditText(label)
Expand Down
21 changes: 9 additions & 12 deletions python/plugins/db_manager/dlg_sql_window.py
Expand Up @@ -80,13 +80,11 @@ def check_comments_in_sql(raw_sql_input):
for line in raw_sql_input.splitlines():
if not line.strip().startswith('--'):
if '--' in line:
comment_positions = []
comments = re.finditer(r'--', line)
for match in comments:
comment_positions.append(match.start())
quote_positions = []
comment_positions = [match.start() for match in comments]
identifiers = re.finditer(r'"(?:[^"]|"")*"', line)
quotes = re.finditer(r"'(?:[^']|'')*'", line)
quote_positions = []
for match in identifiers:
quote_positions.append((match.start(), match.end()))
for match in quotes:
Expand Down Expand Up @@ -390,14 +388,11 @@ def executeSqlCompleted(self):
with OverrideCursor(Qt.WaitCursor):
if self.modelAsync.task.status() == QgsTask.Complete:
model = self.modelAsync.model
quotedCols = []

self.showError(None)
self.viewResult.setModel(model)
self.lblResult.setText(self.tr("{0} rows, {1:.3f} seconds").format(model.affectedRows(), model.secs()))
cols = self.viewResult.model().columnNames()
for col in cols:
quotedCols.append(self.db.connector.quoteId(col))
quotedCols = [self.db.connector.quoteId(col) for col in cols]

self.setColumnCombos(cols, quotedCols)

Expand Down Expand Up @@ -681,10 +676,12 @@ def uniqueChanged(self):

def uniqueTextChanged(self, text):
# Whenever there is new text displayed in the combobox, check if it is the correct one and if not, display the correct one.
checkedItems = []
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard):
if item.checkState() == Qt.Checked:
checkedItems.append(item.text())
checkedItems = [
item.text()
for item in self.uniqueModel.findItems("*", Qt.MatchWildcard)
if item.checkState() == Qt.Checked
]

label = ", ".join(checkedItems)
if text != label:
self.uniqueCombo.setEditText(label)
Expand Down

0 comments on commit 4b50c1a

Please sign in to comment.