A Tour of Go "moretypes/23"

package main

import (
	"fmt"
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	fmt.Println(strings.Fields(s))
	var m = map[string]int{}
	for _, v := range strings.Split(s, " ") {
		_, exist := m[v]
		if exist {
			m[v] += 1
		} else {
			m[v] = 1
		}
	}

	return m
}

func main() {
	wc.Test(WordCount)
}

/usr/local/Cellar/go/1.8.1/libexec/bin/go run /Users/seijiro/Dropbox/code/GoglandProjects/first/10.go
[I am learning Go!]
PASS
f("I am learning Go!") =
map[string]int{"I":1, "am":1, "learning":1, "Go!":1}
[The quick brown fox jumped over the lazy dog.]
PASS
f("The quick brown fox jumped over the lazy dog.") =
map[string]int{"fox":1, "lazy":1, "dog.":1, "The":1, "quick":1, "brown":1, "jumped":1, "over":1, "the":1}
[I ate a donut. Then I ate another donut.]
PASS
f("I ate a donut. Then I ate another donut.") =
map[string]int{"another":1, "I":2, "ate":2, "a":1, "donut.":2, "Then":1}
[A man a plan a canal panama.]
PASS
f("A man a plan a canal panama.") =
map[string]int{"man":1, "a":2, "plan":1, "canal":1, "panama.":1, "A":1}

Process finished with exit code 0

XMLパースはPython速い。Ruby糞遅い。でもRubyにも希望はある。それはOx!

qiita.com

rubyでも試してみた。

結論

rubyは糞遅い。この差はやばいね。pure rubyだと100倍違う。nokogiriでも5秒かかるとか。
これだけ見るとrubyはもうダメだ、という結論になってしまうね。

<追記>

悔しいじゃないですかぁー。いろいろためしましたよー。
Oxというのが良いカンジ。用途によってはNokogiriより使いやすい。速い。libxmlに依存していないらしい。インストールも速い。
遅いPythonぐらいまでは来ている。Oxで良いじゃんとなる。
www.reddit.com
ライブラリを選ぶときは用途とかパフォーマンスを調べる必要がありますね。特に車輪の再開発が多いRubyでは。今回Oxというライブラリを初めて使い、今まではXMLパーサーはNokogiriしか考えたことなかったけど、ちょっと態度を改めるべきですね。っていうかNokogiriはインストールあんなに遅いし、よくインストール失敗するし、なんでRubyデファクトスタンダードに居座っているのかしら?高機能なの?そんなみんな使いこなしているのかしら?

python

parse時間: 0.16052603721618652秒

rexml

parse時間: 16.04039192199707

nokogiri(Slop)

parse時間: 4.752880811691284

nokogiri(XML)

parse時間: 3.484325885772705
XMLにしたらいくらか早くなったけどpythonとの差は絶望感しか無い。

Ox

parse時間: 0.2525479793548584
おっと良いカンジ。

Oga

parse時間: 3.470038890838623

Oga(IO渡し)

parse時間: 3.2525930404663086

pure rubyのライブラリrexmlでのソース

#! /usr/bin/env ruby
# coding: utf-8
# frozen_string_literal: true

require 'rexml/document'

class ImageInfo
  attr_accessor :url, :width, :height
end

class BookInfo
  attr_accessor :asin, :title, :binding, :author, :publisher, :publicationDate, :images
  def initialize
    @images = {}
  end
end

def get_text(dom, tag)
  if dom.elements[tag]
    dom.elements[tag].text
  else
    ''
  end
end

