[PATCH 08 of 10 logilab-database] Please the flake8 god

Laurent Peuch cortex at worlddomination.be
Fri Apr 3 09:14:16 CEST 2020


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1585790696 -7200
#      Thu Apr 02 03:24:56 2020 +0200
# Node ID 57b15060a8e1a2decd8b74bc18ba86125efa5e52
# Parent  bc8c31fb228fd8f6543342baf5b05341eb37c5b4
# Available At https://hg.logilab.org/users/lpeuch/logilab-database
#              hg pull https://hg.logilab.org/users/lpeuch/logilab-database -r 57b15060a8e1
# EXP-Topic good-practice-modernisation
Please the flake8 god

diff --git a/logilab/database/__init__.py b/logilab/database/__init__.py
--- a/logilab/database/__init__.py
+++ b/logilab/database/__init__.py
@@ -38,16 +38,17 @@ from __future__ import with_statement
 
 __docformat__ = "restructuredtext en"
 
-import sys
 import threading
 import logging
-from datetime import datetime, time, date
+from datetime import datetime, date
 from warnings import warn
 
 from logilab.common.modutils import load_module_from_name
 from logilab.common.date import todate, todatetime, utcdatetime, utctime
 from logilab.common.deprecation import deprecated
 
+from logilab.database.fti import FTIndexerMixIn
+
 _LOGGER = logging.getLogger("logilab.database")
 
 USE_MX_DATETIME = False
@@ -240,7 +241,7 @@ def _import_driver_module(driver, driver
     :returns: the tuple module_object, module_name where module_object
               is the dbapi module, and modname the module's name
     """
-    if not driver in drivers:
+    if driver not in drivers:
         raise UnknownDriver(driver)
     with _LOAD_MODULES_LOCK:
         for modname in drivers[driver]:
@@ -258,7 +259,7 @@ def _import_driver_module(driver, driver
     return module, modname
 
 
-## base connection and cursor wrappers #####################
+# base connection and cursor wrappers #####################
 
 
 class _SimpleConnectionWrapper(object):
@@ -339,7 +340,7 @@ class PyCursor(object):
         return getattr(self._cursor, attrname)
 
 
-## abstract class for dbapi adapters #######################
+# abstract class for dbapi adapters #######################
 
 
 class DBAPIAdapter(object):
@@ -483,8 +484,6 @@ class DBAPIAdapter(object):
 
 # advanced database helper #####################################################
 
-from logilab.database.fti import FTIndexerMixIn
-
 
 class BadQuery(Exception):
     pass
diff --git a/logilab/database/fti.py b/logilab/database/fti.py
--- a/logilab/database/fti.py
+++ b/logilab/database/fti.py
@@ -125,7 +125,7 @@ CREATE INDEX appears_word_id ON appears 
         try:
             self.cursor_index_object(uid, obj, cursor)
             cnx.commit()
-        except:
+        except Exception:
             cnx.rollback()
             raise
 
@@ -139,7 +139,7 @@ CREATE INDEX appears_word_id ON appears 
         try:
             self.cursor_unindex_object(uid, cursor)
             cnx.commit()
-        except:
+        except Exception:
             cnx.rollback()
             raise
 
@@ -153,7 +153,7 @@ CREATE INDEX appears_word_id ON appears 
         try:
             self.cursor_reindex_object(uid, obj, cursor)
             cnx.commit()
-        except:
+        except Exception:
             cnx.rollback()
             raise
 
@@ -187,7 +187,7 @@ CREATE INDEX appears_word_id ON appears 
                 VALUES (%(uid)s,%(word)s);""",
                     {"uid": wid, "word": word},
                 )
-            except:
+            except Exception:
                 # Race condition occured.
                 # someone inserted the word before we did.
                 # Never mind, let's use the new entry...
