Browse Source

[lint] clean models/variable.py

Maxime Alves LIRMM@home 3 years ago
parent
commit
c9904a64d8
1 changed files with 27 additions and 17 deletions
  1. 27
    17
      pyheatpump/models/variable.py

+ 27
- 17
pyheatpump/models/variable.py View File

@@ -1,10 +1,8 @@
1 1
 from pyheatpump.db import RowClass
2 2
 from pyheatpump.db import DB
3 3
 from datetime import date
4
-from pyheatpump.modbus import rtu
5 4
 
6 5
 from .variable_type import VariableType
7
-from .variable_value import VariableValue
8 6
 
9 7
 from pyheatpump.logger import logger_init
10 8
 logger = logger_init()
@@ -21,18 +19,22 @@ class Variable(RowClass):
21 19
 
22 20
         super().__init__(**kwargs)
23 21
 
22
+    @property
23
+    def type_slabel(self):
24
+        return self.type.slabel
24 25
 
25 26
     def insert(self):
26 27
 
27 28
         try:
28
-            self.type_slabel = self.type.slabel
29
-
30 29
             DB.sql(
31 30
             """INSERT INTO variable
32 31
             (type, address)
33 32
             VALUES
34 33
             (:type_slabel, :address)
35
-            """, self.__dict__)
34
+            """, {
35
+                'type_slabel': self.type_slabel,
36
+                'address': self.address
37
+            })
36 38
             return True
37 39
         except Exception as e:
38 40
             print(e)
@@ -52,36 +54,44 @@ class Variable(RowClass):
52 54
 
53 55
     @staticmethod
54 56
     def getall():
55
-        return dict([
56
-            (str(row), Variable.getall_of_type(row))
57
+        return {
58
+            str(row): Variable.getall_of_type(row)
57 59
             for _, row in VariableType.getall().items()
58
-        ])
60
+        }
59 61
 
60 62
 
61 63
     @staticmethod
62
-    def getall_of_type(type: VariableType) -> dict:
64
+    def getall_of_type(var_type: VariableType) -> dict:
63 65
         return {
64 66
             row['address']: Variable(**dict(row))
65 67
             for row in DB.sql(
66 68
             """SELECT * FROM variable
67
-            WHERE type = :slabel
69
+            WHERE var_type = :slabel
68 70
                 AND address >= :start_address
69
-                AND address <= :end_address""",
70
-            type.__dict__)
71
+                AND address <= :end_address""", {
72
+
73
+                'slabel': var_type.slabel,
74
+                'start_address': var_type.start_address,
75
+                'end_address': var_type.end_address
76
+            })
71 77
        }
72 78
 
73 79
 
74 80
     @staticmethod
75
-    def getall_values_of_type(type: VariableType) -> dict:
76
-        return Variable.getall_values_of_type_since(type, 0)
81
+    def getall_values_of_type(var_type: VariableType) -> dict:
82
+        return Variable.getall_values_of_type_since(var_type, 0)
77 83
 
78 84
 
79 85
     @staticmethod
80
-    def getall_values_of_type_since(type: VariableType, since: int) -> dict:
86
+    def getall_values_of_type_since(var_type: VariableType, since: int) -> dict:
81 87
         floatcast = lambda x: round(float(x) / 10, 2)
82 88
         cast_fct = floatcast if type.slabel == 'A' else lambda x: x
83
-        params = type.__dict__.copy()
84
-        params.update({'since': since})
89
+        params = {
90
+            'slabel': var_type.slabel,
91
+            'start_address': var_type.start_address,
92
+            'end_address': var_type.end_address,
93
+            'since': since
94
+        }
85 95
 
86 96
         return {
87 97
             row['address']: cast_fct(row['value'])

Loading…
Cancel
Save