ページ更新: 2016-02-01 (月) (1265日前)

(2010-04-21 新規作成)

目次

[編集]

Jupyter サーバー設定メモ (2016-02-01) #

Jupyter を他のPCから接続できるようにした。

設定ファイル jupyter_notebook_config.pyを生成する:

D:\jupyter> jupyter notebook --generate-config
Writing default config to: C:\Users\USERNAME\.jupyter\jupyter_notebook_config.py

jupyter_notebook_config.py を最低限設定する。どのIPで待ち受けるかと、起動時にWebブラウザで開くかどうかを設定する(Falseにする):

c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False

(必須では無いが)サーバに接続するときに使うパスワードを生成する:

D:> py
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from notebook.auth import passwd
>>> passwd()
Enter password:
Verify password:
'sha1:..(略)..'
>>>

パスワードは、jupyter_notebook_config.py の以下の行に設定する:

c.NotebookApp.password = u'sha1:..(略)..'

(必須では無いが)通信を暗号化するために、SSLの鍵ペアを生成する。Cygwinを使った:

$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mykey.key -out mycert.pem
Generating a 1024 bit RSA private key
(略)

鍵は適当な場所に置き、jupyter_notebook_config.py の以下の2行に設定する:

c.NotebookApp.certfile = u'C:/..(略)../mycert.pem'
c.NotebookApp.keyfile = u'C:/..(略)../mykey.key'

また、鍵を使うときは、サーバーを起動するときに、コマンドラインで指定する必要がある:

jupyter notebook --certfile=C:/..(略)../mycert.pem --keyfile C:/..(略)../mykey.key

以上の設定を行うと、jupyter_notebook_config.py は以下のようになる:

c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.certfile = u'C:/..(略)../.jupyter/mycert.pem'
c.NotebookApp.keyfile = u'C:/..(略)../.jupyter/mykey.key'
c.NotebookApp.password = u'sha1:..(略)..'

SSLを用いた場合、https://サーバ:8888/tree をWebブラウザで開く。

SSLを用いない場合、http://サーバ:8888/tree をWebブラウザで開く。

[編集]

Jupyter インストールメモ (2016-01-21) #

Python 2.7.10, Windows 7。

C:> pip install jupyter
Collecting jupyter
  Downloading jupyter-1.0.0-py2.py3-none-any.whl
Collecting notebook (from jupyter)
  Downloading notebook-4.1.0-py2.py3-none-any.whl (5.7MB)
    100% |################################| 5.7MB 86kB/s
Collecting ipywidgets (from jupyter)
  Downloading ipywidgets-4.1.1-py2.py3-none-any.whl (117kB)
    100% |################################| 118kB 2.2MB/s
Collecting nbconvert (from jupyter)
  Downloading nbconvert-4.1.0-py2.py3-none-any.whl (281kB)
    100% |################################| 282kB 660kB/s
Collecting ipykernel (from jupyter)
  Downloading ipykernel-4.2.2-py2.py3-none-any.whl (91kB)
    100% |################################| 94kB 1.6MB/s
Collecting jupyter-console (from jupyter)
  Downloading jupyter_console-4.1.0-py2.py3-none-any.whl
Collecting qtconsole (from jupyter)
  Downloading qtconsole-4.1.1-py2.py3-none-any.whl (98kB)
    100% |################################| 102kB 2.0MB/s
Collecting traitlets (from notebook->jupyter)
  Downloading traitlets-4.1.0-py2.py3-none-any.whl (65kB)
    100% |################################| 69kB 2.4MB/s
Requirement already satisfied (use --upgrade to upgrade): tornado>=4 in c:\pytho
n27\lib\site-packages (from notebook->jupyter)
Collecting jupyter-client (from notebook->jupyter)
  Downloading jupyter_client-4.1.1-py2.py3-none-any.whl (70kB)
    100% |################################| 73kB 2.4MB/s
Collecting nbformat (from notebook->jupyter)
  Downloading nbformat-4.0.1-py2.py3-none-any.whl (138kB)
    100% |################################| 139kB 1.9MB/s
Collecting jupyter-core (from notebook->jupyter)
  Downloading jupyter_core-4.0.6-py2.py3-none-any.whl (74kB)
    100% |################################| 77kB 2.6MB/s
Collecting ipython-genutils (from notebook->jupyter)
  Downloading ipython_genutils-0.1.0-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): jinja2 in c:\python27\
lib\site-packages (from notebook->jupyter)
Collecting ipython>=4.0.0dev0 (from ipywidgets->jupyter)
  Downloading ipython-4.0.3-py2-none-any.whl (730kB)
    100% |################################| 733kB 650kB/s
Collecting pygments (from nbconvert->jupyter)
  Downloading Pygments-2.1-py2-none-any.whl (754kB)
    100% |################################| 757kB 640kB/s
Collecting mistune!=0.6 (from nbconvert->jupyter)
  Downloading mistune-0.7.1-py2.py3-none-any.whl
Collecting pyreadline (from jupyter-console->jupyter)
  Downloading pyreadline-2.1.zip (109kB)
    100% |################################| 110kB 3.2MB/s
Collecting decorator (from traitlets->notebook->jupyter)
  Downloading decorator-4.0.6-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): backports.ssl-match-ho
stname in c:\python27\lib\site-packages (from tornado>=4->notebook->jupyter)
Requirement already satisfied (use --upgrade to upgrade): certifi in c:\python27
\lib\site-packages (from tornado>=4->notebook->jupyter)
Requirement already satisfied (use --upgrade to upgrade): pyzmq>=13 in c:\python
27\lib\site-packages (from jupyter-client->notebook->jupyter)
Collecting jsonschema!=2.5.0,>=2.0 (from nbformat->notebook->jupyter)
  Downloading jsonschema-2.5.1-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): markupsafe in c:\pytho