def parse_xmls(xmls)
  bookinfos = []
  xmls.each do |xmlstring|
    doc = REXML::Document.new(xmlstring)
    image_labels = %w(SmallImage MediumImage LargeImage)
    doc.elements.each('ItemLookupResponse/Items/Item') do |item|
      bookinfo = BookInfo.new
      bookinfo.asin = item.elements['ASIN'].text
      attr = item.elements['ItemAttributes']
      bookinfo.title = get_text(attr, 'Title')
      bookinfo.binding = get_text(attr, 'Binding')
      bookinfo.author = get_text(attr, 'Author')
      bookinfo.publisher = get_text(attr, 'Publisher')
      bookinfo.publicationDate = get_text(attr, 'PublicationDate')

      image_labels.each do |image_label|
        next unless item.elements[image_label]
        image = ImageInfo.new
        imgtag = item.elements[image_label]
        image.url = imgtag.elements['URL'].text
        image.width = imgtag.elements['Width'].text.to_i
        image.height = imgtag.elements['Height'].text.to_i
        bookinfo.images[image_label] = image
      end
      bookinfos << bookinfo
    end
  end
  bookinfos
end

def get_xmls
  xmls = []
  Dir.glob('xmls/*.xml').each do |file|
    open(file, 'r') do |io|
      xmls << io.read
    end
  end
  xmls
end

xmls = get_xmls
t = Time.now
bookinfos = parse_xmls(xmls)
duration = (Time.now.to_f - t.to_f)
puts "xml数: #{xmls.size}"
puts "book数: #{bookinfos.size}"
puts "parse時間: #{duration}"

nokogiri

#! /usr/bin/env ruby
# coding: utf-8
# frozen_string_literal: true

# require 'rexml/document'
require 'nokogiri'

class ImageInfo
  attr_accessor :url, :width, :height
end

class BookInfo
  attr_accessor :asin, :title, :binding, :author, :publisher, :publicationDate, :images
  def initialize
    @images = {}
  end
end

def get_text(dom, tag)
  if dom.css(tag)
    dom.css(tag).text
  else
    ''
  end
end

def parse_xmls(xmls)
  bookinfos = []
  xmls.each do |xmlstring|
    doc = Nokogiri::Slop(xmlstring)
    image_labels = %w[SmallImage MediumImage LargeImage]
    doc.css('Item').each do |item|
      bookinfo = BookInfo.new
      bookinfo.asin = item.css('ASIN').text
      attr = item.css('ItemAttributes')
      bookinfo.title = get_text(attr, 'Title')
      bookinfo.binding = get_text(attr, 'Binding')
      bookinfo.author = get_text(attr, 'Author')
      bookinfo.publisher = get_text(attr, 'Publisher')
      bookinfo.publicationDate = get_text(attr, 'PublicationDate')

      image_labels.each do |image_label|
        next unless item.css(image_label)
        image = ImageInfo.new
        imgtag = item.css(image_label)
        image.url = imgtag.css('URL').text
        image.width = imgtag.css('Width').text.to_i
        image.height = imgtag.css('Height').text.to_i
        bookinfo.images[image_label] = image
      end
      bookinfos << bookinfo
    end
  end
  bookinfos
end

def get_xmls
  xmls = []
  Dir.glob('xmls/*.xml').each do |file|
    open(file, 'r') do |io|
      xmls << io.read
    end
  end
  xmls
end

xmls = get_xmls
t = Time.now
bookinfos = parse_xmls(xmls)
duration = (Time.now.to_f - t.to_f)
puts "xml数: #{xmls.size}"
puts "book数: #{bookinfos.size}"
puts "parse時間: #{duration}"

nokogiri(XML+追い込み)

#! /usr/bin/env ruby
# coding: utf-8
# frozen_string_literal: false

# require 'rexml/document'
require 'nokogiri'

class ImageInfo
  attr_accessor :url, :width, :height
end

class BookInfo
  attr_accessor :asin, :title, :binding, :author, :publisher, :publicationDate, :images
  def initialize
    @images = {}
  end
end

def get_text(dom, tag)
  if dom.css(tag).size.positive?
    dom.css(tag).text
  else
    ''
  end
end

