** iPython Notebook [#g6ea7e98] 起動方法 $ipython notebook ** iPythonおまじない [#lb7e6868] iPythonとmatplotlibを使ってグラフを描画するときのおまじない。 %matplotlib inline import numpy as np import matplotlib.pyplot as plt ** nbviwer [#h75d005f] .ipynbファイルをgistかgithubにアップロードして置くことで、vbviwerによりwebページとして表示することができる。 #ref(s-nbviewer.jpg,,50%) [[→nbviwer>http://nbviewer.ipython.org/]] ** 時間計測 [#x64e078e] import time start = time.time() print(">> Finish Analys :{0}".format(time.time() - start)) ** pyinstaller [#qde9d4cd] pyinstaller SimpleTextEditor.py --onefile --noconsole + --onefile : .exeを一つのファイルにする + --windowed, -w : コマンドプロンプト非表示 + -i<appicon.icon>, --icon=<appicon.icon> : プログラムのアイコンを設定 + --noconsole : コマンドプロンプト非表示 ** エラーの発行 raise [#ya1a635b] [[→組み込み例外>http://docs.python.jp/2/library/exceptions.html#exceptions.EnvironmentError]] # ファイルが存在しない場合は返却 if not os.path.exists(filepath): # raise StandardError("File is not exist %s" % (filepath)) return try: # ファイルオープン data = wave.open(filepath, 'rb') except IOError: raise StandardError("Cant file load %s" % (filepath)) else: # 正常時の処理 finally: pass ** 例外処理のよい例・悪い例 [#hc442a98] まずは悪い例 def get_status(file): if not os.path.exists(file): print "file not found" sys.exit(1) return open(file).readline() これだと、open(file)の際に出たエラーを捕まえていない。 よい例 def get_status(file): try: return open(file).readline() except EnvironmentError as err: print "Unable to open file: {}".format(err) sys.exit(1) ** LookupError: unknown encoding: windows-31j [#r75276b1] - Pycahrmの設定で、文字コード設定をUTF=8に変更する