diff --git a/logilab/database/ftiparser.py b/logilab/database/ftiparser.py
--- a/logilab/database/ftiparser.py
+++ b/logilab/database/ftiparser.py
@@ -23,7 +23,8 @@ from __future__ import print_function
 __docformat__ = "restructuredtext en"
 
 # Begin -- grammar generated by Yapps
-import sys, re
+import sys
+import re
 from yapps import runtime
 
 
@@ -48,7 +49,7 @@ class IndexerQuery(runtime.Parser):
     def goal(self, Q, _parent=None):
         _context = self.Context(_parent, self._scanner, "goal", [Q])
         while self._peek(context=_context) != "'$'":
-            all = self.all(Q, _context)
+            self.all(Q, _context)
         self._scan("'$'", context=_context)
 
     def all(self, Q, _parent=None):
diff --git a/logilab/database/ftiquery.py b/logilab/database/ftiquery.py
--- a/logilab/database/ftiquery.py
+++ b/logilab/database/ftiquery.py
@@ -79,7 +79,7 @@ class Query(object):
             for uid, rating in results.items():
                 try:
                     _results[uid] += rating
-                except:
+                except Exception:
                     continue
             results = _results
 
diff --git a/logilab/database/postgres.py b/logilab/database/postgres.py
--- a/logilab/database/postgres.py
+++ b/logilab/database/postgres.py
@@ -190,7 +190,7 @@ class _Psycopg2CffiAdapter(_Psycopg2Adap
 
 
 db._PREFERED_DRIVERS["postgres"] = [
-    #'logilab.database._pyodbcwrap',
+    # 'logilab.database._pyodbcwrap',
     "psycopg2",
     "psycopg2ct",
     "psycopg2cffi",
diff --git a/logilab/database/sqlgen.py b/logilab/database/sqlgen.py
--- a/logilab/database/sqlgen.py
+++ b/logilab/database/sqlgen.py
@@ -182,7 +182,7 @@ class SQLGenerator:
         """
         table_names = ["%s AS %s" % (k, v) for k, v in tables]
         sql = "SELECT %s FROM %s" % (", ".join(model), ", ".join(table_names))
-        if joins and type(joins) != type(""):
+        if joins and not isinstance(joins, str):
             joins = " AND ".join(joins)
         where = self.where(params, joins)
         if where:
diff --git a/logilab/database/sqlite.py b/logilab/database/sqlite.py
--- a/logilab/database/sqlite.py
+++ b/logilab/database/sqlite.py
@@ -119,7 +119,7 @@ class _Sqlite3Adapter(db.DBAPIAdapter):
             def convert_mxtime(ustr):
                 try:
                     return strptime(ustr, "%H:%M:%S")
-                except:
+                except Exception:
                     # DateTime used as Time?
                     return strptime(ustr, "%Y-%m-%d %H:%M:%S")
 
@@ -138,6 +138,7 @@ class _Sqlite3Adapter(db.DBAPIAdapter):
                 return time(*[int(i) for i in data.split(":")])
 
             sqlite.register_converter("time", convert_time)
+
             # datetime.timedelta
             def adapt_timedelta(data):
                 """the sign in the result only refers to the number of days.  day
@@ -343,7 +344,7 @@ def init_sqlite_connexion(cnx):
             date = date.split(".")[0]  # remove microseconds
             try:
                 date = strptime(date, "%Y-%m-%d %H:%M:%S")
-            except:
+            except Exception:
                 date = strptime(date, "%Y-%m-%d")
         return date
 
diff --git a/logilab/database/sqlserver2000.py b/logilab/database/sqlserver2000.py
--- a/logilab/database/sqlserver2000.py
+++ b/logilab/database/sqlserver2000.py
@@ -17,7 +17,7 @@
 # with logilab-database. If not, see <http://www.gnu.org/licenses/>.
 """Sqlserver 2000 RDBMS support
 
-Mostly untested, use at your own risks. 
+Mostly untested, use at your own risks.
 
 Supported drivers, in order of preference:
 - pyodbc (recommended, others are not well tested)
diff --git a/logilab/database/sqlserver2005.py b/logilab/database/sqlserver2005.py
--- a/logilab/database/sqlserver2005.py
+++ b/logilab/database/sqlserver2005.py
@@ -25,11 +25,14 @@ Supported drivers, in order of preferenc
 import os
 import sys
 import shutil
+import logging
 from warnings import warn
 
 from logilab import database as db
 from logilab.database.sqlserver import _PyodbcAdapter, _AdodbapiAdapter
 
+_LOGGER = logging.getLogger("logilab.database")
+
 
 class _PyodbcSqlServer2005Adapter(_PyodbcAdapter):
     driver = "SQL Server Native Client 10.0"
@@ -189,7 +192,8 @@ AND j.column_id = k.column_id;"""
         """
         modify the sql statement to add LIMIT and OFFSET clauses
         (or to emulate them if the backend does not support these SQL extensions)
-        reference: http://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server
+        reference:
+        http://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server
         """
         if limit is None and not offset:
             return sql
@@ -409,8 +413,8 @@ AND j.column_id = k.column_id;"""
             time.sleep(1)
             try:
                 size = os.path.getsize(file_share_filename)
-            except OSError as exc:
-                self.logger.exception("error accessing %s", file_share_filename)
+            except OSError:
+                _LOGGER.exception("error accessing %s", file_share_filename)
                 err_count += 1
             if size > prev_size:
                 same_size_count = 0
@@ -458,7 +462,7 @@ AND j.column_id = k.column_id;"""
                     extra_args="trusted_connection",
                 )
                 break
-            except:
+            except Exception:
                 sleeptime = min(sleeptime * 2, 300)
         os.remove(file_share_filename)
         sys.exit(0)
diff --git a/logilab/database/sqlserver2008.py b/logilab/database/sqlserver2008.py
--- a/logilab/database/sqlserver2008.py
+++ b/logilab/database/sqlserver2008.py
@@ -17,7 +17,7 @@
 # with logilab-database. If not, see <http://www.gnu.org/licenses/>.
 """Sqlserver 2008 RDBMS support
 
-Mostly untested, use at your own risks. 
+Mostly untested, use at your own risks.
 
 Supported drivers, in order of preference:
 - pyodbc (recommended, others are not well tested)
diff --git a/test/unittest_converters.py b/test/unittest_converters.py
--- a/test/unittest_converters.py
+++ b/test/unittest_converters.py
@@ -16,7 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with logilab-database. If not, see <http://www.gnu.org/licenses/>.
 import unittest
-from datetime import datetime, time, date
+from datetime import datetime, date
 
 from logilab.database import get_db_helper
 
diff --git a/test/unittest_db.py b/test/unittest_db.py
--- a/test/unittest_db.py
+++ b/test/unittest_db.py
@@ -25,11 +25,22 @@ import socket
 import unittest
 
 from logilab.common.shellutils import getlogin
-from logilab.database import *
+from logilab.database import (
+    set_prefered_driver,
+    UnknownDriver,
+    get_connection,
+    PyConnection,
+    PyCursor,
+    get_dbapi_compliant_module,
+    get_db_helper,
+    FunctionDescr,
+    register_function,
+    UnsupportedFunction,
+)
 from logilab.database import _PREFERED_DRIVERS as PREFERED_DRIVERS
 from logilab.database.postgres import _PGAdvFuncHelper
 from logilab.database.sqlite import _SqliteAdvFuncHelper
-from logilab.database import mysql, sqlserver  # trigger registration
+from logilab.database import mysql, sqlserver  # trigger registration # noqa
 
 # from logilab.common.adbh import (_GenericAdvFuncHelper, _SqliteAdvFuncHelper,
 #                                  _PGAdvFuncHelper, _MyAdvFuncHelper,
@@ -81,7 +92,7 @@ class GetCnxTC(unittest.TestCase):
         self.host = "localhost"
         try:
             socket.gethostbyname(self.host)
-        except:
+        except Exception:
             self.skipTest("those tests require specific DB configuration")
         self.db = "template1"
         self.user = getlogin()
@@ -99,7 +110,7 @@ class GetCnxTC(unittest.TestCase):
         except ImportError:
             self.skipTest("python-mysqldb is not installed")
         try:
-            cnx = get_connection("mysql", self.host, database="", user="root", quiet=1)
+            get_connection("mysql", self.host, database="", user="root", quiet=1)
         except MySQLdb.OperationalError as ex:
             if ex.args[0] == 1045:  # find MysqlDb
                 self.skipTest("mysql test requires a specific configuration")
@@ -117,7 +128,7 @@ class GetCnxTC(unittest.TestCase):
             cnx = get_connection(
                 "postgres", self.host, self.db, self.user, self.passwd, quiet=1
             )
-        except psycopg2.OperationalError as ex:
+        except psycopg2.OperationalError:
             self.skipTest("pgsql test requires a specific configuration")
         self.failIf(
             isinstance(cnx, PyConnection), "cnx should *not* be a PyConnection instance"
@@ -145,7 +156,7 @@ class GetCnxTC(unittest.TestCase):
                 quiet=1,
                 pywrap=True,
             )
-        except psycopg2.OperationalError as ex:
+        except psycopg2.OperationalError:
             self.skipTest("pgsql test requires a specific configuration")
         cursor = cnx.cursor()
         self.failUnless(
diff --git a/test/unittest_fti.py b/test/unittest_fti.py
--- a/test/unittest_fti.py
+++ b/test/unittest_fti.py
@@ -115,7 +115,10 @@ class IndexerTC(unittest.TestCase):
             self.cnx.received,
             [
                 (
-                    "SELECT count(*) as rating, appears0.uid FROM appears as appears0, word as word0 WHERE word0.word = %(word0)s  AND word0.word_id = appears0.word_id  GROUP BY appears0.uid ;",
+                    "SELECT count(*) as rating, appears0.uid "
+                    "FROM appears as appears0, word as word0 "
+                    "WHERE word0.word = %(word0)s  AND word0.word_id = appears0.word_id  "
+                    "GROUP BY appears0.uid ;",
                     {"word0": "ginco"},
                 )
             ],
@@ -127,7 +130,11 @@ class IndexerTC(unittest.TestCase):
             self.cnx.received,
             [
                 (
-                    "SELECT count(*) as rating, appears0.uid FROM appears as appears0, word as word0, appears as appears1, word as word1 WHERE word0.word = %(word0)s  AND word0.word_id = appears0.word_id  AND word1.word = %(word1)s  AND word1.word_id = appears1.word_id  AND appears0.uid = appears1.uid  GROUP BY appears0.uid ;",
+                    "SELECT count(*) as rating, appears0.uid "
+                    "FROM appears as appears0, word as word0, appears as appears1, word as word1 "
+                    "WHERE word0.word = %(word0)s  AND word0.word_id = appears0.word_id  AND "
+                    "word1.word = %(word1)s  AND word1.word_id = appears1.word_id  AND "
+                    "appears0.uid = appears1.uid  GROUP BY appears0.uid ;",
                     {"word1": "jpl", "word0": "ginco"},
                 )
             ],
diff --git a/test/unittest_mysql.py b/test/unittest_mysql.py
--- a/test/unittest_mysql.py
+++ b/test/unittest_mysql.py
@@ -57,7 +57,8 @@ class MyHelperTC(unittest.TestCase):
             self.cnx.received,
             [
                 (
-                    "SELECT 1, uid FROM appears WHERE MATCH (words) AGAINST (%(words)s IN BOOLEAN MODE)",
+                    "SELECT 1, uid FROM appears "
+                    "WHERE MATCH (words) AGAINST (%(words)s IN BOOLEAN MODE)",
                     {"words": "ginco jpl"},
                 )
             ],
diff --git a/test/unittest_postgres.py b/test/unittest_postgres.py
--- a/test/unittest_postgres.py
+++ b/test/unittest_postgres.py
@@ -43,7 +43,8 @@ class PGHelperTC(unittest.TestCase):
             [
                 (
                     "INSERT INTO appears(uid, words, weight) "
-                    "VALUES (%(uid)s, setweight(to_tsvector(%(config)s, %(wrds_A)s), 'A')||setweight(to_tsvector(%(config)s, %(wrds_B)s), 'B'), 1.0);",
+                    "VALUES (%(uid)s, setweight(to_tsvector(%(config)s, %(wrds_A)s), 'A')||"
+                    "setweight(to_tsvector(%(config)s, %(wrds_B)s), 'B'), 1.0);",
                     {
                         "wrds_B": "cubic 456",
                         "wrds_A": "ginco jpl bla blip blop blap",
diff --git a/test/unittest_sqlgen.py b/test/unittest_sqlgen.py
--- a/test/unittest_sqlgen.py
+++ b/test/unittest_sqlgen.py
@@ -16,9 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with logilab-database. If not, see <http://www.gnu.org/licenses/>.
 import unittest
-from datetime import datetime, time, date
-
-from logilab.database import get_db_helper
+
 from logilab.database.sqlgen import SQLGenerator, SQLExpression
 
 
diff --git a/test/unittest_sqlserver2005.py b/test/unittest_sqlserver2005.py
--- a/test/unittest_sqlserver2005.py
+++ b/test/unittest_sqlserver2005.py
@@ -28,7 +28,7 @@ from logilab import database as db
 
 
 def monkey_patch_import_driver_module(driver, drivers, quiet=True):
-    if not driver in drivers:
+    if driver not in drivers:
         raise db.UnknownDriver(driver)
     for modname in drivers[driver]:
         try:
@@ -81,7 +81,8 @@ class SqlServer2005HelperTC(unittest.Tes
         new_sql = self.helper.sql_add_order_by(sql, ["1", "2"], [1, 2], True, False)
         self.assertEqual(
             new_sql,
-            "SELECT T1.C0,T1.C1 FROM (SELECT A AS C0, B AS C1, C FROM Table1, Table2 WHERE Table1.D = Table2.D) AS T1\nORDER BY T1.C0,T1.C1",
+            "SELECT T1.C0,T1.C1 FROM (SELECT A AS C0, B AS C1, C FROM Table1, Table2 "
+            "WHERE Table1.D = Table2.D) AS T1\nORDER BY T1.C0,T1.C1",
         )
 
     def test_order_by_with_limit(self):
@@ -96,7 +97,10 @@ class SqlServer2005HelperTC(unittest.Tes
         )
         self.assertEqual(
             new_sql,
-            """WITH orderedrows AS (\nSELECT \n C0,  C1, _L01\n, ROW_NUMBER() OVER (ORDER BY  C0,  C1) AS __RowNumber\nFROM (\nSELECT A  AS  C0, B  AS  C1, C AS _L01 FROM  Table1, Table2 WHERE Table1.D = Table2.D\n) AS _SQ1 )\nSELECT \n C0,  C1, _L01\nFROM orderedrows WHERE \n__RowNumber <= 20 AND __RowNumber > 10""",
+            "WITH orderedrows AS (\nSELECT \n C0,  C1, _L01\n, ROW_NUMBER() OVER "
+            "(ORDER BY  C0,  C1) AS __RowNumber\nFROM (\nSELECT A  AS  C0, B  AS  C1, C AS _L01 "
+            "FROM  Table1, Table2 WHERE Table1.D = Table2.D\n) AS _SQ1 )\nSELECT \n C0,  C1, _L01\n"
+            "FROM orderedrows WHERE \n__RowNumber <= 20 AND __RowNumber > 10",
         )
 
 



More information about the cubicweb-devel mailing list