Skip to content

Instantly share code, notes, and snippets.

View shiumachi's full-sized avatar

Sho Shimauchi shiumachi

View GitHub Profile
@shiumachi
shiumachi / gist:8208585
Created January 1, 2014 14:52
botのsaying listをcsvに変換する
# text converter
# source file:
# [string]: [string]
# convert to csv:
# [string],[string]
import sys
for line in sys.stdin:
name, word = line.rstrip().split(': ')
@shiumachi
shiumachi / gist:8216537
Created January 2, 2014 08:51
HipChatに書きこむ
# TOKEN, ROOM_NAME は適宜置き換え
# 事前に pip install hypchat でパッケージをインストールしておくこと
import hypchat
hc = hypchat.HypChat(TOKEN)
for i in hc.rooms()['items']:
if i['name'] == ROOM_NAME:
room = hc.get_room(i['id'])
@shiumachi
shiumachi / gist:8233944
Created January 3, 2014 06:56
任意の文字列 line に word が含まれているかどうかを判定し、集計する。複数の word があっても効率よく集計できる
import re
import sys
import logging
logging.getLogger().setLevel(logging.DEBUG)
d = {}
def count(line, word):
#logging.debug("line: {0}".format(line))
@shiumachi
shiumachi / ast_example.py
Created January 4, 2014 16:43
astモジュールの使用例
# sample of ast module
# reference:
# http://docs.python.jp/2.7/library/ast.html
# http://stackoverflow.com/questions/1515357/simple-example-of-how-to-use-ast-nodevisitor
import ast
import sys
import logging
@shiumachi
shiumachi / convert_to_datetime.py
Created January 5, 2014 15:48
Hadoop等の時間表記をdatetimeオブジェクトに変換する
date_string = "2014-01-05 22:20:50,307"
def convert_to_datetime(date_string):
""" input: %Y-%m-%d %H:%M:%S,%f
example: 2014-01-05 22:20:50,307
return: datetime object
"""
date_format = "%Y-%m-%d %H:%M:%S,%f"
import datetime
return datetime.datetime.strptime(date_string, date_format)
@shiumachi
shiumachi / gist:8279961
Created January 6, 2014 08:49
Hadoopのログから日付、ログレベル、メッセージを分割して返す
example = """2014-01-05 22:20:50,307 INFO org.apache.hadoop.hdfs.server.datanode.BlockPoolSliceScanner: Verification succeeded for BP-1337840909-192.168.0.1-1374311151785:blk_8462243608396789329_201176
"""
def parse_datetime_and_level(line):
""" input: hadoop log
output: [datetime, loglevel, message]
"""
import re
r = re.compile("(^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}) ([A-Z]{4,5}) (.*)")
m = r.match(line)
@shiumachi
shiumachi / gist:8301406
Created January 7, 2014 15:53
文字列のリストを与えたとき、要素内の最大長の文字列に合わせて右寄せで出力する
def print_arr_right_aligned(arr):
""" input: string array ['a', 'ab', 'abc']
output: None. print with right aligned.
a:
ab:
abc:
"""
len_a = max(map(lambda x: len(x), arr))
for i in arr:
print("{0:>{1}}: ".format(i, len_a))
@shiumachi
shiumachi / gist:8319962
Created January 8, 2014 16:48
datetime型をunixタイムスタンプに変換する
def convert_to_timestamp(dt):
""" input: DateTime object
output: unix timestamp
reference: http://stackoverflow.com/questions/2775864/python-datetime-to-unix-timestamp
"""
import calendar, datetime
return calendar.timegm(dt.utctimetuple())
@shiumachi
shiumachi / gist:8330455
Created January 9, 2014 07:01
すごくシンプルな hipchat bot。リストに登録したメッセージを一定間隔でランダムに出力する。
# -*- coding: utf-8 -*-
import hypchat
import ConfigParser
import time
import random
config = ConfigParser.RawConfigParser()
config.read("config.ini")
TOKEN = config.get("account", "token")
@shiumachi
shiumachi / gist:8359272
Created January 10, 2014 17:59
どんなキーが入っているかわからない辞書のリストの整理方法
data = [{'a' : 100 , 'b' : 50}, {'a': 120}, {'b': 40}]
for i in data:
a = i.setdefault('a', 0)
b = i.setdefault('b', 0)
print("{0},{1}".format(a, b))