Overview¶
IDE¶
- PyCharm
- PyScripter
- PTVS (Python Tools for Visual Studio)
Style guide¶
守らなくても良いけど、理由が無いなら守っておく。
Code style checker¶
- pycodestyle
- flake8 - PEP8以外にいくつかスタイルガイドが追加されているチェッカー
- hacking - OpenStack projectで使われているスタイルガイドラインチェッカー
- ~~pep8~~ obsolated. pycodestypeに改名。
virtualenv/venv¶
どうやらDjangoによればvenvは1か所のディレクトリにまとめて置いておくのが良いらしい。
と言うわけで、今後はそうすることにした。
Windowsの場合¶
Powershellでこのようにする。
> mkdir ~\.virtualenvs
> python -m venv ~\.virtualenvs\project
> .\~\.virtualenvs\project\Scripts\activate.ps1
以下のエラーが出る場合はps1スクリプトを叩けるように、事前に Set-ExecutionPolicy RemoteSigned -Scope Process -force
を叩いておく。
+ CategoryInfo : セキュリティ エラー: (: ) []、PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
Testing¶
BDD framework¶
- pytest-bdd
- behave
pytest¶
- pytest-cov
- pytest-html
- pytest-benchmark
- Python: pytest-benchmark でベンチマークテストを書く - https://blog.amedama.jp/entry/2019/05/08/235438
- Pytestを利用したPythonのユニットテスト
- tarvern
- pytest拡張だけど単体でも動かせるRESTful APIテストフレームワーク
- xxx が含まれるテスト関数だけ実行:
pytest -k xxx
Packaging¶
- setup.py
- requirements.txt
インストール¶
- setup.pyを使う(パッケージングされたファイル郡に含まれていれば)
python setup.py install
- easy_install
apt-get install python-setuptools
- proxyは環境変数が使える
export http_proxy=http://example.com:8080 export https_proxy=http://example.com:8080
- proxyは環境変数が使える
- pip
- apt-get install python-pip
- バージョン低すぎて、uninstall出来ないとかなるので、easy_install推奨。
easy_install pip
- proxy:
pip install <package-name> --proxy=http://example.com:8080
- apt-get install python-pip
Utilities¶
- Python の PrettyTable でプリティーな ASCII テーブルを作る http://momijiame.tumblr.com/post/44704474054/
Parser¶
- textfsm - Network装置のshow出力をパースしたりするのに使われる。
状態遷移¶
- transitions
非同期ジョブ¶
- Celery
- Django-Q
Graph¶
- matplotlib
- Bokeh
- Highstock
matplotlib¶
公式サイト: http://matplotlib.sourceforge.net/index.html
- 数値計算、グラフ描画の助けに。
- ヒストグラムのbinsはmax(value)で決定してもいいものだろうか。デフォルト10って何なんだろう。
- ヒストグラムに突っ込んだ値を何分割するかを決めるから、最大値でいいんじゃね、って思ってる。
- py2exeでexe化するときは少し癖があるよう。
- http://www.py2exe.org/index.cgi/MatPlotLib
- winの場合は、さらにsipとPyQt4._qtを消せば動いた。
- インストール
- win用のインストーラ使ってしまったので...
- windows 7でeasy_installしたらエラー出たし...
pypcap¶
- 本家
- Windows + Python 2.7.1でビルド
- http://pypcap.googlecode.com/files/pypcap-1.1.tar.gz
- Windows環境だと、WpdPackが必要。
- コンパイルは下記URLと一緒。
memo¶
- setup.pyではトップディレクトリの
.
から始まるディレクトリをpackage_files
に含めることができない - Pythonのパッケージ周りの歴史簡易まとめ - https://www.m3tech.blog/entry/python-packaging
- [[pipenv]]
- Python の or と and 演算子の罠 https://qiita.com/keisuke-nakata/items/e0598b2c13807f102469
- デフォルト値Noneを使って引数処理するときもう少しスマートに書けそう
- Pythonでloggingモジュール使ってライブラリ内でログ出力する時のまとめ
- jmespath - JSONPath filter library
- Python3で通信内容確認する方法メモ - https://qiita.com/htohsaki/items/e7150d83484e662eaabb
- eventlet backdoor - https://eventlet.net/doc/modules/backdoor.html
- Pythonの処理系 - 2020年における各Python処理系の状況
RFC5952¶
文字列とバイナリ表現の相互変換用のスクリプト。
こんな感じで使う。
>>> import rfc5952
>>> binary = '\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'
>>> rfc5952.inet6_ntoa(binary)
'2001:db8::1'
>>> string = "2001:db8:1f1f:90::82"
>>> rfc5952.inet6_aton(string)
' \x01\r\xb8\x1f\x1f\x00\x90\x00\x00\x00\x00\x00\x00\x00\x82'
~~Bitbucketで公開: https://bitbucket.org/ainoniwa/rfc5952/src/dcb12192d35f730d843f2922e0d0b75f702d2092/rfc5952.py~~
GUI¶
PySide¶
PythonからQtを扱うためのバインディング。PyQt4のライセンスがGPLだったので、LGPLなPySideを使うことにする
- PySide
- The PySide tutorial
- Qtプログラミング入門
- Qtに関する技術資料
- designer.exe
- GUIでGUIの設計が出来るツール
- Pythonディレクトリの".\Lib\site-packages\PySide"に追加されてる。
wxPython¶
- http://www.wxpython.org/
- PythonでGUI。
- マルチプラットフォーム。
- サンプルはこの辺が良さそう。
開発環境¶
- Windows
- 小さめのスクリプトをクイックスタート
- PyScripter
- ただし、スクリプトパスに2バイト文字があると、"UnicodeEncodeError: 'ascii' codec can't encode characters" とか言われる。
- http://code.google.com/p/pyscripter/issues/detail?id=427
- PyScripter
- Visual Studio 2010に上乗せ
- http://pytools.codeplex.com/
- 要 : Microsoft Visual Studio 2010 Shell (Integrated)
- http://www.microsoft.com/download/en/details.aspx?id=115
- http://pytools.codeplex.com/
- ライブラリのコンパイル
- 直接Pythonと関係はないけど、ライブラリのコンパイルにgccがいるのでWindowsの場合はコンパイラを準備したりする
- MinGW: http://sourceforge.net/projects/mingw/
- 常にmingwを使うなら、
- "C:\Python26\Lib\distutils\distutils.cfg"を作って、以下を書けばいい。
[build] compiler = mingw32
- "C:\Python26\Lib\distutils\distutils.cfg"を作って、以下を書けばいい。
- 小さめのスクリプトをクイックスタート
- 独立環境
- virtualenv
- pip install virtualenv
- 使う時は、以下をセットで。
$ virtualenv project-name --no-site-packages $ source ./project-name/bin/activate $ # --- ホスト名に(project-name)が付いたらOK $ deactivate $ # --- これで抜ける
- virtualenv
最終更新日: 2021-05-22 13:37:13