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 : コマンドプロンプト非表示

エラー IOError: [Errno 22] invalid mode ('rb') or filename: ''

 File "C:\Users\fifi\Anaconda\lib\site-packages\PyInstaller\build.py", line 796, in cacheDigest
   data = open(fnm, "rb").read()
IOError: [Errno 22] invalid mode ('rb') or filename: ''

http://codedmi.com/questions/785472/pyinstaller-errno-22

	Well reinstalled pywin32 and now working :S just going go with it
	Just spent the better part of a week tracking this bug down. Was getting this error just by trying to compile a script importing numpy or pandas and printing "hello world".
	Eventually fixed it by running command prompt as administrator... Yeah.
	Hope this helps some poor desperate soul.
	I had the same issues but found these other solutions did not fix the problem. I did however find a fix as follows:
	First, my situation may be a little different to the OP as I'm using the Anaconda Python distribution on Windows 7, and used the conda command line too to install pywin32, and then used pip to install pyinstaller.
	I found the same IOError was preceded by this earlier error message in the pyinstaller output log:
	ImportError: No system module 'pywintypes' (pywintypes27.dll)  
	The solution that fixed both errors was to copy the DLL files:
	pywintypes27.dll
	pythoncom27.dll 
	sitting in: C:\<anaconda-dir>\Lib\site-packages\win32
	to C:\<anaconda-dir>\Lib\site-packages\win32\lib
	Where <anaconda-dir> will either be your root Anaconda directory:
	C:\Users\<username>\AppData\Local\Continuum\Anaconda\ by default,
	or an environment you have set up e.g.
	C:\Users\<username>\AppData\Local\Continuum\Anaconda\envs\<environment-name>
	A came across this answer thanks to Tompa here, who found it solved a similar problem in py2exe.

エラーの発行 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

py2exe

→http://www.py2exe.org/

→py2exe Python スクリプトからスタンドアロンのWindowsプログラムへの変換

→Py2exe 利用ノート

→Pythonで単体で動くバイナリを作ろう!

platformを調べる

################# if PySide not installed inside houdini libs
import platform

if platform.system() == 'Windows':
   sp = 'C:/Python27/Lib/site-packages'
   if not sp in sys.path:
       # if os.path.exists(sp):
       sys.path.insert(0, sp)
       print sp

OpenCV for Python

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