From 9f2b81d385275794eb587408deb317c7cb7e0b51 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Wed, 11 Oct 2023 11:47:09 +0200
Subject: [PATCH] pep8: Fix code format for model.

---
 .../BoundaryCondition/BoundaryCondition.py    |  8 ++---
 .../BoundaryConditionList.py                  |  8 ++---
 src/Model/Except.py                           |  6 +++-
 src/Model/Friction/Friction.py                |  3 +-
 src/Model/Geometry/PointXYZ.py                |  2 +-
 src/Model/Geometry/ProfileXYZ.py              | 19 ++++++------
 src/Model/Geometry/Reach.py                   | 13 ++++----
 .../InitialConditions/InitialConditions.py    |  3 +-
 .../LateralContribution.py                    | 11 +++----
 .../LateralContributionList.py                |  8 ++---
 src/Model/Network/Edge.py                     |  5 +++-
 src/Model/River.py                            |  6 ++--
 src/Model/SedimentLayer/SedimentLayer.py      | 22 +++++++-------
 .../SolverParameters/SolverParametersList.py  | 10 +++++--
 src/Model/Study.py                            | 30 ++++++++++++++-----
 src/Model/Tools/PamhyrDB.py                   |  4 +--
 src/Model/Tools/PamhyrList.py                 | 16 +++++-----
 17 files changed, 106 insertions(+), 68 deletions(-)

diff --git a/src/Model/BoundaryCondition/BoundaryCondition.py b/src/Model/BoundaryCondition/BoundaryCondition.py
index f7e09f3d..21d9604a 100644
--- a/src/Model/BoundaryCondition/BoundaryCondition.py
+++ b/src/Model/BoundaryCondition/BoundaryCondition.py
@@ -188,7 +188,7 @@ class BoundaryCondition(SQLSubModel):
 
     @classmethod
     def time_convert(cls, data):
-        if type(data) == str and data.count(":") == 3:
+        if data is str and data.count(":") == 3:
             return old_pamhyr_date_to_timestamp(data)
 
         return int(data)
@@ -302,10 +302,10 @@ class BoundaryCondition(SQLSubModel):
         return self.data[index]
 
     def get_range(self, _range):
-        l = []
+        lst = []
         for r in _range:
-            l.append(r)
-        return l
+            lst.append(r)
+        return lst
 
     def _set_i_c_v(self, index, column, value):
         v = list(self._data[index])
diff --git a/src/Model/BoundaryCondition/BoundaryConditionList.py b/src/Model/BoundaryCondition/BoundaryConditionList.py
index 44499316..edf6d60e 100644
--- a/src/Model/BoundaryCondition/BoundaryConditionList.py
+++ b/src/Model/BoundaryCondition/BoundaryConditionList.py
@@ -74,16 +74,16 @@ class BoundaryConditionList(PamhyrModelListWithTab):
     def __copy__(self):
         new = BoundaryConditionList()
 
-        for l in self._tabs:
-            new.tabs[l] = self._tabs[l].copy()
+        for lst in self._tabs:
+            new.tabs[lst] = self._tabs[lst].copy()
 
         return new
 
     def __deepcopy__(self):
         new = BoundaryConditionList()
 
-        for l in self._tabs:
-            new.tabs[l] = self._tabs[l].deepcopy()
+        for lst in self._tabs:
+            new.tabs[lst] = self._tabs[lst].deepcopy()
 
         return new
 
diff --git a/src/Model/Except.py b/src/Model/Except.py
index ce667e38..ef45ffd0 100644
--- a/src/Model/Except.py
+++ b/src/Model/Except.py
@@ -82,12 +82,16 @@ class NotImplementedMethodeError(ExeceptionWithMessageBox):
         self.alert()
 
     def __str__(self):
