Conditions | 34 |
Total Lines | 189 |
Code Lines | 133 |
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 bm_php2py.pythonize() 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 |
||
100 | def pythonize(line, fn='', subdir='gen'): |
||
101 | """Convert a line of BMPM code from PHP to Python. |
||
102 | |||
103 | Parameters |
||
104 | ---------- |
||
105 | line : str |
||
106 | A line of code |
||
107 | fn : str |
||
108 | A filename |
||
109 | subdir : str |
||
110 | The file's subdirectory |
||
111 | |||
112 | Returns |
||
113 | ------- |
||
114 | The code in Python |
||
115 | |||
116 | """ |
||
117 | global array_seen, nl, sd |
||
118 | |||
119 | if '$all' in line: |
||
120 | return '' |
||
121 | if 'make the sum of all languages be visible in the function' in line: |
||
122 | return '' |
||
123 | |||
124 | line = line.strip() |
||
125 | |||
126 | if 'array' in line and not line.startswith('//'): |
||
127 | array_seen = True |
||
128 | |||
129 | line = re.sub('//+', '#', line) |
||
130 | # line = re.sub('"\.\((\$.+?)\)\."', r'\1', line) |
||
131 | if line and re.search(r'array\("[^"]+?"\)', line): |
||
132 | # print("### " + line) |
||
133 | line = '' |
||
134 | line = line.replace('array', '') |
||
135 | line = re.sub(r'^\s*', '', line) |
||
136 | line = re.sub(';$', '', line) |
||
137 | line = re.sub('^include_.+', '', line) |
||
138 | |||
139 | line = re.sub( |
||
140 | r'\$(approx|rules|exact)\[LanguageIndex\("([^"]+)", ' |
||
141 | + r'\$languages\)\] = \$([a-zA-Z]+)', |
||
142 | lambda m: ( |
||
143 | "BMDATA['" |
||
144 | + subdir |
||
145 | + "']['" |
||
146 | + m.group(1) |
||
147 | + "'][L_" |
||
148 | + m.group(2).upper() |
||
149 | + '] = _' |
||
150 | + subdir.upper() |
||
151 | + '_' |
||
152 | + c2u(m.group(3)).upper() |
||
153 | ), |
||
154 | line, |
||
155 | ) |
||
156 | |||
157 | line = re.sub( |
||
158 | r'\$(approx|rules|exact|hebrew)([A-Za-z]+) = _merge' |
||
159 | + r'\(\$([a-zA-Z]+), \$([a-zA-Z]+)\)', |
||
160 | lambda m: ( |
||
161 | "BMDATA['" |
||
162 | + subdir |
||
163 | + "']['" |
||
164 | + m.group(1) |
||
165 | + "'][L_" |
||
166 | + c2u(m.group(2)).upper() |
||
167 | + '] = _' |
||
168 | + subdir.upper() |
||
169 | + '_' |
||
170 | + c2u(m.group(3)).upper() |
||
171 | + ' + _' |
||
172 | + subdir.upper() |
||
173 | + '_' |
||
174 | + c2u(m.group(4)).upper() |
||
175 | ), |
||
176 | line, |
||
177 | ) |
||
178 | |||
179 | line = re.sub( |
||
180 | r'\$(approx|rules|exact)\[LanguageIndex\("([^"]+)", ' |
||
181 | + r'\$languages\)\] = _merge\(\$([a-zA-Z]+), \$([a-zA-Z]+)\)', |
||
182 | lambda m: ( |
||
183 | "BMDATA['" |
||
184 | + subdir |
||
185 | + "']['" |
||
186 | + m.group(1) |
||
187 | + "'][L_" |
||
188 | + c2u(m.group(2)).upper() |
||
189 | + '] = _' |
||
190 | + subdir.upper() |
||
191 | + '_' |
||
192 | + c2u(m.group(3)).upper() |
||
193 | + ' + _' |
||
194 | + subdir.upper() |
||
195 | + '_' |
||
196 | + c2u(m.group(4)).upper() |
||
197 | ), |
||
198 | line, |
||
199 | ) |
||
200 | |||
201 | line = re.sub( |
||
202 | r'^\$([a-zA-Z]+)', |
||
203 | lambda m: '_' + sd.upper() + '_' + c2u(m.group(1)).upper(), |
||
204 | line, |
||
205 | ) |
||
206 | |||
207 | for _ in range(len(lang_tuple)): |
||
208 | line = re.sub(r'($[a-zA-Z]+) *\+ *($[a-zA-Z]+)', r'\1\+\2', line) |
||
209 | |||
210 | line = re.sub( |
||
211 | r'\$([a-zA-Z]+)', |
||
212 | lambda m: ( |
||
213 | 'L_' + m.group(1).upper() |
||
214 | if m.group(1) in lang_dict |
||
215 | else '$' + m.group(1) |
||
216 | ), |
||
217 | line, |
||
218 | ) |
||
219 | line = re.sub(r'\[\"\.\((L_[A-Z_+]+)\)\.\"\]', r'[\1]', line) |
||
220 | |||
221 | line = re.sub( |
||
222 | 'L_([A-Z]+)', lambda m: str(lang_dict[m.group(1).lower()]), line |
||
223 | ) |
||
224 | for _ in range(4): |
||
225 | line = re.sub( |
||
226 | r'([0-9]+) *\+ *([0-9]+)', |
||
227 | lambda m: str(int(m.group(1)) + int(m.group(2))), |
||
228 | line, |
||
229 | ) |
||
230 | |||
231 | if fn == 'lang': |
||
232 | if len(line.split(',')) >= 3: |
||
233 | parts = line.split(',') |
||
234 | parts[0] = re.sub('/(.+?)/', r'\1', parts[0]) |
||
235 | # parts[1] = re.sub('\$', 'L_', parts[1]) |
||
236 | # parts[1] = re.sub(' *\+ *', '|', parts[1]) |
||
237 | parts[2] = parts[2].title() |
||
238 | line = ','.join(parts) |
||
239 | |||
240 | if 'languagenames' in fn: |
||
241 | line = line.replace('"', "'") |
||
242 | line = line.replace("','", "', '") |
||
243 | if line and line[0] == "'": |
||
244 | line = ' ' * 14 + line |
||
245 | |||
246 | # fix upstream |
||
247 | # line = line.replace('ë', 'ü') |
||
248 | |||
249 | comment = '' |
||
250 | if '#' in line: |
||
251 | hashsign = line.find('#') |
||
252 | comment = line[hashsign:] |
||
253 | code = line[:hashsign] |
||
254 | else: |
||
255 | code = line |
||
256 | |||
257 | code = code.rstrip() |
||
258 | comment = comment.strip() |
||
259 | if not re.match(r'^\s*$', code): |
||
260 | comment = ' ' + comment |
||
261 | |||
262 | if '(' in code and ')' in code: |
||
263 | prefix = code[: code.find('(') + 1] |
||
264 | suffix = code[code.rfind(')') :] |
||
265 | tuplecontent = code[len(prefix) : len(code) - len(suffix)] |
||
266 | |||
267 | elts = tuplecontent.split(',') |
||
268 | for i in range(len(elts)): |
||
269 | elts[i] = elts[i].strip() |
||
270 | if elts[i][0] == '"' and elts[i][-1] == '"': |
||
271 | elts[i] = "'" + elts[i][1:-1].replace("'", "\\'") + "'" |
||
272 | tuplecontent = ', '.join(elts) |
||
273 | |||
274 | code = prefix + tuplecontent + suffix |
||
275 | |||
276 | line = code + comment |
||
277 | line = re.sub('# *', '# ', line) |
||
278 | |||
279 | if line: |
||
280 | nl = False |
||
281 | if array_seen and not (line[0] == '_' or line.startswith('BMDATA')): |
||
282 | line = ' ' * 4 + line |
||
283 | return line + '\n' |
||
284 | elif not nl: |
||
285 | nl = True |
||
286 | return '\n' |
||
287 | else: |
||
288 | return '' |
||
289 | |||
439 |