Skip to content

Instantly share code, notes, and snippets.

View oyakata's full-sized avatar

Imagawa Yakata (oyakata) oyakata

  • Sumpu@Suruga
  • Japan/ the Age of Provincial Wars
View GitHub Profile
@oyakata
oyakata / replace_django_settings.py
Created December 2, 2016 08:32
Djangoのsettingsの設定値をまとめて置換
# -*- coding:utf-8 -*-
import re
import os
def main():
regex = re.compile(r'(COURSE_LEC_DETAIL_URL.*?) % APP_BASE\)', re.S)
for dirpath, dirnames, filenames in os.walk('./settings'):
for base in [x for x in filenames if x.endswith('.py')]:
@oyakata
oyakata / note01.py
Last active November 29, 2016 07:05
Python3を教えてもらう会 2016/11/29
# -*- coding:utf-8 -*-
# except ???, ** という構文がエラーになった
# raise Exception, 'NG' # python2は使えるけど、3はSyntaxError
try:
raise Exception('Not found.')
except Exception as ex:
print(ex)
@oyakata
oyakata / flatten
Created November 16, 2016 05:28
flattenを再帰ではなくループで実装
# -*- coding:utf-8 -*-
def flatten(L):
stack, tmp = [L], None
while stack:
tmp = stack.pop(-1)
while tmp:
x = tmp.pop(0)
if not hasattr(x, '__iter__'):
@oyakata
oyakata / flatten.py
Created November 15, 2016 07:17
itertools.chain.from_iterableを使わずflatten
# -*- coding:utf-8 -*-
def flatten(L):
'''入れ子のリストもすべて掘り下げてflatにする'''
for x in L:
if hasattr(x, '__iter__'):
for y in flatten(x):
yield y
else:
@oyakata
oyakata / rec.py
Last active February 9, 2016 06:56
flattenと逆
# -*- coding:utf-8 -*-
def deeplist(args):
depth, items = len(args), list(args)
result = L = []
while depth > 0:
x = [items.pop(0)]
L.append(x)
L = x
@oyakata
oyakata / randmask.py
Created September 9, 2015 04:53
文字列のN割をランダムにマスキング
# -*- coding:utf-8 -*-
import random
def main(text, n):
length = len(text)
count = int(length * n / 10)
sample = random.sample(range(length), count)
L = list(text)
for i in sample:
@oyakata
oyakata / hexavigesimal.py
Created September 4, 2015 07:59
アルファベットA-Zによる数値表現を扱う(例: Excelの列番号)
# -*- coding:utf-8 -*-
import string
U = string.ascii_uppercase
def to_s(number):
'''数値をアルファベット文字列に変換する。
>>> to_s(1)
@oyakata
oyakata / .gitconfig
Created August 13, 2015 07:18
Linux設定ファイル系
[user]
name = oyakata
email = oyakata@users.noreply.github.com
[core]
excludesfile = $HOME/.gitignore
editor = vim -c \"set fenc=utf-8\"
[merge]
ff = false
[alias]
ci = commit
@oyakata
oyakata / compressing.py
Last active August 29, 2015 14:23
zipファイル圧縮率計算(案)
# -*- coding:utf-8 -*-
import zlib
def rate(bin, size):
compressed = len(zlib.compress(bin))
return float(compressed) / float(size)
KB = 1024
@oyakata
oyakata / result.txt
Created June 18, 2015 06:46
N回ごとにsleep
1
2
3
4
5
6
7
8
9
sleeping...