Conditions | 10 |
Total Lines | 62 |
Code Lines | 57 |
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 mandos.search.chembl.target_traversal.StandardTargetTraversalStrategy.parse() 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 __future__ import annotations |
||
73 | @classmethod |
||
74 | def parse(cls, lines: Sequence[str]) -> Set[TargetEdgeReqs]: |
||
75 | pat_type = r"([a-z_]+)" |
||
76 | pat_rel = r"([<>~=])" |
||
77 | pat_accept = r"(?:accept:([\-*^$]?))?" |
||
78 | pat_src_words = r"(?:src:'''(.+?)''')?" |
||
79 | pat_dest_words = r"(?:dest:'''(.+?)''')?" |
||
80 | comment = r"(?:#(.*))?" |
||
81 | pat = f"^ *{pat_type} *{pat_rel} *{pat_type} *{pat_accept} * {pat_src_words} *{pat_dest_words} *{comment} *$" |
||
82 | print(pat) |
||
83 | pat = re.compile(pat) |
||
84 | to_rel = { |
||
85 | ">": TargetRelType.superset_of, |
||
86 | "<": TargetRelType.subset_of, |
||
87 | "~": TargetRelType.overlaps_with, |
||
88 | "=": TargetRelType.equivalent_to, |
||
89 | "*": TargetRelType.any_link, |
||
90 | ".": TargetRelType.self_link, |
||
91 | } |
||
92 | to_accept = { |
||
93 | "*": Acceptance.always, |
||
94 | "-": Acceptance.never, |
||
95 | "^": Acceptance.at_start, |
||
96 | "$": Acceptance.at_end, |
||
97 | } |
||
98 | edges = set() |
||
99 | edge_to_acceptance: Dict[TargetEdgeReqs, Acceptance] = {} |
||
100 | for line in lines: |
||
101 | match = pat.fullmatch(line) |
||
102 | if match is None: |
||
103 | raise AssertionError(f"Could not parse line '{line}'") |
||
104 | try: |
||
105 | src_str = match.group(1).lower() |
||
106 | sources = TargetType.all_types() if src_str == "any" else [TargetType[src_str]] |
||
107 | rel = to_rel[match.group(2)] |
||
108 | dest_str = match.group(3).lower() |
||
109 | targets = TargetType.all_types() if dest_str == "any" else [TargetType[dest_str]] |
||
110 | accept = to_accept[match.group(4).lower()] |
||
111 | src_pat = ( |
||
112 | None |
||
113 | if match.group(5) is None or match.group(5) == "" |
||
114 | else re.compile(match.group(5)) |
||
115 | ) |
||
116 | dest_pat = ( |
||
117 | None |
||
118 | if match.group(6) is None or match.group(6) == "" |
||
119 | else re.compile(match.group(6)) |
||
120 | ) |
||
121 | except (KeyError, TypeError, sre_compile.error): |
||
122 | raise AssertionError(f"Could not parse line '{line}'") |
||
123 | for source in sources: |
||
124 | for dest in targets: |
||
125 | edge = TargetEdgeReqs( |
||
126 | src_type=source, |
||
127 | src_pattern=src_pat, |
||
128 | rel_type=rel, |
||
129 | dest_type=dest, |
||
130 | dest_pattern=dest_pat, |
||
131 | ) |
||
132 | edges.add(edge) |
||
133 | edge_to_acceptance[edge] = accept |
||
134 | return edges |
||
135 | |||
252 |