Skip to content

Instantly share code, notes, and snippets.

View shiumachi's full-sized avatar

Sho Shimauchi shiumachi

View GitHub Profile
@shiumachi
shiumachi / gist:7422686
Created November 11, 2013 23:47
当たりが1つしかないくじ引きの確率
def f(num, max_num):
"""
num: くじを引く回数
max_num: くじの総数
"""
p = 1.0
for i in xrange(num):
p = p * (float(max_num - i - 1) / (max_num - i))
return p
@shiumachi
shiumachi / gist:7519841
Created November 17, 2013 23:37
Cloudera Manager + Parcel 環境における、Hue のアプリケーション管理の仕方 (app_reg.py の使い方)
[sho@demo]/opt/cloudera/parcels/CDH/share/hue% export HUE_APP_REG_DIR=/opt/cloudera/parcels/CDH/share/hue
[sho@demo]/opt/cloudera/parcels/CDH/share/hue% tools/app_reg/app_reg.py --list
Name Version Author Path
------------------ ------- --------------- -----------------------------------
about 2.5.0 Hue ./apps/about
beeswax 2.5.0 Hue ./apps/beeswax
filebrowser 2.5.0 Hue ./apps/filebrowser
hbase 2.5.0 Hue ./apps/hbase
help 2.5.0 Hue ./apps/help
impala 2.5.0 Hue ./apps/impala
@shiumachi
shiumachi / gist:8157792
Created December 28, 2013 09:42
kaa を起動するとエラーを吐く
% kaa
Traceback (most recent call last):
File "/Users/sho/.virtualenvs/py3/bin/kaa", line 9, in <module>
load_entry_point('kaaedit==0.26.1', 'console_scripts', 'kaa')()
File "/Users/sho/.virtualenvs/py3/lib/python3.3/site-packages/pkg_resources.py", line 352, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/Users/sho/.virtualenvs/py3/lib/python3.3/site-packages/pkg_resources.py", line 2307, in load_entry_point
return ep.load()
File "/Users/sho/.virtualenvs/py3/lib/python3.3/site-packages/pkg_resources.py", line 2021, in load
entry = __import__(self.module_name, globals(),globals(), ['__name__'])
@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))