#author("2020-03-07T20:00:15+09:00","default:haikikyou","haikikyou")
#author("2020-03-20T19:20:28+09:00","default:haikikyou","haikikyou")
[[moritetuのIT関連技術メモ]]

#contents

* 開発環境 [#y005ce61]

** Python [#ude16fab]

データサイエンスメインで考えている場合は、Anacondaが便利である。~
拡張機能のビルドやCに馴染みがある場合は、pipで入れていけば良いと思う。

- Anaconda
-- データサイエンスで使用されるパッケージがまとまったディストリビューション
-- https://www.continuum.io
- Python
-- https://www.python.org/downloads/


*** コーディングスタイル [#b55434a9]

- ja
-- https://pep8-ja.readthedocs.io/ja/latest/
- en
-- https://www.python.org/dev/peps/pep-0008/

** コードチェックツール [#d5fe5da9]

*** flake8 [#d3ab8628]

コーディングスタイルをチェックしてくれる。

#geshi{{{
php install flake8
}}}

''sample.py''

#geshi(python,number){{{
def hello():
  print("hello")
   print("world")

hello()

print({"foo":1})
}}}

flake8でチェックすると以下のようになる。~
ソース付きで表示してみよう。

#geshi{{{
flake8 --show-source test.py
test.py:2:3: E111 indentation is not a multiple of four
  print("hello")
  ^
test.py:3:1: E305 expected 2 blank lines after class or function definition, found 0
   print("world")
^
test.py:3:1: E272 multiple spaces before keyword
   print("world")
^
test.py:3:1: E999 SyntaxError: invalid character in identifier
   print("world")
^
test.py:7:13: E231 missing whitespace after ':'
print({"foo":1})
            ^
}}}

&label(link){URL};
- https://pypi.org/project/flake8/
- https://flake8.pycqa.org/en/latest/
- https://github.com/PyCQA/flake8
** 保守性・可読性のチェック [#q505d1e2]

*** radon [#w8613227]

#geshi{{{
pip install radon
}}}

''bad.py''

#geshi(python){{{
def deep_nest(data):
    for k, fruits in data.items():
        for name, info in fruits.items():
            if name in ['apple', 'orange']:
                if 'count' in info:
                    if info['count'] > 10:
                        print(f'{name}: {info["count"]}')


deep_nest({
    'fruits': {
        'apple': {
            'count': 10
        },
        'orange': {
            'count': 20
        }
    }
})
}}}

&code(){radon cc};(Cyclomatic Complexity)でチェックしてみる。~
ランクがAであるほど良い。

#geshi{{{
 radon cc -s bad.py
bad.py
    F 1:0 deep_nest - B (6)
}}}

スコアテーブルは以下のとおり。

#geshi{{{
    ============= =====================================================
        1 - 5        A (low risk - simple block)
        6 - 10       B (low risk - well structured and stable block)
        11 - 20      C (moderate risk - slightly complex block)
        21 - 30      D (more than moderate risk - more complex block)
        31 - 40      E (high risk - complex block, alarming)
        41+          F (very high risk - error-prone, unstable block)
    ============= =====================================================
}}}

&label(link){URL};
- https://pypi.org/project/radon/
- https://github.com/rubik/radon
- https://radon.readthedocs.io/en/latest/
** エディタ [#kf9c2318]
VSCodeが軽量で使いやすい。~
以下からダウンロードできる。

- VSCode
-- https://azure.microsoft.com/ja-jp/products/visual-studio-code/
- PyCharm
-- https://www.jetbrains.com/ja-jp/pycharm/

* デバッガ [#cbe266f3]

** pdb [#p582ff5b]

#geshi{{{
import pdb; pdb.set_trace()
}}}
- https://docs.python.org/ja/3/library/pdb.html

** prdb [#n266c5a3]

インストール

 pip install rpdb

プログラム内に記述

#geshi{{{
import rpdb; rpdb.set_trace()
}}}

リモートから接続

 nc 127.0.0.1 4444

&label(warn){参考};
- https://pypi.org/project/rpdb/

** ptvsd [#tc202496]

 pip install ptvsd

#geshi{{{
import ptvsd; ptvsd.enable_attach()
}}}

&label(warn){参考};
- https://github.com/Microsoft/ptvsd/
- VSCode
-- https://docs.microsoft.com/ja-jp/visualstudio/python/debugging-python-code-on-remote-linux-machines?view=vs-2019
* 関連リンク [#ze659e4d]

#ls2(python,noprefix)
* 参考リンク [#f2e4306f]

- [[Python公式>https://www.python.org]] - &size(11){&color(gray){on https://www.python.org};};
- [[python_japan>https://www.python.jp/index.html]] - &size(11){&color(gray){on https://www.python.jp/index.html};};
- ドキュメント
-- [[Python 3.8.0 ドキュメント>https://docs.python.org/ja/3.8/]] - &size(11){&color(gray){on https://docs.python.org/ja/3.8/};};
- Python 3 Patterns, Recipes and Idioms
-- [[Python 3 Patterns, Recipes and Idioms>https://python-3-patterns-idioms-test.readthedocs.io/en/latest/index.html]] - &size(11){&color(gray){on https://python-3-patterns-idioms-test.readthedocs.io/en/latest/index.html};};

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP