Conditions | 18 |
Total Lines | 77 |
Code Lines | 48 |
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.interpreter.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 | def get_result_by_report_file(target, file_name): |
||
15 | """ |
||
16 | |||
17 | :param target: Dict |
||
18 | :param file_name: String |
||
19 | :return: |
||
20 | """ |
||
21 | output.println(f'{output.Subject.INFO} Processing result of report', verbose_only=True) |
||
22 | _report_path = f'{utility.get_data_dir()}{file_name}.report.json' |
||
23 | _report = None |
||
24 | |||
25 | if os.path.isfile(_report_path): |
||
26 | with open(_report_path, 'r') as read_file: |
||
27 | _report = json.load(read_file) |
||
28 | else: |
||
29 | sys.exit(f'{output.Subject.ERROR} Report file not found: {_report_path}') |
||
30 | |||
31 | if not isinstance(_report, dict): |
||
32 | sys.exit(f'{output.Subject.ERROR} Report not readable') |
||
33 | |||
34 | if _report['categories']['performance']['score']: |
||
35 | _performance = int(round(_report['categories']['performance']['score'] * 100)) |
||
36 | else: |
||
37 | return None |
||
38 | |||
39 | _result = defaultdict(lambda: defaultdict(dict)) |
||
40 | _result = { |
||
41 | 'title': target['title'], |
||
42 | 'url': target['url'], |
||
43 | 'performance': _performance, |
||
44 | 'report': f'{utility.get_data_dir(absolute_path=False)}{file_name}.report.html', |
||
45 | 'link': f'#{target["identifier"]}', |
||
46 | 'date': '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()) |
||
47 | } |
||
48 | |||
49 | # additional metrics |
||
50 | if 'accessibility' in _report['categories'] and _report['categories']['accessibility']['score']: |
||
51 | _result['accessibility'] = int(round(_report['categories']['accessibility']['score'] * 100)) |
||
52 | |||
53 | if 'best-practices' in _report['categories'] and _report['categories']['best-practices']['score']: |
||
54 | _result['best-practices'] = int(round(_report['categories']['best-practices']['score'] * 100)) |
||
55 | |||
56 | if 'seo' in _report['categories'] and _report['categories']['seo']['score']: |
||
57 | _result['seo'] = int(round(_report['categories']['seo']['score'] * 100)) |
||
58 | |||
59 | database.add_value_to_history(target, _result) |
||
60 | |||
61 | # audits |
||
62 | _result['audits'] = {} |
||
63 | if _report['audits']['first-contentful-paint']: |
||
64 | _result['audits']['first-contentful-paint'] = _report['audits']['first-contentful-paint']['displayValue'] |
||
65 | |||
66 | if _report['audits']['largest-contentful-paint']: |
||
67 | _result['audits']['largest-contentful-paint'] = _report['audits']['largest-contentful-paint'][ |
||
68 | 'displayValue'] |
||
69 | |||
70 | if _report['audits']['speed-index']: |
||
71 | _result['audits']['speed-index'] = _report['audits']['speed-index']['displayValue'] |
||
72 | |||
73 | if _report['audits']['total-blocking-time']: |
||
74 | _result['audits']['total-blocking-time'] = _report['audits']['total-blocking-time']['displayValue'] |
||
75 | |||
76 | if _report['audits']['interactive']: |
||
77 | _result['audits']['interactive'] = _report['audits']['interactive']['displayValue'] |
||
78 | |||
79 | if _report['audits']['cumulative-layout-shift']: |
||
80 | _result['audits']['cumulative-layout-shift'] = _report['audits']['cumulative-layout-shift']['displayValue'] |
||
81 | |||
82 | # average |
||
83 | _result['average'] = { |
||
84 | 'value': database.get_average_by_attribute(target, 'performance'), |
||
85 | 'min': database.get_average_peak(target, 'performance', True), |
||
86 | 'max': database.get_average_peak(target, 'performance', False), |
||
87 | 'trend': get_trend(target, 'performance') |
||
88 | } |
||
89 | |||
90 | return _result |
||
91 | |||
113 |