n27\lib\site-packages (from jinja2->notebook->jupyter)
Collecting simplegeneric>0.8 (from ipython>=4.0.0dev0->ipywidgets->jupyter)
  Downloading simplegeneric-0.8.1.zip
Collecting pickleshare (from ipython>=4.0.0dev0->ipywidgets->jupyter)
  Downloading pickleshare-0.6-py2.py3-none-any.whl
Collecting functools32 (from jsonschema!=2.5.0,>=2.0->nbformat->notebook->jupyte
r)
  Downloading functools32-3.2.3-2.tar.gz
Collecting path.py>=6.2 (from pickleshare->ipython>=4.0.0dev0->ipywidgets->jupyt
er)
  Downloading path.py-8.1.2-py2.py3-none-any.whl
Installing collected packages: decorator, ipython-genutils, traitlets, jupyter-c
ore, jupyter-client, functools32, jsonschema, nbformat, pygments, mistune, nbcon
vert, simplegeneric, path.py, pickleshare, ipython, ipykernel, notebook, ipywidg
ets, pyreadline, jupyter-console, qtconsole, jupyter
  Running setup.py install for functools32
  Running setup.py install for simplegeneric
  Running setup.py install for pyreadline
Successfully installed decorator-4.0.6 functools32-3.2.3.post2 ipykernel-4.2.2 i
python-4.0.3 ipython-genutils-0.1.0 ipywidgets-4.1.1 jsonschema-2.5.1 jupyter-1.
0.0 jupyter-client-4.1.1 jupyter-console-4.1.0 jupyter-core-4.0.6 mistune-0.7.1
nbconvert-4.1.0 nbformat-4.0.1 notebook-4.1.0 path.py-8.1.2 pickleshare-0.6 pygm
ents-2.1 pyreadline-2.1 qtconsole-4.1.1 simplegeneric-0.8.1 traitlets-4.1.0
You are using pip version 7.1.2, however version 8.0.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' comm
and.

以下の場所にイストールされたようだ。

C:> c:\Gnuwin32\bin\which jupyter
C:> dir /b C:\Python27\Scripts\ju*
jupyter-console.exe
jupyter-kernelspec.exe
jupyter-migrate.exe
jupyter-nbconvert.exe
jupyter-nbextension.exe
jupyter-notebook.exe
jupyter-qtconsole.exe
jupyter-trust.exe
jupyter.exe

起動する。念のために作業ディレクトリを作って、そこで起動させた。

D:\> mkdir D:\jupypter
D:\> cd juipter
D:\jupyter> jupyter notebook
[I 00:45:34.367 NotebookApp] Writing notebook server cookie secret to C:\Users\u
sername\AppData\Roaming\jupyter\runtime\notebook_cookie_secret
[I 00:45:34.740 NotebookApp] Serving notebooks from local directory: D:\jupyter
[I 00:45:34.740 NotebookApp] 0 active kernels
[I 00:45:34.740 NotebookApp] The Jupyter Notebook is running at: http://localhos
t:8888/
[I 00:45:34.740 NotebookApp] Use Control-C to stop this server and shut down all
 kernels (twice to skip confirmation).

Webブラウサに http://localhost:8888/tree が表示された。

[編集]

Python 3.4 の Lib/statistics.py のバックポート? (2016-01-20) #

Python 2.7.10 にインストールしてみた:

C:> pip install backports.statistics
Collecting backports.statistics
  Downloading backports.statistics-0.1.0-py2.py3-none-any.whl
Installing collected packages: backports.statistics
Successfully installed backports.statistics-0.1.0

import してエラーが出ないので、これでよいはず:

C:> python
(略)
>>> from backports import statistics
>>> dir(statistics)
['Counter', 'Decimal', 'Fraction', 'StatisticsError', '__all__', '__builtins__',
 '__doc__', '__file__', '__name__', '__package__', '__path__', '_coerce_types',
'_counts', '_decimal_to_ratio', '_exact_ratio', '_ss', '_sum', 'division', 'isfinite',
'math', 'mean', 'median', 'median_grouped', 'median_high', 'median_low',
'mode', 'pstdev', 'pvariance', 'range', 'stdev', 'sys', 'variance']
>>>
[編集]

gawk っぽい処理 #

[編集]

「#define シンボル名」を抽出して出力する (2014-07-01) #

gawk

gawk "/#.*define/ {print $1, $2}" *.h

python (2.7?):

# coding: utf-8

import sys
import glob
import fileinput
import re

filelist = []
for f in sys.argv[1:]:
    g = glob.glob(f)
    if len(g):
        filelist.extend(g)

for line in fileinput.input(filelist):
    a = line.split()
    if (len(a) > 1) and re.match("#.*define", a[0]):
        print a[0], a[1]
[編集]

optparse #

(2010-04-21, Python 2.6.4)

# -*- coding: UTF-8 -*-

from optparse import OptionParser

def doProcess():
    parser = OptionParser()
##    args = ['infile.tif', 'outfile.tif']
    args = ['-h']
    (option, args) = parser.parse_args(args)
    print "option=", option
    print "args=", args

## main
if __name__ == '__main__':
    doProcess()

実行例 (args = ['-h']):

Usage: sample.py [options]

Options:
  -h, --help  show this help message and exit
Exit code:  0

実行例 (args = ['infile.tif', 'outfile.tif']):

option= {}
args= ['infile.tif', 'outfile.tif']