iPython Notebook

起動方法

$ipython notebook

iPythonおまじない

iPythonとmatplotlibを使ってグラフを描画するときのおまじない。

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

nbviwer

.ipynbファイルをgistかgithubにアップロードして置くことで、vbviwerによりwebページとして表示することができる。

s-nbviewer.jpg

→nbviwer

時間計測

   import time
   start = time.time()
   print(">>  Finish Analys :{0}".format(time.time() - start))

pyinstaller

pyinstaller SimpleTextEditor.py --onefile --noconsole
  1. --onefile : .exeを一つのファイルにする
  1. --windowed, -w : コマンドプロンプト非表示
  1. -i<appicon.icon>, --icon=<appicon.icon> : プログラムのアイコンを設定
  1. --noconsole : コマンドプロンプト非表示

エラーの発行 raise

→組み込み例外

       # ファイルが存在しない場合は返却
       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

例外処理のよい例・悪い例

まずは悪い例

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

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS