#author("2020-03-11T15:39:16+09:00","","")
#contents()
*** マニュアル ・ 教材関係 [#q1d4541f]
- [[Python 3.7.3 ドキュメント (公式マニュアル):https://docs.python.org/ja/3/]]
- [[Python Boot Camp Text:https://pycamp.pycon.jp/]]
*** .bashrc の設定(Ubuntu 18.04 のデフォルトを python3 系に指定) [#a988efa5]
alias python='python3'
alias pip='pip3'がある
*** .vimrc の設定 (python ファイルに対してだけ有効な設定、TABをスペース4に変更など) [#p38d979c]
autocmd FileType python setl autoindent
autocmd FileType python setl smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
autocmd FileType python setl tabstop=8 expandtab shiftwidth=4 softtabstop=4
*** 関数の場所、main() 関数の意味 [#ba83ba57]
- Pythonは上から下にプログラム文を解釈するので &color(red){先に関数を定義しないと関数を呼び出しできません};
- &color(red){実行部分のコードをmain()関数で括る}; ことで変数の定義はすべて関数の中で行われるので、仮に間違って変数名に同じ名前をつけていてもスコープが異なるので干渉せずプログラムは問題なく動作してしまい &color(red){バグの温床となってしまうという潜在的な危険性が回避}; される。
- 先頭でmain()関数を定義した場合でも &color(red){main()関数の呼び出しは必ず最後}; です。Pythonはプログラムを上から順に実行しますが、プログラムが作動する起点(エントリポイント)は最後のmain()です。それまでに関数がすべて定義済みならば、関数どうしの順序を入れ替えても動作に支障はありません。
- [[Pythonスクリプトの書き方(4パターン):https://gammasoft.jp/python/python-script-pattern/]]
*** 関数の引数(args, kwargs) [#n74096ab]
関数定義で引数に*と**(1個または2個のアスタリスク)をつけると、任意の数の引数(可変長引数)を指定することができる
- *args: 複数の引数をタプルとして受け取る
- **kwargs: 複数のキーワード引数を辞書として受け取る (kw = key word)
-- キーワード引数 : 引数名=値 で指定する
- [[詳細説明:https://note.nkmk.me/python-args-kwargs-usage/]]
*** ファイル読み込み(コマンドライン引数でファイル名を指定、エラー処理付き) [#je854e30]
import sys
script_name = sys.argv[0]
try:
file_name = sys.argv[1]
file_desc = open(file_name, 'r')
except IndexError:
print 'Usage: %s TEXTFILE' % script_name
except IOError:
print '"%s" cannot be opened.' % file_name
else:
# ファイルを一行ずつ読み込む場合
for line in file_desc:
print line
# ファイル全体をまとめてリストに読み込む場合
lines = file_desc.readlines()
print lines
file_desc.close()
*** 数字の整形(桁区切り、小数点の丸め) [#z0157cf8]
>>> "{:,}".format(1234.5678)
'1,234.5678'
>>> "{:,.2f}".format(1234.5678)
'1,234.57'
logging.debug("size = %s byte", "{:,}".format(vfp_size))
*** print の自動改行を無効化 [#l87282c8]
- print("abc","") のように空白文字を行末に付与する
*** 文字列を指定したセパレータで分解する [#r2bcea4c]
- split("セパレータ文字") で要素に分解されてリストに格納される
- split("セパレータ",N) で N 番目まで区切って、以下は分解しない
*** python で正規表現を使う方法 [#i15d2659]
import re
- re モジュールには正規表現パターンを使用した検索、置換、連結、分割などのメソッドがある
- これらのメソッドはマッチした文字列を返すものもあれば、MatchObject インスタンスを返すものがある
|group()|マッチした文字列を返す|
|start()|マッチした文字列の開始位置を返す|
|end()|マッチした文字列の終了位置を返す|
|span()|マッチした文字列の (開始位置, 終了位置) のタプルを返す|
- [[python 正規表現 HOWTO:https://docs.python.org/ja/3/howto/regex.html]]
*** [[Python 日付、時刻の処理 (外部モジュール dateutil):https://qiita.com/xza/items/9618e25a8cb08c44cdb0]] [#q98f0aae]
- インストール
munakata@muna-E450:~/code/python/$ pip3 install python-dateutil
*** SQLite [#q40bf7c8]
- [[python3でsqlite3の操作。作成や読み出しなどの基礎:https://qiita.com/saira/items/e08c8849cea6c3b5eb0c]]
- SQLiteのデータ型は5種類しかない
-- NULL,
-- INTEGER(整数),
-- REAL(浮動小数点),
-- TEXT(文字列),
-- BLOB(バイナリ)
- ヘッダー表示
-- .headers ON
- 表示形式 = .mode
-- .mode line
-- .mode list
-- .mode column
*** Open CV [#w30f7baa]
- install (sudo をつけて実行するとエラーになる)
munakata@muna-E450:~/code/python/vlman2019$ pip3 install opencv-python
- それでも module not found になる場合、OpenCV は特定のバージョンの下にインストールされるので実行中の python のバージョン(3.6 or 3.7 とか)を確認する。
*** pylint 関連 [#e6878135]
- ~/.config に pylintrc を作成
[local] munakata:~/source/python/vlman$ pylint --generate-rcfile > pylintrc
- 警告の最後の()内を~/.config/pylintrcのdisabledに追加すればその警告は表示されなくなる
-- &ref(pylintrc);
- 正攻法の解決方法例は [[Python(Pylint)の警告を解決する:https://imoni.net/tips/00xx/0014.html#c0326]]
*** 仮想環境の構築 [#p31eb801]
- python では開発アプリ毎に python や関連ソフトウエアのバージョンを固定するためにデフォルトで仮想環境での開発を推奨している
- とはいえ、個人で一つのアプリを作るだけなら仮想環境は不要だったが vscode で DJANGO のデバッグをするのに必須という事がわかり追加した。
- コマンド(Ubuntu 18.04)
-- パッケージ名 : python3-venv
-- 仮想環境の構築 : python -m env (仮想環境名)
-- 仮想環境の起動 : (仮想環境ディレクトリー)&color(red){source bin/activate};
-- 仮想環境の終了 : deactivate
-- 仮想環境の削除 : rm (仮想環境名)
munakata@muna-E450:~/code/python$ sudo apt install python3-venv
munakata@muna-E450:~/code/python$ python -m venv vman <------------ vman という仮想環境を構築
munakata@muna-E450:~/code/python$ cd vman
munakata@muna-E450:~/code/python/vman$ ls -l <-------------- 仮想環境を構築しただけでファイルが色々できている
合計 20
drwxr-xr-x 2 munakata munakata 4096 6月 25 17:51 bin
drwxr-xr-x 2 munakata munakata 4096 6月 25 17:51 include
drwxr-xr-x 3 munakata munakata 4096 6月 25 17:51 lib
lrwxrwxrwx 1 munakata munakata 3 6月 25 17:51 lib64 -> lib
-rw-r--r-- 1 munakata munakata 69 6月 25 17:51 pyvenv.cfg
drwxr-xr-x 3 munakata munakata 4096 6月 25 17:51 share
munakata@muna-E450:~/code/python/vman$ source bin/activate <------------ 仮想環境の起動
(vman) munakata@muna-E450:~/code/python/vman$ <----------- プロンプトに仮想環境名が付与された
(vman) munakata@muna-E450:~/code/python/vman$ deactivate <--------- 仮想環境から抜ける時
munakata@muna-E450:~/code/python/vman$ <----------- プロンプトが元に戻った
*** [[PyAV:https://docs.mikeboers.com/pyav/develop/index.html]] [#sa9f8d6b]
- [[installation:https://docs.mikeboers.com/pyav/develop/installation.html]]
*** [[python-pptx:https://python-pptx.readthedocs.io/en/latest/index.html]] [#wa6b7489]
- [[Documentatrion (user guide):https://python-pptx.readthedocs.io/en/latest/index.html#user-guide]]
-- [[Concepts (オブジェクトの構成、テンプレートの読み込み):https://python-pptx.readthedocs.io/en/latest/user/concepts.html]]
-- [[Working with Slides (スライドの構造、スライドの追加):https://python-pptx.readthedocs.io/en/latest/user/slides.html]]
-- [[Understanding placeholders (既存テンプレートのプレースホルダーの確認方法):https://python-pptx.readthedocs.io/en/latest/user/placeholders-understanding.html]]
- スライドアスペクト指定
-- # set slide aspect = 16:9
-- prs.slide_width = 9144000
-- prs.slide_height = 5143500
//-- 16:9 標準設定 は 33,867cm x 19,05cm => 12,192,120 EMU x 6,858,000 EMU
//-- https://gist.github.com/glass5er/748cda36befe17fd1cb0
- スライドレイアウトの指定(スライドマスターのどの表示形式を使うか)
-- prs.slide_layouts[0] = Title (presentation title slide)
-- prs.slide_layouts[1] = Title and Content
-- prs.slide_layouts[2] = Section Header (sometimes called Segue)
-- prs.slide_layouts[3] = Two Content (side by side bullet textboxes)
-- prs.slide_layouts[4] = Comparison (same but additional title for each side by side content box)
-- prs.slide_layouts[5] = Title Only
-- prs.slide_layouts[6] = Blank
-- prs.slide_layouts[7] = Content with Caption
-- prs.slide_layouts[8] = Picture with Caption
- &ref(python-pptx — python-pptx 0.6.18 documentation.mhtml);
*** [[2次元リストの任意の要素でソートする:https://note.nkmk.me/python-list-2d-sort/]] [#nda6fe71]
- デフォルトでは最初の要素でソートされる
- ソートキーを指定するには python の lambda(無名関数)を使う
sort_2d_table.sort(key=lambda, x: x[1]) <--- 2つめの要素でソート
- 参考 URL
-- [[python-pptxまとめ:https://qiita.com/pocket8137/items/3d8fda2c47664bf9130b]]
-- [[Pythonでパワポの説明資料(報告書)を生成する:https://qiita.com/code_440/items/22e8539da465686496d3]]
-- [[pythonで作ったパワーポイントで自己紹介してみた:https://qiita.com/Ishio/items/54912dc3ead034c32f88]]
-- [[[Python]爆速で報告パワポを生成する!:https://qiita.com/mion-fer/items/0630400bc245784ad881]]
*** [[Progate:https://prog-8.com/languages]] [#v37c3304]
- account = Google account
- 2019/07/04 で有償会員を退会
*** [[paiza:https://paiza.jp/works/mypage]] [#lbdf6a1f]
- account = magu775@gmail.com
- pass = frex7785
*** 参考 URL [#ib6cbc95]
-- [[35歳+営業職+プログラミング経験なしのPython学習方法(プログラミング経験なし向け):https://qiita.com/1-row/items/6a67596bf5b7f624eba2]]
-- [[初学者・初級者向け Django の学習ロードマップ:https://akiyoko.hatenablog.jp/entry/2018/12/01/133427]]