Skip to content

Instantly share code, notes, and snippets.

View yubessy's full-sized avatar

Shotaro Tanaka yubessy

View GitHub Profile
@yubessy
yubessy / sqlite3db.py
Created July 9, 2015 08:32
Stupid SQLite3 handler
import re
import sqlite3
class SQLite3DB:
_IDENTIFIER_RE = re.compile(r'^[a-zA-Z_]\w*$')
def __init__(self, filename):
self._con = sqlite3.connect(filename, isolation_level=None)
self._con.row_factory = sqlite3.Row
@yubessy
yubessy / file0.txt
Last active August 29, 2015 14:13
あまり知られていないPythonの言語仕様(Python3.4以降対応) ref: http://qiita.com/yubessy/items/bfcce577e2266ce86641
>>> c1 = 1 + 1j
>>> c2 = 1 - 2j
>>> c1 + c2
(2-1j)
>>> c1 * c2
(3-1j)
>>> c1 / c2
(-0.2+0.6j)
@yubessy
yubessy / インストール
Created January 16, 2015 10:13
Pythonで正規表現にマッチする文字列をランダムに生成する ref: http://qiita.com/yubessy/items/6df954f3ad1806c17fdd
$ pip install rstr
@yubessy
yubessy / Gemfile
Last active August 29, 2015 14:04
LESSやらSassやらを使わずにRailsにBootstrapを導入 ref: http://qiita.com/yubessy/items/d01a1e4ea54741331942
gem 'twitter-bootstrap-rails', :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git', :branch => 'bootstrap3'
@yubessy
yubessy / mywiki.py
Created June 29, 2014 03:19
MediaWIki APIを使って項目の別名を取得したりなど
#-*- encoding: utf-8 -*-
# thirdlib
import requests
BASE_URL = "http://en.wikipedia.org/w/api.php"
BLLIMIT = 250
def get_regular_name(q):
@yubessy
yubessy / myshorten.py
Created June 29, 2014 03:16
長い文字列を短く表示
# -*- coding: utf-8 -*-
def shorten(s, length=80):
u"""
長い文字列を"abraca ... tabra"のように短く表示
"""
if len(s) < length:
return s
else:
if isinstance(s, unicode):
@yubessy
yubessy / myhtmlclean.py
Created June 29, 2014 03:03
HTMLをクリーンアップ
# -*- coding: utf-8 -*-
# stdlib
import re
LEFT_SPACES = re.compile(r'\s+<')
RIGHT_SPACES = re.compile(r'>\s+')
SCRIPT_TAG = re.compile(r'<script[^>]*>.*?</script>')
COMMENT = re.compile(r'<!--[\s\S]*?-->')
@yubessy
yubessy / myerr.py
Created June 29, 2014 02:35
簡単なエラーメッセージ出力
# -*- coding: utf-8 -*-
# stdlib
import sys
import traceback
def print_exception(func, log_file=sys.stderr):
u"""
関数実行中に発生した例外を出力
"""
@yubessy
yubessy / mydatex.py
Created June 29, 2014 02:33
Wikipediaの各年ページの日付をパース
# -*- coding: utf-8 -*-
# stdlib
import re
import datetime
# thirdlib
from dateutil.parser import parse
MONTHS = (
"January",
"February",
@yubessy
yubessy / mycompress.py
Last active August 29, 2015 14:03
Pythonのオブジェクト圧縮・展開
# -*- coding: utf-8 -*-
# stdlib
import cPickle as pickle
import zlib
PROTOCOL = pickle.HIGHEST_PROTOCOL
def save_to_str(obj):