def parse_xmls(xmls)
  bookinfos = []
  xmls.each do |xmlstring|
    doc = Nokogiri::XML(xmlstring)
    image_labels = %w[SmallImage MediumImage LargeImage]
    doc.css('Item').each do |item|
      bookinfo = BookInfo.new
      bookinfo.asin = item.css('ASIN').text
      attr = item.css('ItemAttributes')
      bookinfo.title = get_text(attr, 'Title')
      bookinfo.binding = get_text(attr, 'Binding')
      bookinfo.author = get_text(attr, 'Author')
      bookinfo.publisher = get_text(attr, 'Publisher')
      bookinfo.publicationDate = get_text(attr, 'PublicationDate')

      image_labels.each do |image_label|
        next unless item.css(image_label).size.positive?
        image = ImageInfo.new
        imgtag = item.css(image_label)[0]
        image.url = imgtag.css('URL').text
        image.width = imgtag.css('Width').text.to_i
        image.height = imgtag.css('Height').text.to_i
        bookinfo.images[image_label] = image
      end
      bookinfos << bookinfo
    end
  end
  bookinfos
end

def get_xmls
  xmls = []
  Dir.glob('xmls/*.xml').each do |file|
    open(file, 'r') do |io|
      xmls << io.read
    end
  end
  xmls
end

xmls = get_xmls
t = Time.now
bookinfos = parse_xmls(xmls)
duration = (Time.now.to_f - t.to_f)
puts "xml数: #{xmls.size}"
puts "book数: #{bookinfos.size}"
puts "parse時間: #{duration}"

Ox

#! /usr/bin/env ruby
# coding: utf-8
# frozen_string_literal: false

# require 'rexml/document'
# require 'nokogiri'
require 'ox'

class ImageInfo
  attr_accessor :url, :width, :height
end

class BookInfo
  attr_accessor :asin, :title, :binding, :author, :publisher, :publicationDate, :images
  def initialize
    @images = {}
  end
end

def get_text(dom, tag)
  if dom.locate(tag).size.positive?
    dom.locate(tag)[0].text
  else
    ''
  end
end

def parse_xmls(xmls)
  bookinfos = []
  xmls.each do |xmlstring|
    doc = Ox.parse(xmlstring)
    image_labels = %w[SmallImage MediumImage LargeImage]
    doc.locate('ItemLookupResponse/Items/Item').each do |item|
      bookinfo = BookInfo.new
      bookinfo.asin = item.ASIN.text
      attr = item.ItemAttributes
      bookinfo.title = get_text(attr, 'Title')
      bookinfo.binding = get_text(attr, 'Binding')
      bookinfo.author = get_text(attr, 'Author')
      bookinfo.publisher = get_text(attr, 'Publisher')
      bookinfo.publicationDate = get_text(attr, 'PublicationDate')

      image_labels.each do |image_label|
        next unless item.locate(image_label).size.positive?
        image = ImageInfo.new
        imgtag = item.locate(image_label)[0]
        image.url = imgtag.URL.text
        image.width = imgtag.Width.text.to_i
        image.height = imgtag.Height.text.to_i
        bookinfo.images[image_label] = image
      end
      bookinfos << bookinfo
    end
  end
  bookinfos
end

def get_xmls
  xmls = []
  Dir.glob('xmls/*.xml').each do |file|
    open(file, 'r') do |io|
      xmls << io.read
    end
  end
  xmls
end

xmls = get_xmls
t = Time.now
bookinfos = parse_xmls(xmls)
duration = (Time.now.to_f - t.to_f)
puts "xml数: #{xmls.size}"
puts "book数: #{bookinfos.size}"
puts "parse時間: #{duration}"

oga

#! /usr/bin/env ruby
# coding: utf-8
# frozen_string_literal: false

# require 'rexml/document'
# require 'nokogiri'
# require 'ox'
require 'oga'

class ImageInfo
  attr_accessor :url, :width, :height
end

class BookInfo
  attr_accessor :asin, :title, :binding, :author, :publisher, :publicationDate, :images
  def initialize
    @images = {}
  end
end

def get_text(dom, tag)
  if dom.xpath(tag).size.positive?
    dom.xpath(tag)[0].text
  else
    ''
  end
end

