マニュアル ・ 教材関係†
.bashrc の設定(Ubuntu 18.04 のデフォルトを python3 系に指定)†
alias python='python3'
alias pip='pip3'がある
.vimrc の設定 (python ファイルに対してだけ有効な設定、TABをスペース4に変更など)†
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
ファイル読み込み(コマンドライン引数でファイル名を指定、エラー処理付き)†
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()
数字の整形(桁区切り、小数点の丸め)†
>>> "{:,}".format(1234.5678)
'1,234.5678'
>>> "{:,.2f}".format(1234.5678)
'1,234.57'
logging.debug("size = %s byte", "{:,}".format(vfp_size))
print の自動改行を無効化†
- print("abc","") のように空白文字を行末に付与する
文字列を指定したセパレータで分解する†
- split("セパレータ文字") で要素に分解されてリストに格納される
- split("セパレータ",N) で N 番目まで区切って、以下は分解しない
python で正規表現を使う方法†
import re
- re モジュールには正規表現パターンを使用した検索、置換、連結、分割などのメソッドがある
- これらのメソッドはマッチした文字列を返すものもあれば、MatchObject インスタンスを返すものがある
| group() | マッチした文字列を返す |
| start() | マッチした文字列の開始位置を返す |
| end() | マッチした文字列の終了位置を返す |
| span() | マッチした文字列の (開始位置, 終了位置) のタプルを返す |
SQLite†
- SQLiteのデータ型は5種類しかない
- NULL,
- INTEGER(整数),
- REAL(浮動小数点),
- TEXT(文字列),
- BLOB(バイナリ)
- 表示形式 = .mode
- .mode line
- .mode list
- .mode column
Open CV†
- DJANGO からデーターベースを操作する場合には、専用に用意された記述を用いる。ほぼ SQL 文を書くことは無い
- モデル変更時には以下のコマンドでデータベースに更新を反映する
- python3 manage.py makemigrations
- python3 manage.py migrate
[local] munakata:~/source/python/vlman/vlmanui$ python3 manage.py makemigrations
Migrations for 'fview':
fview/migrations/0001_initial.py
- Create model Filedir
[local] munakata:~/source/python/vlman/vlmanui$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, fview, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying fview.0001_initial... OK
Applying sessions.0001_initial... OK
- プロジェクトルートディレクトリに media ディレクトリーを作成
- settings.py に以下の行を追加
# media file for "pillow"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
- ルート URL ルーティングファイル (urls.py) に以下の行を追加
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
- model に画像を扱うための設定を追加(upload_toを’documents’にしておくことで、/media/documentsに画像が保存される)
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
photo = models.ImageField(upload_to='documents/', default='defo')
uploaded_at = models.DateTimeField(auto_now_add=True)
pylint 関連†
- スライドアスペクト指定
- # set slide aspect = 16:9
- prs.slide_width = 9144000
- prs.slide_height = 5143500
- スライドレイアウトの指定(スライドマスターのどの表示形式を使うか)
- 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
- account = Google account
- 2019/07/04 で有償会員を退会
参考 URL†