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 / 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])]
@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.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
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.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 / 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 / my-requirements-towards-my-job.md
Last active September 15, 2020 00:36
ぼくが職場に求めるもの、もしくは、組織を作るときに大切にしたいこと。該当する項目が増えるほど、楽しく働けると思う。今後も思いついた時に更新。

転職するときに考えること

軸はいろいろある。

キャリアカウンセラーの壁に貼ってあるバッド・カデルの図

https://www.waicrew.com/2015/02/07/

  • "なにをするか"
@shrkw
shrkw / battle_line_rule.ja.md
Last active September 8, 2019 14:21
バトルライン ルール 日本語 意訳。ルールは2012年発売版に基づく。
 *****       ****   ******** ********  **       ****     **      **  ****    **    ****
 **  **     ** ***     **       **     **      **        **      **  ** **   **   **
 ** **     **   ***    **       **     **     ******     **      **  **  **  **  ******
 **  **   **     ***   **       **     **      **        **      **  **    ****   **
 *****   ************  **       **     ******   ****     ******  **  **     ***    ****

準備

@shrkw
shrkw / gunicorn_with_virtualenv
Created October 31, 2013 10:51
CentOS init script for Gunicorn with Virtualenv
#!/bin/sh
#
# gunicorn_sr Startup script for gunicorn for sr
#
# chkconfig: - 86 14
# processname: gunicorn
# pidfile:
# description: Python application server
#
### BEGIN INIT INFO
@shrkw
shrkw / db.sqlite3
Created October 28, 2013 16:06
ForeingKey experiments on SQLAlchemy. It seems following 2 python codes are equivalent. According to my experiments, if you want to have `relationship`, you need to use explicitly any `foreign` by `ForeignKey` schema or `foreign` annotation. Anyway you can get the power of `relationship` with or without the foreign key constraints.
CREATE TABLE addresses (
id INTEGER NOT NULL,
email VARCHAR NOT NULL,
owner_id INTEGER,
PRIMARY KEY (id)
);
CREATE TABLE users (
id INTEGER NOT NULL,
name VARCHAR NOT NULL,
PRIMARY KEY (id)