Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View shrkw's full-sized avatar

Hiroyuki Shirakawa shrkw

  • Nagano, Japan
View GitHub Profile
@shrkw
shrkw / postgresql_function_for_serialnumber.sql
Created August 7, 2014 14:19
two PL/pgSQL functions for PostgreSQL. I wanted to use those functions for serial number on X.509 certificates, but I noticed serial number may be over the capacity of bigint, then gave up and stored serial number as hexadecimal format on varchar.
-- from decimal biginteger to hexdecimal text with any separator
CREATE OR REPLACE FUNCTION serialnumber_hex(serial bigint, sep varchar) RETURNS varchar AS $$
DECLARE
org varchar := '';
res varchar := '';
BEGIN
org := to_hex(serial);
WHILE 0 < length(org) LOOP
res := concat_ws(sep, res, substr(org, 1, 2));
org := substr(org, 3, length(org));
@shrkw
shrkw / file0.scala
Created August 27, 2014 20:43
MaxMindが提供しているGeoLite2をScalaから利用してみる ref: http://qiita.com/shrkw/items/3d98dd7e266932feab21
import java.io.File
import java.net.InetAddress
import com.maxmind.geoip2.DatabaseReader.Builder
import com.maxmind.geoip2.exception.AddressNotFoundException
object GeoipSample {
def main(args: Array[String]): Unit ={
val database = new File("/tmp/GeoLite2-Country.mmdb")
// This creates the DatabaseReader object, which should be reused across lookups.
val reader = new Builder(database).build()
@shrkw
shrkw / file0.txt
Created August 29, 2014 22:43
Scalaのパーザコンビネータでアクセスログをパースしてみる ref: http://qiita.com/shrkw/items/e40f04a74840608fe62d
package com.example.service
import scala.io.Source
import scala.util.parsing.combinator.RegexParsers
object CloudFlareLogParserService {
def resolve: Unit = {
val accessLog =
"""
@shrkw
shrkw / file0.ini
Last active August 29, 2015 14:07
SupervisordにXMLRPC経由で繋いでプログラムを開始/停止してみる ref: http://qiita.com/shrkw/items/264294fcb1afdced6bf1
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
[include]
files = conf.d/*.ini
@shrkw
shrkw / file0.txt
Last active August 29, 2015 14:08
Codeiqの牛乳配達ルート問題 ref: http://qiita.com/shrkw/items/47effa195bd722488084
#!/bin/env python
# coding:utf-8
import csv
import sys
"""
根ノードから再帰的に深さ優先で木構造を作成しつつ探索も行う。
コスト集計も中途で行い、最低コストを上回った時点で以降の子孫ノードの探索を中止する。
木構造のプログラムを書くのに慣れていないため、実直にコーディングした。
@shrkw
shrkw / file0.txt
Last active August 29, 2015 14:18
defaultdictを返すdefaultdictを作って、KeyErrrorの発生しない世界を作る(+JSONのパースでの利用例) ref: http://qiita.com/shrkw/items/6f872ece6b29e160c0ec
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
... d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
class Animal
def initialize(species)
@species = species
end
def bark()
"bowbow"
end
def mew()
"mewwww"
end
class Animal
def initialize(species)
@species = species
end
def bark()
"bowbow"
end
def mew()
"mewwww"
end
@shrkw
shrkw / .zshenv.local
Created November 28, 2015 14:33
depends on aws, jq command, and an argument should be image id
function run-ec2 {
source ~/.virtualenv/CP-2710/bin/activate
aws ec2 run-instances --image-id $1 --instance-type t1.micro --key-name oregon > /tmp/run-i
aws_id=$(cat /tmp/run-i | jq -r '.Instances | .[] | .InstanceId')
echo "aws_id: $aws_id"
sleep 10
aws ec2 describe-instances --instance-ids $aws_id > /tmp/run-i
aws_ip=$(cat /tmp/run-i | jq -r '.Reservations | .[] | .Instances | .[] | .PublicDnsName')
echo "aws_ip: $aws_ip"
echo 'ssh -i ~/.ssh/id_rsa.aws.oregon -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ec2-user@$aws_ip'
@shrkw
shrkw / useradd_wih_ssh_key.sh
Created February 8, 2013 01:53
create new user with ssh key
read USER_NAME
useradd ${USER_NAME}
mkdir -m 700 /home/${USER_NAME}/.ssh
cat << _EOF_ > /home/${USER_NAME}/.ssh/authorized_keys
chmod 400 /home/${USER_NAME}/.ssh/authorized_keys
chown ${USER_NAME}:${USER_NAME} -R /home/${USER_NAME}/.ssh