オライリー本を全冊購入するといくらなのか?(ワンライナーバージョン)
コチラを見て、頭の体操程度で。
こんなコマンドで
curl -s https://www.oreilly.co.jp/catalog/ | perl -lnE 's/.+"price">([\d,]+).+/$1/ && (tr/,//d, $c++, $s+=$_); END{ say "合計 $c冊 $s円"}'
結果
合計 445冊 1533060円
結構増えてますね。
Reflect.applyとFunction.prototype.applyの速度を計測してみた
ESLintのprefer-reflect
というルールをONにした
最近、Node.js v6が出たのでバージョンアップがてら、ESLintのprefer-reflect
というルールをONにしました。
そして、既存のソースに早速ESLintをかけたところ・・・
警告ががが
既存のソースに早速ESLintをかけたところ func.call(obj, a)
みたいに書いている箇所で警告を受けました。
http://eslint.org/docs/rules/prefer-reflectによれば、下記の古いメソッドはReflect.xxxに置き換えられると。
Reflect.apply effectively deprecates Function.prototype.apply and Function.prototype.call
Reflect.deleteProperty effectively deprecates the delete keyword
Reflect.getOwnPropertyDescriptor effectively deprecates Object.getOwnPropertyDescriptor
Reflect.getPrototypeOf effectively deprecates Object.getPrototypeOf
Reflect.setPrototypeOf effectively deprecates Object.setPrototypeOf
Reflect.preventExtensions effectively deprecates Object.preventExtensions
Functionのcall
やapply
はReflect.apply
に書き換えなさいとのこと。
ESLintのルールに従い、素直に書き換えよう!とは思ったのですが、速度が激遅にならないかが気になりました。
そこで超ざっくり計測
console.time
にてざっくりと。
'use strict'; function log(i) { return String(i); } for (let n = 1; n <= 10; n++) { console.log(`------------${n}回目-----------`); console.time('Function.apply'); for (let i = 0; i < 100000; i++) { log.apply(log, [i]); } console.timeEnd('Function.apply'); console.time('Reflect.apply'); for (let i = 0; i < 100000; i++) { Reflect.apply(log, undefined, [i]); } console.timeEnd('Reflect.apply'); }
結果。
------------1回目----------- Function.apply: 13.736ms Reflect.apply: 11.770ms ------------2回目----------- Function.apply: 13.979ms Reflect.apply: 8.198ms ------------3回目----------- Function.apply: 10.134ms Reflect.apply: 9.899ms ------------4回目----------- Function.apply: 9.800ms Reflect.apply: 11.548ms ------------5回目----------- Function.apply: 11.939ms Reflect.apply: 7.979ms ------------6回目----------- Function.apply: 8.048ms Reflect.apply: 8.330ms ------------7回目----------- Function.apply: 11.209ms Reflect.apply: 12.215ms ------------8回目----------- Function.apply: 11.397ms Reflect.apply: 8.974ms ------------9回目----------- Function.apply: 9.948ms Reflect.apply: 10.000ms ------------10回目----------- Function.apply: 8.923ms Reflect.apply: 11.215ms
結論
遅いということはなさそうなので、安心して使えそう。
Invokeを少し触ってみた
Invokeとは?
Python製のタスクランナーだそう。make, rake, gulpみたいな。
↓みたいにタスクを書いて、invoke clean build
みたいに実行できると。
from invoke import run, task @task def clean(docs=False, bytecode=False, extra=''): patterns = ['build'] if docs: patterns.append('docs/_build') if bytecode: patterns.append('**/*.pyc') if extra: patterns.append(extra) for pattern in patterns: run("rm -rf %s" % pattern) @task def build(docs=False): run("python setup.py build") if docs: run("sphinx-build docs docs/_build")
どこで使われている?
Fabric(https://github.com/fabric/fabric/)で。
http://www.pyinvoke.org/faq.html#why-was-invoke-split-off-from-the-fabric-project
↑を読むと、Fabricが仕事し過ぎなため、ライブラリを分割した模様。
サンプルは?
非常にシンプルで直感的に使えるのですが、事例が少ない。。
日本語ではこちらのサイトくらい www.hexacosa.net
一番いいのはFabricの事例と、作者のタスク集みたいなのを見ること。
実戦で使ってみる?
ちょっと悩む。
makeとかrakeで何とかならんこともないし。
While fully usable, Invoke is still pre-1.0 software and has no backwards compatibility
なので、API変更もあり得るし。
どうしてもPythonで全部統一したい人は使ってみたらよいのではないでしょうか・・・
Google Cloud DNSで設定ファイルをimport/exportする
Cloud Deployment Manager にはGoogle Cloud DNSを設定する機能がないっぽいですが、代わりにgcloud dns
コマンドが使えそうです。
コマンド例
エクスポート
↓のコマンドを打つと
gcloud dns record-sets export -z example-com-zone example-com-zone.yml
↓のようなYAML形式で出力されます。
--- kind: dns#resourceRecordSet name: example.com. rrdatas: - ns-cloud-c1.googledomains.com. - ns-cloud-c2.googledomains.com. - ns-cloud-c3.googledomains.com. - ns-cloud-c4.googledomains.com. ttl: 21600 type: NS --- kind: dns#resourceRecordSet name: example.com. rrdatas: - ns-cloud-c1.googledomains.com. cloud-dns-hostmaster.google.com. 3 21600 3600 1209600 300 ttl: 21600 type: SOA
インポート
↓のコマンドでYAML形式のファイルをインポートできます。
gcloud dns record-sets import --delete-all-existing -z example-com-zone example-com-zone.yml
ポイント
- インポート時には
--delete-all-existing
を指定しないとダメなよう - 書式にエラーがある場合は更新されないので、中途半端な状態にならないかは気にしなくて大丈夫なよう
Slackで絵文字をskin-toneを変えた絵文字全てをクリップボードにコピー
ESLintで拡張子をみてテキストをゴニョゴニョできる話
久しぶりにESLintの話題を。
公式ドキュメントに書いてあるのですが、ESLintプラグインでは拡張子毎にprocessorを設定できるそう。
http://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins
processor is 何?
このeslint-plugin-markdownがわかりやすいかもしれません。
markdownからJavaScriptのコード部分だけを抜き出し、lintできるというプラグインです。
↓のpreprocess
という関数の部分を見ればわかりますが、markdownのテキストからJavaScriptのコード部分だけを抜き出すということをやっています。
https://github.com/eslint/eslint-plugin-markdown/blob/f97e0c01beb7cc6dad21d828ea80e4fbe55317af/lib/processor.js#L43
抜き出したコード部分を配列で返せば、ESLint本体側でそれらをlintしてくれる挙動になっています。
※ちなみにmarkdownをパースする箇所では、昨日の記事で書いたプレゼンテーションツールのremarkが使われていましたw
どんな場合に使えるか?
ぱっとプラグインが見つからなかったですが、HTMLファイルの
<script>〜</script>
の中身のチェックにも使えそう
→ ありました eslint-plugin-html - npm (mysticateaさん、ありがとうございます)テキストの中身をみて、pluginをOFFにするとか (eslint-plugin-disable がまさにそれ)
/* eslint no-var: 0 */
みたいなコメントを一行目に強引につけて、ファイルの中身に応じて、動的にルールをON/OFFする(ES2015とES5が混じっている環境で使いたい)
など。
まとめ
使いたい(使うかも)
Github Pages + Markdownで楽々プレゼン生活
の記事の内容ほぼままなのですが、
自分的にはライフチェインジングな発見だったのでご紹介。
Github Pagesとremarkを組み合わせるとプレゼン資料作成が捗るよというお話です。
Github Pagesとは?
みなさん知っていると思いますが、Github上で、yukidarake.github.io
みたいなリポジトリを作ると、https://yukidarake.github.io/にWebページを公開できるという機能です。
が、上記のようなユーザ/オーガニゼーションだけでなく、gh-pages
ブランチを作成すれば、プロジェクト毎にも作れるそう!(これ知らなかった)
その場合、URLは https://yukidarake.github.io/presentations みたいになります。
詳しくは↓をご覧ください。
remarkとは?
HTML and CSSを使って、ブラウザ上で表示できる(よくありそうな)プレゼンテーション用ツールです。
が、このremarkの良い所は、スライドの内容を別ファイルにmarkdownで書くことができるということ。
さらに、一枚、index.htmlを準備し、パラメータでスライド用ファイルを読み分けるよう細工すれば、都度HTMLやCSSを触る必要ありません!
では、Github Pagesとremarkを組み合わせてみましょう
で、こんな感じになりました。
画像キャプチャ
https://yukidarake.github.io/presentations/?my-first-presentation
TIPS
- Githubのリポジトリの設定より、デフォルトブランチをgh-pagesに変更しておくとよい
- README.mdを各資料へのインデックスページとして利用するとよい https://yukidarake.github.io/presentations/
- ローカルでの動作確認は
python -m SimpleHTTPServer 8000
が便利だった - 絵文字を使うと見栄えが良くなる(気がする)
こんなところで。