Conditions | 10 |
Total Lines | 63 |
Code Lines | 51 |
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 make_blueprints_table.create_table() 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 | """Generate Blueprints table.""" |
||
6 | def create_table(directory): |
||
7 | """Create the table header and cells.""" |
||
8 | t_header = '' |
||
9 | t_cell = '' |
||
10 | |||
11 | bps_rst = [] |
||
12 | bps_titles = [] |
||
13 | bps_status = [] |
||
14 | |||
15 | max_len_title = -1 |
||
16 | max_len_status = -1 |
||
17 | max_len_bps = -1 |
||
18 | |||
19 | for fp_file in sorted(glob.glob(f'{directory}/EP*.rst')): |
||
20 | split_dir = ''.join(fp_file.split('./blueprints/')) |
||
21 | bp_rst = ''.join(split_dir.split('.rst')) |
||
22 | bps_rst.append(f" :doc:`{bp_rst}<{bp_rst}/>` ") |
||
23 | if max_len_bps < len(bps_rst[-1]): |
||
24 | max_len_bps = len(bps_rst[-1]) |
||
25 | |||
26 | with open(fp_file) as origin_file: |
||
27 | title = '' |
||
28 | status = '' |
||
29 | for line in origin_file: |
||
30 | if re.findall(r':Title:', line): |
||
31 | title = ''.join(line.split(':Title:')) |
||
32 | bps_titles.append(''.join(title.split("\n"))) |
||
33 | if max_len_title < len(title): |
||
34 | max_len_title = len(title) |
||
35 | |||
36 | if re.findall(r':Status:', line): |
||
37 | status = ''.join(line.split(':Status:')) |
||
38 | bps_status.append(''.join(status.split("\n"))) |
||
39 | if max_len_status < len(status): |
||
40 | max_len_status = len(status) |
||
41 | break |
||
42 | |||
43 | th_title_len = max_len_title - len(' Title') |
||
44 | th_status_len = max_len_status - len(' Status') |
||
45 | th_bps_len = max_len_bps - len(' Blueprint') |
||
46 | |||
47 | t_header += f"+{'-' * max_len_bps}+" |
||
48 | t_header += f"{'-' * max_len_title}+{'-' * max_len_status}+\n" |
||
49 | t_header += f"|{' Blueprint'}{' ' * th_bps_len}|{' Title'}" |
||
50 | t_header += f"{' ' * th_title_len}|{' Status'}{' ' * th_status_len}|\n" |
||
51 | t_header += f"+{'=' * max_len_bps}+{'=' * max_len_title}+" |
||
52 | t_header += f"{'=' * max_len_status}+\n" |
||
53 | |||
54 | for i, _ in enumerate(bps_rst, start=0): |
||
55 | title_space = max_len_title - len(bps_titles[i]) |
||
56 | status_space = max_len_status - len(bps_status[i]) |
||
57 | bp_space = max_len_bps - len(bps_rst[i]) |
||
58 | |||
59 | name = bps_rst[i] |
||
60 | title = bps_titles[i] |
||
61 | status = bps_status[i] |
||
62 | |||
63 | t_cell += f"|{name}{' ' * bp_space}|{title}{' ' * title_space}|" |
||
64 | t_cell += f"{status}{' ' * status_space}|\n" |
||
65 | t_cell += f"+{'-' * max_len_bps}+{'-' * max_len_title}+" |
||
66 | t_cell += f"{'-' * max_len_status}+\n" |
||
67 | |||
68 | return t_header + t_cell |
||
69 | |||
81 |