def parse_xmls(xmls)
  bookinfos = []
  xmls.each do |xmlstring|
    doc = Oga.parse_xml(xmlstring)
    image_labels = %w[SmallImage MediumImage LargeImage]
    doc.xpath('ItemLookupResponse/Items/Item').each do |item|
      bookinfo = BookInfo.new
      bookinfo.asin = item.xpath('ASIN').text
      bookinfo.title = get_text(item, 'ItemAttributes/Title')
      bookinfo.binding = get_text(item, 'ItemAttributes/Binding')
      bookinfo.author = get_text(item, 'ItemAttributes/Author')
      bookinfo.publisher = get_text(item, 'ItemAttributes/Publisher')
      bookinfo.publicationDate = get_text(item, 'ItemAttributes/PublicationDate')

      image_labels.each do |image_label|
        next unless item.xpath(image_label).size.positive?
        image = ImageInfo.new
        imgtag = item.xpath(image_label)[0]
        image.url = imgtag.xpath('URL').text
        image.width = imgtag.xpath('Width').text.to_i
        image.height = imgtag.xpath('Height').text.to_i
        bookinfo.images[image_label] = image
      end
      bookinfos << bookinfo
    end
  end
  bookinfos
end

def get_xmls
  xmls = []
  Dir.glob('xmls/*.xml').each do |file|
    open(file, 'r') do |io|
      xmls << io.read
    end
  end
  xmls
end

xmls = get_xmls
t = Time.now
bookinfos = parse_xmls(xmls)
duration = (Time.now.to_f - t.to_f)
puts "xml数: #{xmls.size}"
puts "book数: #{bookinfos.size}"
puts "parse時間: #{duration}"
oga(IO渡し)
def get_xmls
  xmls = []
  Dir.glob('xmls/*.xml').each do |file|
    xmls << open(file, 'r')
  end
  xmls
end

バンドのデモ音源をpodcast配信することにした

新曲デモ追加しましたよーのメールとかめんどくさい。
指定ディレクトリ内のmp3ファイルから必要最低限のPodcast用RSSを生成する · GitHub
を参考にちょっと変えてpodcast配信することにしました。
バンド活動が多分ちょっと楽になる。


  • 引数でフレキシブルに動く機能とかいらない。
  • サブディレクトリにmp3が存在する状態だったので変更
  • rubocop/ ruby2.3準拠

shで起動してcronで更新

#! /bin/bash

export PATH="/home/hoge/.rvm/bin:$PATH" # Add RVM to PATH for scripting
source /home/seijiro/.rvm/environments/ruby-2.3.0

cd /path/to/makepodcast/

ruby make_podcast.rb > /var/www/chiaki/demo.rss

github.com


まったく需要ないけど一応rssファイルのurl貼っておくー
https://lovesaemi.daemon.asia/chiaki/demo.rss

google日本語入力のアニメ辞書をDocker無しで動かしてみる(Pythonの知識ほぼゼロ)

honeshabri.hatenablog.com
元ネタはここです。すごいですね。スターつけちゃう仕事ですね。
ただ、cronで定期的に回したかったですね。
流行りのDockerでやる方法が紹介されています。
Dockerだと自動で動かす方法がわからなかったのでDocker無しでやる方法を模索しました。

git clone

git clone https://github.com/anilogia/animedb.git

python

2.7系なんですねー。pyenvを導入
qiita.com

git clone https://github.com/yyuu/pyenv.git ~/.pyenv

~/.bashrcやら~/.zshrcやらに書き込んで(そのままですけど)

export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"

docker-compose.ymlを読むと2.7.12なので

pyenv install 2.7.12

じっと待つ。
git cloneしたanimedbにcdして

pyenv local 2.7.12

すると.python-versionというファイルが出来がるので

source ~/.zshrc
python --version # 2.7.12

でうまく2.7.12になりました。

cronで回したいスクリプトを用意

あとはスクリプトを用意すれば良いですねー

#! /bin/bash
export PYTHONIOENCODING=UTF-8
# change
cd /path/to/animedb
git pull
pip install -r requirements.txt --user --upgrade
./animedb list --format google_ime > output.txt

