Python-Fileのソースを眺める②~適当リファクタ~
前回のあらすじ。
対象のファイルを決めた(copy.py)
テストがどうやらあるっぽいので、うごかしてみる。
file_test.py というテストがすでにあった。さっすが。
残念。うごかない。 どうにもパッケージ周りの読み込みがおかしいんだよな。
別の対応でもパッケージ化してみたけど、うまくよみこめなかった。
相変わらず階層周りがよくわからない。。。まともに突破できた試しがない。
諦めて、 file_test.pyをパッケージの外側に逃がす。
termcolor がいるね
インポートしてない。 pip でぶちこむ
もう一度テストを流してみる。。。
Ran 58 tests in 1.335sOK
はっはー。全然わかんねーけどOKらしい。ほんとかよ。。。
わざとIFをつぶしてみる。
本当かな。全然わからんが、とりあえず分解はできるかもしれない。
Ran 58 tests in 0.352sFAILED (failures=1, errors=50)
1週間という期間も考えて、ひとまずテストを信じることにして進めよう。
今回の目標
・radonのメトリクス値の数値を改善させる。
・テストはいまのまま通るようにする。
Don't initialize the final class・・・
とりあえず目についたIf。 でもこれこのままのほうがわかりやすい気もする。ひとまずメソッドに切り出してみるか。
これを
if not remaining_args and (show_help or interactive or show_trace or show_completion is not None): # Don't initialize the final class or call the final function unless # there's a separator after it, and instead process the current component. break
こうした。
if(_is_final_class_or_function(remaining_args, show_help, interactive, show_trace, show_completion)): # Don't initialize the final class or call the final function unless # there's a separator after it, and instead process the current component. break def _is_final_class_or_function(remaining_args, show_help, interactive, show_trace, show_complation): if(remaining_args): return False if(not show_help): return False if(not interactive): return False if(not show_trace): return False if(show_complation is None): return False return True
本当かなこれ。見やすくなったかな。
とりあえず 否定なのか、orなのか何なのか。ということは悩まなくて済むようになったが。
文量すくないけど一回ここで切るか。
試しにテストを流しなおしたら通った。
適当なところの判定を逆転させると通らなくなったので、既存のテストのままでいけるでしょう。
この状態でradonをぶつけてみる。
修正前は・・・
で、
file.py の数値は「359」
修正後は・・・
で「376」
微々たるもんだけどCyclomatic Complexityの観点からいけば、
改善した。ということになる。
まだまだ「F」ランクなので、もうちょっとつぶしていく。テストがあるっていいなぁ。