This file contains hidden or 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 hist(arr, meter=20): | |
""" print histogram. | |
Arguments: | |
arr: dataset. contents must be numeric | |
meter (default to 20) | |
""" | |
for n in arr: | |
print("{0:>4}: {1}".format(n, "*" * int(round(float(n) / (float(max(arr)) / meter))))) |
This file contains hidden or 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 application(environ, start_response): | |
start_response('200 OK', [('Content-type', 'text/plain')]) | |
return "Hello, world!" | |
from wsgiref import simple_server | |
if __name__=='__main__': | |
server = simple_server.make_server('', 8080, application) | |
server.serve_forever() |
This file contains hidden or 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
from BeautifulSoup import BeautifulSoup | |
import requests | |
r = requests.get("https://github.com/") | |
soup = BeautifulSoup(r.text) | |
for i in soup.findAll('h1'): | |
print(i.text) |
This file contains hidden or 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 requests | |
def get_nn_used_heap(nn_jmx_url): | |
""" return NN used heap (bytes) | |
Argument: | |
nn_jmx_url (required) | |
Return Value: |
This file contains hidden or 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 parse_ls(ls_line): | |
""" parse a line of ls -l result. | |
Argument: | |
ls_line (one line of ls -l) | |
Return: | |
dict of file / dir information. | |
permission, filenum, user, group, size, month, day, time, name | |
""" |
This file contains hidden or 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
from convert_to_datetime import convert_to_datetime | |
def add_yeardayhour_to_log(line): | |
""" add YYMMDDhh to the beginning of the log. | |
Argument: | |
line (hadoop log line) | |
""" | |
arr = line.rstrip().split() | |
date_string = ' '.join(arr[0:2]) |
This file contains hidden or 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 -*- | |
# 参考: Python クックブック第2版 p.183 | |
class Region(object): | |
def __init__(self, **kwds): | |
self.__dict__.update(kwds) |
This file contains hidden or 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 -*- | |
# 参考: Python クックブック第2版 p.188 | |
import random | |
def random_pick(item_list, probabilities): | |
""" return a random item in a list, which has different probability. |
This file contains hidden or 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 -*- | |
# 参考: http://kondou.com/BS4/ | |
# note: This code uses BeautifulSoup3 which is deprecated. | |
# If you need code sample of BS, please see https://gist.github.com/shiumachi/8633275 | |
from BeautifulSoup import BeautifulSoup | |
import requests | |
This file contains hidden or 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 sys | |
if __name__ == '__main__': | |
# <2.6 doesn't support named value like sys.version_info_major | |
if sys.version_info[0] == 2 and sys.version_info[1] < 6: | |
print "Python version: %d.%d" % (sys.version_info[0], sys.version_info[1]) | |
else: | |
print("Python version: {0}.{1}".format(sys.version_info[0], sys.version_info[1])) |