| Conditions | 23 |
| Total Lines | 95 |
| Code Lines | 71 |
| Lines | 95 |
| 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_category_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 |
||
| 10 | View Code Duplication | def get_energy_category_tariffs(cost_center_id, energy_category_id, start_datetime_utc, end_datetime_utc): |
|
|
|
|||
| 11 | # todo: verify parameters |
||
| 12 | if cost_center_id is None: |
||
| 13 | return dict() |
||
| 14 | |||
| 15 | # get timezone offset in minutes, this value will be returned to client |
||
| 16 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 17 | if config.utc_offset[0] == '-': |
||
| 18 | timezone_offset = -timezone_offset |
||
| 19 | |||
| 20 | tariff_dict = collections.OrderedDict() |
||
| 21 | |||
| 22 | cnx = None |
||
| 23 | cursor = None |
||
| 24 | try: |
||
| 25 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 26 | cursor = cnx.cursor() |
||
| 27 | query_tariffs = (" SELECT t.id, t.valid_from_datetime_utc, t.valid_through_datetime_utc " |
||
| 28 | " FROM tbl_tariffs t, tbl_cost_centers_tariffs cct " |
||
| 29 | " WHERE t.energy_category_id = %s AND " |
||
| 30 | " t.id = cct.tariff_id AND " |
||
| 31 | " cct.cost_center_id = %s AND " |
||
| 32 | " t.valid_through_datetime_utc >= %s AND " |
||
| 33 | " t.valid_from_datetime_utc <= %s " |
||
| 34 | " ORDER BY t.valid_from_datetime_utc ") |
||
| 35 | cursor.execute(query_tariffs, (energy_category_id, cost_center_id, start_datetime_utc, end_datetime_utc,)) |
||
| 36 | rows_tariffs = cursor.fetchall() |
||
| 37 | except Exception as e: |
||
| 38 | print(str(e)) |
||
| 39 | if cnx: |
||
| 40 | cnx.disconnect() |
||
| 41 | if cursor: |
||
| 42 | cursor.close() |
||
| 43 | return dict() |
||
| 44 | |||
| 45 | if rows_tariffs is None or len(rows_tariffs) == 0: |
||
| 46 | if cursor: |
||
| 47 | cursor.close() |
||
| 48 | if cnx: |
||
| 49 | cnx.disconnect() |
||
| 50 | return dict() |
||
| 51 | |||
| 52 | for row in rows_tariffs: |
||
| 53 | tariff_dict[row[0]] = {'valid_from_datetime_utc': row[1], |
||
| 54 | 'valid_through_datetime_utc': row[2], |
||
| 55 | 'rates': list()} |
||
| 56 | |||
| 57 | try: |
||
| 58 | query_timeofuse_tariffs = (" SELECT tariff_id, start_time_of_day, end_time_of_day, price " |
||
| 59 | " FROM tbl_tariffs_timeofuses " |
||
| 60 | " WHERE tariff_id IN ( " + ', '.join(map(str, tariff_dict.keys())) + ")" |
||
| 61 | " ORDER BY tariff_id, start_time_of_day ") |
||
| 62 | cursor.execute(query_timeofuse_tariffs, ) |
||
| 63 | rows_timeofuse_tariffs = cursor.fetchall() |
||
| 64 | except Exception as e: |
||
| 65 | print(str(e)) |
||
| 66 | if cnx: |
||
| 67 | cnx.disconnect() |
||
| 68 | if cursor: |
||
| 69 | cursor.close() |
||
| 70 | return dict() |
||
| 71 | |||
| 72 | if cursor: |
||
| 73 | cursor.close() |
||
| 74 | if cnx: |
||
| 75 | cnx.disconnect() |
||
| 76 | |||
| 77 | if rows_timeofuse_tariffs is None or len(rows_timeofuse_tariffs) == 0: |
||
| 78 | return dict() |
||
| 79 | |||
| 80 | for row in rows_timeofuse_tariffs: |
||
| 81 | tariff_dict[row[0]]['rates'].append({'start_time_of_day': row[1], |
||
| 82 | 'end_time_of_day': row[2], |
||
| 83 | 'price': row[3]}) |
||
| 84 | |||
| 85 | result = dict() |
||
| 86 | for tariff_id, tariff_value in tariff_dict.items(): |
||
| 87 | current_datetime_utc = tariff_value['valid_from_datetime_utc'] |
||
| 88 | while current_datetime_utc < tariff_value['valid_through_datetime_utc']: |
||
| 89 | for rate in tariff_value['rates']: |
||
| 90 | current_datetime_local = current_datetime_utc + timedelta(minutes=timezone_offset) |
||
| 91 | seconds_since_midnight = (current_datetime_local - |
||
| 92 | current_datetime_local.replace(hour=0, |
||
| 93 | second=0, |
||
| 94 | microsecond=0, |
||
| 95 | tzinfo=None)).total_seconds() |
||
| 96 | if rate['start_time_of_day'].total_seconds() <= \ |
||
| 97 | seconds_since_midnight < rate['end_time_of_day'].total_seconds(): |
||
| 98 | result[current_datetime_utc] = rate['price'] |
||
| 99 | break |
||
| 100 | |||
| 101 | # start from the next time slot |
||
| 102 | current_datetime_utc += timedelta(minutes=config.minutes_to_count) |
||
| 103 | |||
| 104 | return {k: v for k, v in result.items() if start_datetime_utc <= k <= end_datetime_utc} |
||
| 105 | |||
| 206 |