以上です。
Docker使ってアイソレイトとかしたいけどコレぐらい汚れても良いよね。

How to serve Mastodon via h2o web server(Mastodonをh2oで配信する方法)

h2o is great http/2 web server(MIT license).
http/2 protocol leads very good performance with page with many files.
I love h2o server!
I will write about how to serve Mastodon via h2o.

h2oは素晴らしいhttp/2対応のwebサーバーです(MIT ライセンス)。
http/2はたくさんのファイル読み込みがあるページなどで良いパフォーマンスを発揮します。
h2oが好きだー!
h2oでMastodonを配信する方法を記述します。

environment(環境)

server: ubuntu16.04
Front web server is h2o,backend server are apache2.4 and masdon server(docker).
フロントはh2o。バックエンドはapache2.4とdockerのmastodonです

h2o ----   apache2 (domain1) port:81
    |
    ----   docker server(domain2 - mastodon) port:3000,4000

h2o config

very simple,but "//api/v1/streaming/" is little complex.why "/api/v1/streaming/" ? I think this is mastodon's problem but I solved.
とてもかんたんですね。"//api/v1/streaming/"のところがちょっときもいですけど、まぁ、なんとか。

user: www-data
hosts:
  "lovesaemi.daemon.asia":
    listen:
      port: 80
    paths:
      "/":
        redirect: "https://lovesaemi.daemon.asia"
  "lovesaemi.daemon.asia:443":
     listen:
       port: 443
       ssl:
          certificate-file: /etc/letsencrypt/live/lovesaemi.daemon.asia/fullchain.pem
          key-file: /etc/letsencrypt/live/lovesaemi.daemon.asia/privkey.pem
          cipher-suite: "EECDH+AESGCM:AES256+EECDH:AES128+EECDH"
          cipher-preference: server
          minimum-version: TLSv1.2
     paths:
       "/":
         proxy.reverse.url: http://127.0.0.1:81/
  "mastdn.lovesaemi.daemon.asia":
    listen:
      port: 80
    paths:
      "/":
        redirect: "https://mastdn.lovesaemi.daemon.asia"
  "mastdn.lovesaemi.daemon.asia:443":
     listen:
       port: 443
       ssl:
          certificate-file: /etc/letsencrypt/live/mastdn.lovesaemi.daemon.asia/fullchain.pem
          key-file: /etc/letsencrypt/live/mastdn.lovesaemi.daemon.asia/privkey.pem
          cipher-suite: "EECDH+AESGCM:AES256+EECDH:AES128+EECDH"
          cipher-preference: server
          minimum-version: TLSv1.2
     paths:
       "//api/v1/streaming/":
         proxy.websocket: ON
         proxy.preserve-host: ON
         proxy.reverse.url: http://127.0.0.1:4000/
       "/":
         proxy.preserve-host: ON
         proxy.reverse.url: http://127.0.0.1:3000/
access-log: /var/log/h2o/access-log
error-log: /var/log/h2o/error-log
pid-file: /tmp/h2o.pid

check http/2 ? http/2で動いてる?

f:id:new_pill:20170430003959p:plain
f:id:new_pill:20170430003857p:plain
OK,http/2
http/2ですね。良い感じ。

enjoy!
是非h2oを使ってみてください。

Mastodonの画像消すブックマークレット

javascript:(function(){
  var a=document.createElement("script");
  a.src="https://j.mp/1bPoAXq";
  document.head.appendChild(a);
  setInterval(
      function(){
         $("a[style^='background']").parent().parent().css("display","none");}
      ,100);
  }
)();

Mastodonでrake task avatarとheader再取得

DRYにしろといわれたのでやってみました。
send祭り。ちょっと動的すぎるかね。
rubocopとcodeclimateの解析がちがうのでウガーってなったし。
CIでyarnが503だして失敗してウガーってなった。
本当に取り込まれたい。

gistb415ac44f80294bdf199008b2f2b974f