| Conditions | 17 |
| Total Lines | 73 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| 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 lighthouse_garden.lighthouse.database.get_result_by_report_file() 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 | #!/usr/bin/env python3 |
||
| 14 | :param target: Dict |
||
| 15 | :param file_suffix: String Additional suffix |
||
| 16 | :return: Dict |
||
| 17 | """ |
||
| 18 | data_file = f'{utility.get_data_dir()}_{target["identifier"]}{file_suffix}.json' |
||
| 19 | if os.path.isfile(data_file): |
||
| 20 | with open(data_file, 'r') as read_file: |
||
| 21 | return json.load(read_file) |
||
| 22 | else: |
||
| 23 | return [] |
||
| 24 | |||
| 25 | |||
| 26 | def set_data(target, data, file_suffix=''): |
||
| 27 | """ |
||
| 28 | Write json data by target |
||
| 29 | :param target: Dict |
||
| 30 | :param data: Dict |
||
| 31 | :param file_suffix: String Additional suffix |
||
| 32 | :return: |
||
| 33 | """ |
||
| 34 | data_file = f'{utility.get_data_dir()}_{target["identifier"]}{file_suffix}.json' |
||
| 35 | with open(data_file, 'w') as write_file: |
||
| 36 | json.dump(data, write_file) |
||
| 37 | |||
| 38 | |||
| 39 | def add_value_to_history(target, result): |
||
| 40 | """ |
||
| 41 | Adding a new result value to the history data of a specific target |
||
| 42 | :param target: Dict |
||
| 43 | :param result: Dict |
||
| 44 | :return: |
||
| 45 | """ |
||
| 46 | _data = get_data(target, '.history') |
||
| 47 | while len(_data) > system.config['keep_history']: |
||
| 48 | output.println(f'{output.Subject.INFO} Cleaning history value', verbose_only=True) |
||
| 49 | utility.remove_file(f'{utility.get_export_dir()}{_data[0]["report"]}') |
||
| 50 | del _data[0] |
||
| 51 | _data.append(result) |
||
| 52 | set_data(target, _data, '.history') |
||
| 53 | |||
| 54 | |||
| 55 | def get_average_by_attribute(target, attribute): |
||
| 56 | """ |
||
| 57 | Calculates the average value from a specific attribute by all available history values |
||
| 58 | :param target: Dict |
||
| 59 | :param attribute: String |
||
| 60 | :return: Float |
||
| 61 | """ |
||
| 62 | history_data = get_history_by_attribute(target, attribute) |
||
| 63 | return sum(history_data) / float(len(history_data)) |
||
| 64 | |||
| 65 | |||
| 66 | def get_average_peak(target, attribute, min_max=True): |
||
| 67 | """ |
||
| 68 | Get the min or max value of a specific attribute by all available history values |
||
| 69 | :param target: |
||
| 70 | :param attribute: |
||
| 71 | :param min_max: |
||
| 72 | :return: |
||
| 73 | """ |
||
| 74 | history_data = get_history_by_attribute(target, attribute) |
||
| 75 | |||
| 76 | if min_max: |
||
| 77 | return min(history_data) |
||
| 78 | else: |
||
| 79 | return max(history_data) |
||
| 80 | |||
| 81 | |||
| 82 | def get_history_by_attribute(target, attribute): |
||
| 83 | """ |
||
| 84 | Fetch all history values of a specific attribute |
||
| 85 | :param target: Dict |
||
| 86 | :param attribute: String |
||
| 87 | :return: |
||
| 145 |