Skip to content

Instantly share code, notes, and snippets.

View shiumachi's full-sized avatar

Sho Shimauchi shiumachi

View GitHub Profile
@shiumachi
shiumachi / match_literal_text.py
Created February 3, 2014 11:55
制御文字や句読点にマッチする正規表現
import re
# ref: Regular Expression Cookbook p.26
text = """!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
aaa
bbb
"""
@shiumachi
shiumachi / linecache_sample.py
Created January 29, 2014 01:59
テキストの任意の行を高速に読み込む
# http://docs.python.jp/2.7/library/linecache.html
import linecache
linecache.getline("test.txt", 10)
@shiumachi
shiumachi / readcsv.py
Created January 28, 2014 17:36
csvを読み込む
import csv
data_reader = csv.reader(open("./data.csv"))
for row_list in data_reader:
','.join(row_list)
@shiumachi
shiumachi / gist:8641812
Created January 27, 2014 01:19
Python3.3 で pysqlite をインストールしようとしたらうまくいかない

Python3.3 で pysqlite をインストールしようとしたらうまくいかない。 検索したけど、有効な解決策が見つからなかった。

% python -V
Python 3.3.3
% pip install pysqlite
Downloading/unpacking pysqlite
  Running setup.py egg_info for package pysqlite
@shiumachi
shiumachi / get_girls_senbatsu.py
Last active January 4, 2016 14:29
GF(仮) Wiki からセンバツボーナス一覧を取得し、標準出力に出力する。
# -*- coding: utf-8 -*-
"""
GF(仮) Wiki からセンバツボーナス一覧を取得し、標準出力に出力する。
以下のフォーマットの csv として出力する。
ガールNo, ガール名, ガールタイプ, 功セン1, 功セン2, 功セン3, 守セン1, 守セン2, 守セン3
事前準備
--------
@shiumachi
shiumachi / get_soup.py
Created January 26, 2014 14:10
指定したURLのBeautifulSoupオブジェクトを取得する(BeautifulSoup4版)
# -*- coding: utf-8 -*-
# requirement: lxml
import requests
from bs4 import BeautifulSoup
URL = "https://gist.github.com/"
def get_soup(url=URL):
""" get soup object of url.
@shiumachi
shiumachi / is_some_of_values_over_n.py
Created January 25, 2014 15:21
d の中の値のうち少なくとも n 個の要素のカウントが m 以上だったらそのリストを返す。そうでなければ False を返す。
# -*- coding: utf-8 -*-
def is_some_of_values_over_n(d, n, m):
""" at least m (in d) properties' count is larger than or equal to n, return the list of the properties.
if not, return False.
"""
s_over_n_list = []
for s, count in d.iteritems():
if count >= n:
@shiumachi
shiumachi / sqlalchemy_create_table.py
Created January 25, 2014 02:22
SQLAlchemyでテーブルを作成する
# -*- coding: utf-8 -*-
# 参考: http://omake.accense.com/static/doc-ja/sqlalchemy/ormtutorial.html
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData
from sqlalchemy.orm import mapper
from sqlalchemy.orm import sessionmaker
@shiumachi
shiumachi / sort_case_insensitive.py
Created January 25, 2014 01:45
文字列のリストを文字の大小無視でソート
# -*- coding: utf-8 -*-
# 参考: Python クックブック第2版 p.199
def sort_case_insensitive(l):
""" in place sort string list (case insensitive)
Argument:
l (list)
@shiumachi
shiumachi / get_sorted_dict_values.py
Created January 22, 2014 16:25
辞書をソートした結果の値のリストを取り出す
# -*- coding: utf-8 -*-
# 参考: Python クックブック第2版 p.198
def get_sorted_dict_values(d):
keys = d.keys()
keys.sort()
return [d[key] for key in keys]