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