meta要素に関する仕様
そういや真面目にmeta要素の仕様を読んだことなかったので、調べてみました。
meta 要素
お馴染みこんなやつです。
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport"> <meta name="csrf-param" content="authenticity_token">
4.2 Document metadata — HTML5 に定義が書かれています。適当に訳すと...
The meta element represents various kinds of metadata that cannot be expressed using the title, base, link, style, and script elements.
title, base, link, style, script 要素では表現できないメタデータを表現するための要素です。
The meta element can represent document-level metadata with the name attribute, pragma directives with the http-equiv attribute, and the file's character encoding declaration when an HTML document is serialized to string form (e.g. for transmission over the network or for disk storage) with the charset attribute.
name属性でドキュメントレベルのメタデータを、http-quiv属性でプラグマディレクティブを、charset属性で文字エンコーディング宣言を表現します。
name 属性
If a meta element has a name attribute, it sets document metadata. Document metadata is expressed in terms of name-value pairs, the name attribute on the meta element giving the name, and the content attribute on the same element giving the value.
もしmeta要素がname属性を持つならば、meta要素はドキュメントメタデータを設定します。ドキュメントメタデータは name-value として表現され、name属性は名前を、content属性は値を示します。
Standard metadata names
- application-name
- author
- description
- generator
- keywords
Other metadata names
MetaExtensions - WHATWG Wiki を見るのがよいとのこと。 twitter:site, csrf-token など様々なものが定義されています。
MacでMecabをpython3から利用する方法
2016年5月時点で最もカンタンと思われる方法。 python3 はインストール済みという前提で。
インストール
brew install mecab brew install mecab-ipadic pip install mecab-python3
>>> import MeCab >>> m = MeCab.Tagger("-Ochasen") >>> print(m.parse("すもももももももものうち")) すもも スモモ すもも 名詞-一般 も モ も 助詞-係助詞 もも モモ もも 名詞-一般 も モ も 助詞-係助詞 もも モモ もも 名詞-一般 の ノ の 助詞-連体化 うち ウチ うち 名詞-非自立-副詞可能 EOS
natto
名詞、形容詞、形容動詞だけ抜き出す。 natto を使うと便利。
from natto import MeCab def tokenize(text): tokens = [] with MeCab('-F%f[0],%f[6]') as nm: for n in nm.parse(text, as_nodes=True): # ignore any end-of-sentence nodes if not n.is_eos() and n.is_nor(): klass, word = n.feature.split(',', 1) if klass in ['名詞', '形容詞', '形容動詞']: tokens.append(word) return tokens print(tokenize('私の名前は太郎です。')) # ['私', '名前', '太郎']
ファイルの拡張子を一括置換する
例えば
application.css.scss -> application.scss
という一括置換をしたい場合には
find . -type f -print0 | perl -pe 's/\.css\.scss//g' | xargs -0 -I% git mv %.css.scss %.scss
とすれば ok。
参照:
Mac OS X でのpyenvによるpython環境構築 - Tensorflow 編
というのを以前書いたのですが、Tensorflow を始めるにあたっては anaconda を使ったほうが何かと便利なので、メモを残しておきます。
1. pyenv と pyenv-virtualenv のインストール
brew install pyenv brew install pyenv-virtualenv
2.bashrcの設定
# pyenv [[ -s "$HOME/.pyenv" ]] && export PYENV_ROOT="$HOME/.pyenv" && export PATH="$PYENV_ROOT/bin:$PATH" && if which pyenv > /dev/null; then eval "$(pyenv init -)"; eval "$(pyenv virtualenv-init -)"; fi
3. pyenv で anaconda をインストール
pyenv install anaconda3-2.5.0 pyenv local anaconda3-2.5.0
4. Tensorflow をインストール
https://www.tensorflow.org/versions/master/get_started/os_setup.html#download-and-setup
pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py3-none-any.whl
setuptools に起因してうまくインストールできないケースがある。 その際は、 osx 10.11 installation issues · Issue #135 · tensorflow/tensorflow · GitHub を参照のこと。
anaconda には numpy 等、数値計算に必要なライブラリはインストール済みなので、これで Tensorflow 入門ができる。
unicornのlog rotationでハマったが、実は test コマンドの exit コード起因だったという話
unicorn の log を cron で毎日ローテートする設定を書いたところ、下記エラーメールが届いて「なぜだろう」と数時間唸ってました。ようやく解決に至ったので、メモしておきます。
メール
件名:
Cron <root@***> test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
本文:
/etc/cron.daily/logrotate: error: error running last action script for /path/to/unicorn/log/*.log run-parts: /etc/cron.daily/logrotate exited with return code 1
設定
/etc/logrotate.d に配備した設定ファイル内容は以下の通りです。
/path/to/unicorn/log/*.log { daily missingok rotate 90 ... lastaction pid=/path/to/unicorn.pid test -s $pid && kill -USR1 "$(cat $pid)" endscript }
原因
ログロテーションは正常に完了しており、"run-parts: /etc/cron.daily/logrotate exited with return code 1" なので last action script に問題がある模様です。
「test -s して存在確認してから実行してるし...何がいけないのかサッパリわからん!」 と思っていたのですが、この test の return code こそが原因でした。
DESCRIPTION Exit with the status determined by EXPRESSION. --help display this help and exit --version output version information and exit An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit status.
となっており、test コマンドの成否によって、exit status が設定されるとのこと。
$ test -s /tmp/not_found_file $ echo $? 1
実際に試してみると、存在しないファイルについては確かに 1 になるようです。。
test -s $pid が false になるのは unicorn を稼働させてなかったからで、これで理屈が通りました。
irisデータをロジスティック回帰とSVMで分類してみた
Coursera の Machine Learning の演習は、コスト関数とその最小化を自前で実装することになるのでなかなか大変だけど、python の sklearn を使うと交差検定含め、ものすごくカンタンに書ける。
import numpy as np import sklearn.datasets as datasets from sklearn.linear_model import LogisticRegression from sklearn import svm from sklearn.cross_validation import cross_val_score iris = datasets.load_iris() logistic = LogisticRegression() # Logistic Regression scores = cross_val_score(logistic, iris.data, iris.target, cv=5) print("logistic regression", np.mean(scores)) # SVM(kernel:rbf) scores = cross_val_score(svm.SVC(kernel='rbf'), iris.data, iris.target, cv=5) print("svm(rbf): ", np.mean(scores)) # SVM(kernel:linear) scores = cross_val_score(svm.SVC(kernel='linear'), iris.data, iris.target, cv=5) print("svm(linear): ", np.mean(scores))
logistic regression 0.96 svm(rbf): 0.98 svm(linear): 0.98
カンタンに書けるが故に、基礎理論をきちんと理解し、思うように結果が出ないときに原因分析できる力が必要だと感じます。 月並みですが。
nginx try_filesディレクティブ
nginx の try_files ディレクティブについての備忘録。
Syntax
Syntax: try_files file ... uri; try_files file ... =code; Default: — Context: server, location
書かれていることを箇条書きでまとめると...
- 指定された順序で "file" の存在をチェックし(つまり複数設定できる)、最初に見つかったものを処理する。
- "file" への path は root と alias ディレクティブに従う
- 何も見つからない場合は、最後に指定した uri に内部リダイレクトする
例1
設定
server { root /usr/share/nginx/html; location /images/ { try_files $uri /images/default.gif; } location = /images/default.gif { expires 30s; } }
ディレクトリ
$ tree /usr/share/nginx/html/ /usr/share/nginx/html/ |-- 50x.html |-- images | |-- default.gif | `-- hello.gif `-- index.html
という場合に、リクエストとレスポンスの対応は以下のとおり。
リクエスト | レスポンス | 理由 |
---|---|---|
http://example.com/images/hello.gif | /images/hello.gif | $uri は /images/hello.gif となり、対象ファイルが存在するため。 |
http://example.com/images/goodbye.gif | /images/default.gif | $uri は /images/goodbye.gif となり、対象ファイルが存在しない。このため、default.gif にフォールバックする。 |
例2
server { root /usr/share/nginx/html; location / { try_files $uri $uri/index.html $uri.html =404; } }
http://example.com/hello をリクエストした場合、nginx は root(/usr/share/nginx/html)を起点に /hello, /hello/index.html, /hello.html を順に探し、見つからない場合には 404 エラーページを返す。
例3
location / { try_files /system/maintenance.html $uri $uri/index.html $uri.html @mongrel; } location @mongrel { proxy_pass http://mongrel; }
この例では、ファイルが見つからない場合は、@mongrel という名前付ロケーションにフォールバックする。