| Conditions | 13 |
| Total Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like HistorySQLite.read_node_history() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import logging |
||
| 87 | def read_node_history(self, node_id, start, end, nb_values): |
||
| 88 | with self._lock: |
||
| 89 | _c_read = self._conn.cursor() |
||
| 90 | |||
| 91 | order = "ASC" |
||
| 92 | |||
| 93 | if start is None or start == ua.DateTimeMinValue: |
||
| 94 | order = "DESC" |
||
| 95 | start = ua.DateTimeMinValue |
||
| 96 | |||
| 97 | if end is None or end == ua.DateTimeMinValue: |
||
| 98 | end = datetime.utcnow() + timedelta(days=1) |
||
| 99 | |||
| 100 | if start < end: |
||
| 101 | start_time = start.isoformat(' ') |
||
| 102 | end_time = end.isoformat(' ') |
||
| 103 | else: |
||
| 104 | order = "DESC" |
||
| 105 | start_time = end.isoformat(' ') |
||
| 106 | end_time = start.isoformat(' ') |
||
| 107 | |||
| 108 | if nb_values: |
||
| 109 | limit = nb_values + 1 # add 1 to the number of values for retrieving a continuation point |
||
| 110 | else: |
||
| 111 | limit = -1 # in SQLite a LIMIT of -1 returns all results |
||
| 112 | |||
| 113 | table = self._get_table_name(node_id) |
||
| 114 | |||
| 115 | cont = None |
||
| 116 | results = [] |
||
| 117 | |||
| 118 | # select values from the database; recreate UA Variant from binary ORDER BY "ServerTimestamp" DESC |
||
| 119 | try: |
||
| 120 | for row in _c_read.execute('SELECT * FROM "{tn}" WHERE "ServerTimestamp" BETWEEN ? AND ? ' |
||
| 121 | 'ORDER BY "Id" {dir} LIMIT ?'.format(tn=table, dir=order), (start_time, end_time, limit,)): |
||
| 122 | dv = ua.DataValue(ua.Variant.from_binary(Buffer(row[6]))) |
||
| 123 | dv.ServerTimestamp = row[1] |
||
| 124 | dv.SourceTimestamp = row[2] |
||
| 125 | dv.StatusCode = ua.StatusCode(row[3]) |
||
| 126 | |||
| 127 | results.append(dv) |
||
| 128 | |||
| 129 | except sqlite3.Error as e: |
||
| 130 | self.logger.error('Historizing SQL Read Error for %s: %s', node_id, e) |
||
| 131 | |||
| 132 | if nb_values: |
||
| 133 | if start > ua.DateTimeMinValue and len(results) > nb_values: |
||
| 134 | cont = results[nb_values].ServerTimestamp |
||
| 135 | |||
| 136 | results = results[:nb_values] |
||
| 137 | |||
| 138 | return results, cont |
||
| 139 | |||
| 156 |