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