+        class_type = self.obj
+        if self.obj.__class__ != type:
+            class_type = self.obj.__class__
+
         return (
             _translate("Exception", "Method") +
             f" '{self.func.__name__}' " +
             _translate("Exception", "not implemented") +
             _translate("Exception", "for class") +
-            f" '{self.obj.__class__ if self.obj.__class__ != type else self.obj}'"
+            f" '{class_type}'"
         )
 
     def header(self):
diff --git a/src/Model/Friction/Friction.py b/src/Model/Friction/Friction.py
index baea7397..0faa44a4 100644
--- a/src/Model/Friction/Friction.py
+++ b/src/Model/Friction/Friction.py
@@ -92,7 +92,8 @@ class Friction(SQLSubModel):
         ind = data["ind"]
         execute(
             "INSERT INTO " +
-            "friction(ind, begin_kp, end_kp, reach, begin_strickler, end_strickler) " +
+            "friction(ind, begin_kp, end_kp, " +
+            "reach, begin_strickler, end_strickler) " +
             "VALUES (" +
             f"{ind}, {self._begin_kp}, {self._end_kp}, " +
             f"{self._edge.id}, " +
diff --git a/src/Model/Geometry/PointXYZ.py b/src/Model/Geometry/PointXYZ.py
index 9a1082a5..ae91df77 100644
--- a/src/Model/Geometry/PointXYZ.py
+++ b/src/Model/Geometry/PointXYZ.py
@@ -97,7 +97,7 @@ class PointXYZ(Point, SQLSubModel):
                 status=status
             )
 
-            if sl == -1 or sl == None:
+            if sl == -1 or sl is None:
                 new._sl = None
             else:
                 new._sl = next(
diff --git a/src/Model/Geometry/ProfileXYZ.py b/src/Model/Geometry/ProfileXYZ.py
index de0fcd8c..d2691509 100644
--- a/src/Model/Geometry/ProfileXYZ.py
+++ b/src/Model/Geometry/ProfileXYZ.py
@@ -136,7 +136,7 @@ class ProfileXYZ(Profile, SQLSubModel):
                 status=status
             )
 
