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