| Conditions | 20 |
| Total Lines | 142 |
| Code Lines | 91 |
| 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._run_script() 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 |
||
| 291 | def _run_script(): |
||
| 292 | global array_seen, nl, sd, tail_text |
||
| 293 | |||
| 294 | if len(sys.argv) > 1: |
||
| 295 | bmdir = sys.argv[1].rstrip('/') + '/' |
||
| 296 | else: |
||
| 297 | bmdir = '../../bmpm/' |
||
| 298 | |||
| 299 | outfilename = '../abydos/phonetic/_beider_morse_data.py' |
||
| 300 | outfile = codecs.open(outfilename, 'w', 'utf-8') |
||
| 301 | |||
| 302 | outfile.write( |
||
| 303 | '# Copyright 2014-2020 by \ |
||
| 304 | Christopher C. Little.\n# This file is part of Abydos.\n#\n# This file is \ |
||
| 305 | based on Alexander Beider and Stephen P. Morse\'s implementation\n# of the \ |
||
| 306 | Beider-Morse Phonetic Matching (BMPM) System, available at\n# \ |
||
| 307 | http://stevemorse.org/phonetics/bmpm.htm.\n#\n# Abydos is free software: \ |
||
| 308 | you can redistribute it and/or modify\n# it under the terms of the GNU \ |
||
| 309 | General Public License as published by\n# the Free Software Foundation, \ |
||
| 310 | either version 3 of the License, or\n# (at your option) any later version.\n\ |
||
| 311 | #\n# Abydos is distributed in the hope that it will be useful,\n# but WITHOUT \ |
||
| 312 | ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or \ |
||
| 313 | FITNESS FOR A PARTICULAR PURPOSE. See the\n# GNU General Public License for \ |
||
| 314 | more details.\n#\n# You should have received a copy of the GNU General Public \ |
||
| 315 | License\n# along with Abydos. If not, see <http://www.gnu.org/licenses/>.\n\n\ |
||
| 316 | """abydos.phonetic._beider_morse_data.\n\nBehind-the-scenes constants, \ |
||
| 317 | rules, etc. for the Beider-Morse Phonentic\nMatching (BMPM) algorithm\n\nDO \ |
||
| 318 | NOT EDIT - This document is automatically generated from the reference\n\ |
||
| 319 | implementation in PHP.\n"""\n\nfrom \ |
||
| 320 | __future__ import (\n absolute_import,\n division,\n print_function,\ |
||
| 321 | unicode_literals,\n)\n' |
||
| 322 | ) |
||
| 323 | |||
| 324 | outfile.write('L_NONE = 0\n') |
||
| 325 | for i, l in enumerate(lang_tuple): |
||
| 326 | outfile.write('L_' + l.upper() + ' = 2**' + str(i) + '\n') |
||
| 327 | outfile.write('\n\n') |
||
| 328 | |||
| 329 | tail_text += '\nBMDATA = {} # type: ignore\n' |
||
| 330 | |||
| 331 | subdirs = ('gen', 'sep', 'ash') |
||
| 332 | |||
| 333 | for s in subdirs: |
||
| 334 | sd = s |
||
| 335 | tail_text += "\nBMDATA['" + s + "'] = {}\n" |
||
| 336 | tail_text += "BMDATA['" + s + "']['approx'] = {}\n" |
||
| 337 | tail_text += "BMDATA['" + s + "']['exact'] = {}\n" |
||
| 338 | tail_text += "BMDATA['" + s + "']['rules'] = {}\n" |
||
| 339 | tail_text += "BMDATA['" + s + "']['hebrew'] = {}\n\n" |
||
| 340 | tail_text += ( |
||
| 341 | "BMDATA['" |
||
| 342 | + s |
||
| 343 | + "']['language_rules'] = _" |
||
| 344 | + s.upper() |
||
| 345 | + '_LANGUAGE_RULES\n' |
||
| 346 | ) |
||
| 347 | tail_text += ( |
||
| 348 | "BMDATA['" + s + "']['languages'] = _" + s.upper() + '_LANGUAGES\n' |
||
| 349 | ) |
||
| 350 | |||
| 351 | phps = [ |
||
| 352 | f |
||
| 353 | for f in sorted(listdir(bmdir + s + '/')) |
||
| 354 | if (isfile(bmdir + s + '/' + f) and f.endswith('.php')) |
||
| 355 | ] |
||
| 356 | for infilename in phps: |
||
| 357 | for pfx in ( |
||
| 358 | 'rules', |
||
| 359 | 'approx', |
||
| 360 | 'exact', |
||
| 361 | 'hebrew', |
||
| 362 | 'language', |
||
| 363 | 'lang', |
||
| 364 | ): |
||
| 365 | if infilename.startswith(pfx): |
||
| 366 | array_seen = False |
||
| 367 | infilepath = bmdir + s + '/' + infilename |
||
| 368 | infileenc = chardet.detect(open(infilepath, 'rb').read())[ |
||
| 369 | 'encoding' |
||
| 370 | ] |
||
| 371 | print(s + '/' + infilename) # noqa: T001 |
||
| 372 | infile = codecs.open(infilepath, 'r', infileenc) |
||
| 373 | # if infilename.startswith('lang'): |
||
| 374 | # tuplename = infilename[:-4] |
||
| 375 | # else: |
||
| 376 | # tuplename = pfx + '_' + infilename[len(pfx) : -4] |
||
| 377 | # indent = len(tuplename) + 21 |
||
| 378 | |||
| 379 | outfile.write('# ' + s + '/' + infilename + '\n') |
||
| 380 | |||
| 381 | ignore = True |
||
| 382 | for line in infile: |
||
| 383 | if 'function Language' in line: |
||
| 384 | break |
||
| 385 | if not ignore: |
||
| 386 | if re.search(r'\?>', line): |
||
| 387 | ignore = True |
||
| 388 | else: |
||
| 389 | line = pythonize(line, infilename[:-4], s) |
||
| 390 | if line.startswith('BMDATA'): |
||
| 391 | tail_text += line |
||
| 392 | else: |
||
| 393 | outfile.write(line) |
||
| 394 | if '*/' in line: |
||
| 395 | ignore = False |
||
| 396 | |||
| 397 | outfile.write('\n\n') |
||
| 398 | break |
||
| 399 | |||
| 400 | outfile.write(tail_text) |
||
| 401 | |||
| 402 | outfile.close() |
||
| 403 | outfilelines = codecs.open(outfilename, 'r', 'utf-8').readlines() |
||
| 404 | outfile = codecs.open(outfilename, 'w', 'utf-8') |
||
| 405 | nl = False |
||
| 406 | fixlanguagesarray = False |
||
| 407 | |||
| 408 | sep_lang = ( |
||
| 409 | "('any', 'french', 'hebrew', 'italian', 'portuguese', 'spanish')" |
||
| 410 | ) |
||
| 411 | |||
| 412 | for line in outfilelines: |
||
| 413 | line = line.rstrip() |
||
| 414 | if line: |
||
| 415 | if fixlanguagesarray: |
||
| 416 | line = ' ' + line.strip() |
||
| 417 | fixlanguagesarray = False |
||
| 418 | if len(line) > 79 or sep_lang in line: |
||
| 419 | line += ' # noqa: E501' |
||
| 420 | outfile.write(line) |
||
| 421 | if not line.endswith('='): |
||
| 422 | outfile.write('\n') |
||
| 423 | else: |
||
| 424 | fixlanguagesarray = True |
||
| 425 | nl = False |
||
| 426 | else: |
||
| 427 | if not nl: |
||
| 428 | outfile.write('\n') |
||
| 429 | nl = True |
||
| 430 | |||
| 431 | outfile.write( |
||
| 432 | "\n\nif __name__ == '__main__':\n import doctest\n\n\ |
||
| 433 | doctest.testmod()\n" |
||
| 439 |