| Conditions | 23 |
| Total Lines | 96 |
| Code Lines | 71 |
| Lines | 96 |
| Ratio | 100 % |
| Changes | 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 tariff.get_energy_item_tariffs() 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 | from datetime import timedelta |
||
| 110 | View Code Duplication | def get_energy_item_tariffs(cost_center_id, energy_item_id, start_datetime_utc, end_datetime_utc): |
|
| 111 | # todo: verify parameters |
||
| 112 | if cost_center_id is None: |
||
| 113 | return dict() |
||
| 114 | |||
| 115 | # get timezone offset in minutes, this value will be returned to client |
||
| 116 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 117 | if config.utc_offset[0] == '-': |
||
| 118 | timezone_offset = -timezone_offset |
||
| 119 | |||
| 120 | tariff_dict = collections.OrderedDict() |
||
| 121 | |||
| 122 | cnx = None |
||
| 123 | cursor = None |
||
| 124 | try: |
||
| 125 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 126 | cursor = cnx.cursor() |
||
| 127 | query_tariffs = (" SELECT t.id, t.valid_from_datetime_utc, t.valid_through_datetime_utc " |
||
| 128 | " FROM tbl_tariffs t, tbl_cost_centers_tariffs cct, tbl_energy_items ei " |
||
| 129 | " WHERE ei.id = %s AND " |
||
| 130 | " t.energy_category_id = ei.energy_category_id AND " |
||
| 131 | " t.id = cct.tariff_id AND " |
||
| 132 | " cct.cost_center_id = %s AND " |
||
| 133 | " t.valid_through_datetime_utc >= %s AND " |
||
| 134 | " t.valid_from_datetime_utc <= %s " |
||
| 135 | " ORDER BY t.valid_from_datetime_utc ") |
||
| 136 | cursor.execute(query_tariffs, (energy_item_id, cost_center_id, start_datetime_utc, end_datetime_utc,)) |
||
| 137 | rows_tariffs = cursor.fetchall() |
||
| 138 | except Exception as e: |
||
| 139 | print(str(e)) |
||
| 140 | if cnx: |
||
| 141 | cnx.disconnect() |
||
| 142 | if cursor: |
||
| 143 | cursor.close() |
||
| 144 | return dict() |
||
| 145 | |||
| 146 | if rows_tariffs is None or len(rows_tariffs) == 0: |
||
| 147 | if cursor: |
||
| 148 | cursor.close() |
||
| 149 | if cnx: |
||
| 150 | cnx.disconnect() |
||
| 151 | return dict() |
||
| 152 | |||
| 153 | for row in rows_tariffs: |
||
| 154 | tariff_dict[row[0]] = {'valid_from_datetime_utc': row[1], |
||
| 155 | 'valid_through_datetime_utc': row[2], |
||
| 156 | 'rates': list()} |
||
| 157 | |||
| 158 | try: |
||
| 159 | query_timeofuse_tariffs = (" SELECT tariff_id, start_time_of_day, end_time_of_day, price " |
||
| 160 | " FROM tbl_tariffs_timeofuses " |
||
| 161 | " WHERE tariff_id IN ( " + ', '.join(map(str, tariff_dict.keys())) + ")" |
||
| 162 | " ORDER BY tariff_id, start_time_of_day ") |
||
| 163 | cursor.execute(query_timeofuse_tariffs, ) |
||
| 164 | rows_timeofuse_tariffs = cursor.fetchall() |
||
| 165 | except Exception as e: |
||
| 166 | print(str(e)) |
||
| 167 | if cnx: |
||
| 168 | cnx.disconnect() |
||
| 169 | if cursor: |
||
| 170 | cursor.close() |
||
| 171 | return dict() |
||
| 172 | |||
| 173 | if cursor: |
||
| 174 | cursor.close() |
||
| 175 | if cnx: |
||
| 176 | cnx.disconnect() |
||
| 177 | |||
| 178 | if rows_timeofuse_tariffs is None or len(rows_timeofuse_tariffs) == 0: |
||
| 179 | return dict() |
||
| 180 | |||
| 181 | for row in rows_timeofuse_tariffs: |
||
| 182 | tariff_dict[row[0]]['rates'].append({'start_time_of_day': row[1], |
||
| 183 | 'end_time_of_day': row[2], |
||
| 184 | 'price': row[3]}) |
||
| 185 | |||
| 186 | result = dict() |
||
| 187 | for tariff_id, tariff_value in tariff_dict.items(): |
||
| 188 | current_datetime_utc = tariff_value['valid_from_datetime_utc'] |
||
| 189 | while current_datetime_utc < tariff_value['valid_through_datetime_utc']: |
||
| 190 | for rate in tariff_value['rates']: |
||
| 191 | current_datetime_local = current_datetime_utc + timedelta(minutes=timezone_offset) |
||
| 192 | seconds_since_midnight = (current_datetime_local - |
||
| 193 | current_datetime_local.replace(hour=0, |
||
| 194 | second=0, |
||
| 195 | microsecond=0, |
||
| 196 | tzinfo=None)).total_seconds() |
||
| 197 | if rate['start_time_of_day'].total_seconds() <= \ |
||
| 198 | seconds_since_midnight < rate['end_time_of_day'].total_seconds(): |
||
| 199 | result[current_datetime_utc] = rate['price'] |
||
| 200 | break |
||
| 201 | |||
| 202 | # start from the next time slot |
||
| 203 | current_datetime_utc += timedelta(minutes=config.minutes_to_count) |
||
| 204 | |||
| 205 | return {k: v for k, v in result.items() if start_datetime_utc <= k <= end_datetime_utc} |
||
| 206 |