-            if sl == -1 or sl == None:
+            if sl == -1 or sl is None:
                 new._sl = None
             else:
                 new._sl = next(
@@ -163,7 +163,8 @@ class ProfileXYZ(Profile, SQLSubModel):
 
         sql = (
             "INSERT OR REPLACE INTO " +
-            "geometry_profileXYZ(id, ind, name, reach, kp, num, code1, code2, sl) " +
+            "geometry_profileXYZ(id, ind, name, reach, " +
+            "kp, num, code1, code2, sl) " +
             "VALUES (" +
             f"{self.id}, {ind}, '{self._sql_format(self._name)}', " +
             f"{self.reach.id}, {self.kp}, {self.num}, " +
@@ -378,28 +379,28 @@ class ProfileXYZ(Profile, SQLSubModel):
                 if (first_named_point != last_named_point and
                         first_named_point.x != last_named_point.x):
                     vector = Vector1d(first_named_point, last_named_point)
-                    normalized_direction_vec = vector.normalized_direction_vector()
+                    norm_dir_vec = vector.normalized_direction_vector()
                 else:
                     vector = Vector1d(first_point_not_nan, last_point_not_nan)
-                    normalized_direction_vec = vector.normalized_direction_vector()
+                    norm_dir_vec = vector.normalized_direction_vector()
 
                 for point in self.points:
                     xi = point.x - first_named_point.x
                     yi = point.y - first_named_point.y
-                    station_i = (normalized_direction_vec[0] * xi +
-                                 normalized_direction_vec[1] * yi)
+                    station_i = (norm_dir_vec[0] * xi +
+                                 norm_dir_vec[1] * yi)
                     station.append(station_i)
 
                 constant = station[index_first_named_point]
             elif first_named_point is None:
                 vector = Vector1d(first_point_not_nan, last_point_not_nan)
-                normalized_direction_vec = vector.normalized_direction_vector()
+                norm_dir_vec = vector.normalized_direction_vector()
 
                 for point in self.points:
                     xi = point.x - first_point_not_nan.x
                     yi = point.y - first_point_not_nan.y
-                    station_i = (normalized_direction_vec[0] * xi +
-                                 normalized_direction_vec[1] * yi)
+                    station_i = (norm_dir_vec[0] * xi +
+                                 norm_dir_vec[1] * yi)
                     station.append(station_i)
 
                 z_min = self.z_min()
diff --git a/src/Model/Geometry/Reach.py b/src/Model/Geometry/Reach.py
index c2df3a75..fc5cdc75 100644
--- a/src/Model/Geometry/Reach.py
+++ b/src/Model/Geometry/Reach.py
@@ -102,7 +102,7 @@ class Reach(SQLSubModel):
 
     @property
     def name(self):
-        if self._parent == None:
+        if self._parent is None:
             return ""
 
         return self._parent.name
@@ -345,12 +345,12 @@ class Reach(SQLSubModel):
         for guide in guide_set:
             self._guidelines[guide] = flatten(
                 map(
-                    lambda l: list(
+                    lambda lst: list(
                         # Filter point with name (we assume we have
                         # only one point by profile)
                         filter(
                             lambda p: p.name == guide,
-                            l
+                            lst
                         )
                     ),
                     named_points
@@ -385,7 +385,9 @@ class Reach(SQLSubModel):
             reduce(
                 lambda acc, h: acc + h,
                 map(
-                    lambda l: list(set(l).symmetric_difference(guide_set)),
+                    lambda lst: list(
+                        set(lst).symmetric_difference(guide_set)
+                    ),
                     points_name
                 ),
                 []
@@ -469,7 +471,8 @@ class Reach(SQLSubModel):
         """Import a geometry from file (.ST or .st)
 
         Args:
-            file_path_name: The absolute path of geometry file (.ST or .st) to import.
+            file_path_name: The absolute path of geometry file (.ST or .st)
+                            to import.
 
         Returns:
             Nothing.
diff --git a/src/Model/InitialConditions/InitialConditions.py b/src/Model/InitialConditions/InitialConditions.py
index 20bbb3d0..38cb9646 100644
--- a/src/Model/InitialConditions/InitialConditions.py
+++ b/src/Model/InitialConditions/InitialConditions.py
@@ -114,7 +114,8 @@ class Data(SQLSubModel):
 
         execute(
             "INSERT INTO " +
-            "initial_conditions(ind, name, comment, kp, discharge, height, reach) " +
+            "initial_conditions(ind, name, comment, kp, " +
+            "discharge, height, reach) " +
             "VALUES (" +
             f"{ind}, '{self._sql_format(self.name)}', " +
             f"'{self._sql_format(self._comment)}', " +
diff --git a/src/Model/LateralContribution/LateralContribution.py b/src/Model/LateralContribution/LateralContribution.py
index de117744..53e4384f 100644
--- a/src/Model/LateralContribution/LateralContribution.py
+++ b/src/Model/LateralContribution/LateralContribution.py
@@ -154,7 +154,8 @@ class LateralContribution(SQLSubModel):
 
         sql = (
             "INSERT INTO " +
-            "lateral_contribution(id, name, type, tab, edge, begin_kp, end_kp) " +
+            "lateral_contribution(id, name, type, tab, " +
+            "edge, begin_kp, end_kp) " +
             "VALUES (" +
             f"{self.id}, '{self._sql_format(self._name)}', " +
             f"'{self._sql_format(self._type)}', '{tab}', {edge}, " +
@@ -187,7 +188,7 @@ class LateralContribution(SQLSubModel):
 
     @classmethod
     def time_convert(cls, data):
-        if type(data) == str and data.count(":") == 3:
+        if data is str and data.count(":") == 3:
             return old_pamhyr_date_to_timestamp(data)
 
         return int(data)
@@ -338,10 +339,10 @@ class LateralContribution(SQLSubModel):
         return self.data[index]
 
     def get_range(self, _range):
-        l = []
+        lst = []
         for r in _range:
-            l.append(r)
-        return l
+            lst.append(r)
+        return lst
 
     def _set_i_c_v(self, index, column, value):
         v = list(self._data[index])
diff --git a/src/Model/LateralContribution/LateralContributionList.py b/src/Model/LateralContribution/LateralContributionList.py
index a69330ae..d399d73d 100644
--- a/src/Model/LateralContribution/LateralContributionList.py
+++ b/src/Model/LateralContribution/LateralContributionList.py
@@ -71,16 +71,16 @@ class LateralContributionList(PamhyrModelListWithTab):
     def __copy__(self):
         new = LateralContributionList()
 
-        for l in self._tabs:
-            new.tabs[l] = self._tabs[l].copy()
+        for lst in self._tabs:
+            new.tabs[lst] = self._tabs[lst].copy()
 
         return new
 
     def __deepcopy__(self):
         new = LateralContributionList()
 
-        for l in self._tabs:
-            new.tabs[l] = self._tabs[l].deepcopy()
+        for lst in self._tabs:
+            new.tabs[lst] = self._tabs[lst].deepcopy()
 
         return new
 
diff --git a/src/Model/Network/Edge.py b/src/Model/Network/Edge.py
index dddd8418..440e03eb 100644
--- a/src/Model/Network/Edge.py
+++ b/src/Model/Network/Edge.py
@@ -75,7 +75,10 @@ class Edge(object):
 
     @property
     def name(self):
-        return self._name if self._name != "" else f"{self.node1.name} -> {self.node2.name}"
+        name = self._name
+        if self._name == "":
+            name = f"{self.node1.name} -> {self.node2.name}"
+        return name
 
     def is_enable(self):
         return self._enable
diff --git a/src/Model/River.py b/src/Model/River.py
index 798a24e1..c0cb13b7 100644
--- a/src/Model/River.py
+++ b/src/Model/River.py
@@ -28,7 +28,9 @@ from Model.Geometry.Profile import Profile
 from Model.Geometry.Reach import Reach
 
 from Model.BoundaryCondition.BoundaryConditionList import BoundaryConditionList
-from Model.LateralContribution.LateralContributionList import LateralContributionList
+from Model.LateralContribution.LateralContributionList import (
+    LateralContributionList
+)
 from Model.InitialConditions.InitialConditionsDict import InitialConditionsDict
 from Model.Stricklers.StricklersList import StricklersList
 from Model.Friction.FrictionList import FrictionList
@@ -383,7 +385,7 @@ class River(Graph, SQLSubModel):
         has = len(self._sediment_layers) != 0
         has &= any(
             filter(
-                lambda p: p.sl != None,
+                lambda p: p.sl is not None,
                 flatten(
                     map(lambda e: e.reach.profiles, self.edges())
                 )
diff --git a/src/Model/SedimentLayer/SedimentLayer.py b/src/Model/SedimentLayer/SedimentLayer.py
index 69e140bf..8f91efcf 100644
--- a/src/Model/SedimentLayer/SedimentLayer.py
+++ b/src/Model/SedimentLayer/SedimentLayer.py
@@ -117,7 +117,8 @@ class Layer(SQLSubModel):
         sl = data["sl"]
 
         table = execute(
-            "SELECT id, ind, name, type, height, d50, sigma, critical_constraint " +
+            "SELECT id, ind, name, type, height, " +
+            "d50, sigma, critical_constraint " +
             "FROM sedimentary_layer_layer " +
             f"WHERE sl = {sl}"
         )
@@ -146,7 +147,8 @@ class Layer(SQLSubModel):
 
         sql = (
             "INSERT INTO " +
-            "sedimentary_layer_layer(id, ind, name, type, height, d50, sigma, critical_constraint, sl) " +
+            "sedimentary_layer_layer(id, ind, name, type, height, " +
+            "d50, sigma, critical_constraint, sl) " +
             "VALUES (" +
             f"{self.id}, {ind}, '{self._sql_format(self._name)}', " +
             f"'{self._sql_format(self._type)}', {self._height}, " +
@@ -197,7 +199,7 @@ class SedimentLayer(SQLSubModel):
 
     def height(self):
         return list(
-            map(lambda l: l.height, self._layers)
+            map(lambda layer: layer.height, self._layers)
         )
 
     @property
@@ -210,7 +212,7 @@ class SedimentLayer(SQLSubModel):
 
     def names(self):
         return list(
-            map(lambda l: l.name, self._layers)
+            map(lambda layer: layer.name, self._layers)
         )
 
     @property
@@ -280,9 +282,9 @@ class SedimentLayer(SQLSubModel):
         data["sl"] = self
 
         ind = 0
-        for l in self._layers:
+        for layer in self._layers:
             data["ind"] = ind
-            l._sql_save(execute, data)
+            layer._sql_save(execute, data)
             ind += 1
 
         return True
@@ -325,8 +327,8 @@ class SedimentLayer(SQLSubModel):
         if index >= 0:
             next = index - 1
 
-            l = self._layers
-            l[index], l[next] = l[next], l[index]
+            lst = self._layers
+            lst[index], lst[next] = lst[next], lst[index]
 
             self._status.modified()
 
@@ -334,7 +336,7 @@ class SedimentLayer(SQLSubModel):
         if index + 1 < len(self._layers):
             prev = index + 1
 
-            l = self._layers
-            l[index], l[prev] = l[prev], l[index]
+            lst = self._layers
+            lst[index], lst[prev] = lst[prev], lst[index]
 
             self._status.modified()
diff --git a/src/Model/SolverParameters/SolverParametersList.py b/src/Model/SolverParameters/SolverParametersList.py
index 41906ef0..a1dec056 100644
--- a/src/Model/SolverParameters/SolverParametersList.py
+++ b/src/Model/SolverParameters/SolverParametersList.py
@@ -99,9 +99,13 @@ class SolverParametersList(PamhyrModelList):
         if major == minor == "0":
             if int(release) < 3:
                 execute(
-                    f"UPDATE solver_parameter SET name='mage_implicitation' WHERE name='mage_implication'")
+                    "UPDATE solver_parameter SET name='mage_implicitation' " +
+                    "WHERE name='mage_implication'"
+                )
                 execute(
-                    f"UPDATE solver_parameter SET name='mage_iteration_type' WHERE name='mage_iter_type'")
+                    "UPDATE solver_parameter SET name='mage_iteration_type' " +
+                    "WHERE name='mage_iter_type'"
+                )
 
             if int(release) < 4:
                 solvers = execute(
@@ -229,7 +233,7 @@ class SolverParametersList(PamhyrModelList):
                     self._lst
                 )
             )["value"]
-        except:
+        except Exception:
             return None
 
     def set(self, index, new):
diff --git a/src/Model/Study.py b/src/Model/Study.py
index 597b68e2..1369e269 100644
--- a/src/Model/Study.py
+++ b/src/Model/Study.py
@@ -182,7 +182,8 @@ class Study(SQLModel):
         self.execute(
             "CREATE TABLE info(key TEXT NOT NULL UNIQUE, value TEXT NOT NULL)")
         self.execute(
-            f"INSERT INTO info VALUES ('version', '{self._sql_format(self._version)}')",
+            "INSERT INTO info VALUES ('version', " +
+            f"'{self._sql_format(self._version)}')",
             commit=True
         )
         self.execute("INSERT INTO info VALUES ('name', '')")
@@ -260,17 +261,32 @@ class Study(SQLModel):
 
     def _save(self):
         self.execute(
-            f"UPDATE info SET value='{self._sql_format(self.name)}' WHERE key='name'")
+            f"UPDATE info SET " +
+            f"value='{self._sql_format(self.name)}' WHERE key='name'"
+        )
         self.execute(
-            f"UPDATE info SET value='{self._sql_format(self.description)}' WHERE key='description'")
+            f"UPDATE info SET " +
+            f"value='{self._sql_format(self.description)}' " +
+            "WHERE key='description'"
+        )
         self.execute(
-            f"UPDATE info SET value='{self._time_system}' WHERE key='time_system'")
+            f"UPDATE info SET " +
+            f"value='{self._time_system}' WHERE key='time_system'"
+        )
         self.execute(
-            f"UPDATE info SET value='{timestamp(self._date)}' WHERE key='date'")
+            f"UPDATE info SET " +
+            f"value='{timestamp(self._date)}' WHERE key='date'"
+        )
         self.execute(
-            f"UPDATE info SET value='{timestamp(self.creation_date)}' WHERE key='creation_date'")
+            f"UPDATE info SET " +
+            f"value='{timestamp(self.creation_date)}' " +
+            "WHERE key='creation_date'"
+        )
         self.execute(
-            f"UPDATE info SET value='{timestamp(self.last_save_date)}' WHERE key='last_save_date'")
+            f"UPDATE info SET " +
+            f"value='{timestamp(self.last_save_date)}' " +
+            "WHERE key='last_save_date'"
+        )
 
         self._save_submodel([self._river])
         self.commit()
diff --git a/src/Model/Tools/PamhyrDB.py b/src/Model/Tools/PamhyrDB.py
index 803cd05c..078540c3 100644
--- a/src/Model/Tools/PamhyrDB.py
+++ b/src/Model/Tools/PamhyrDB.py
@@ -115,9 +115,9 @@ class SQLSubModel(object):
 
     def _sql_format(self, value):
         # Replace ''' by '&#39;' to preserve SQL injection
-        if type(value) == str:
+        if value is str:
             value = value.replace("'", "&#39;")
-        elif type(value) == bool:
+        elif value is bool:
             value = 'TRUE' if value else 'FALSE'
         return value
 
diff --git a/src/Model/Tools/PamhyrList.py b/src/Model/Tools/PamhyrList.py
index 57160e77..912d4749 100644
--- a/src/Model/Tools/PamhyrList.py
+++ b/src/Model/Tools/PamhyrList.py
@@ -121,8 +121,8 @@ class PamhyrModelList(SQLSubModel):
         if index < len(self._lst):
             next = index - 1
 
-            l = self._lst
-            l[index], l[next] = l[next], l[index]
+            lst = self._lst
+            lst[index], lst[next] = lst[next], lst[index]
 
             if self._status is not None:
                 self._status.modified()
@@ -131,8 +131,8 @@ class PamhyrModelList(SQLSubModel):
         if index >= 0:
             prev = index + 1
 
-            l = self._lst
-            l[index], l[prev] = l[prev], l[index]
+            lst = self._lst
+            lst[index], lst[prev] = lst[prev], lst[index]
 
             if self._status is not None:
                 self._status.modified()
@@ -228,14 +228,14 @@ class PamhyrModelListWithTab(SQLSubModel):
         if index < len(self._tabs[lst]):
             next = index - 1
 
-            l = self._tabs[lst]
-            l[index], l[next] = l[next], l[index]
+            lst = self._tabs[lst]
+            lst[index], lst[next] = lst[next], lst[index]
             self._status.modified()
 
     def move_down(self, lst, index):
         if index >= 0:
             prev = index + 1
 
-            l = self._tabs[lst]
-            l[index], l[prev] = l[prev], l[index]
+            lst = self._tabs[lst]
+            lst[index], lst[prev] = lst[prev], lst[index]
             self._status.modified()
-- 
GitLab