This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# text converter | |
# source file: | |
# [string]: [string] | |
# convert to csv: | |
# [string],[string] | |
import sys | |
for line in sys.stdin: | |
name, word = line.rstrip().split(': ') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
import logging | |
logging.getLogger().setLevel(logging.DEBUG) | |
d = {} | |
def count(line, word): | |
#logging.debug("line: {0}".format(line)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import hypchat | |
import ConfigParser | |
import time | |
import random | |
config = ConfigParser.RawConfigParser() | |
config.read("config.ini") | |
TOKEN = config.get("account", "token") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |