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 argparse | |
from django.core.management.base import handle_default_options | |
from django.utils import importlib | |
import csv | |
def is_noble(user): | |
"""があればTrueを返します。 """ | |
if user.is_superuser: |
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 re | |
regex1 = re.compile(r"[ ]+") | |
regex2 = re.compile(ur"[ ]+") | |
def main(*args): | |
"""\ | |
str でパターンを定義したregex1はおかしな分割がされてしまいます。 | |
unicodeで定義したregex2は大丈夫です。 |
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 -*- | |
from argparse import ArgumentParser | |
psr = ArgumentParser() | |
add = psr.add_argument | |
add("--conf", default="foo") | |
add("--app", default="bar") | |
args = psr.parse_args("--conf=boo --app=zoo".split()) | |
print args.conf, args.app |
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 spam(spams): | |
for s in spams: | |
print "spam(%s)" % s | |
def ham(hams, num): | |
for h in hams: | |
print "ham(%s)" % (h + num) | |
spam(x + 3 for x in range(11)) | |
ham(x + 3 for x in range(11), 9) |
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 -*- | |
# 総当りジェネレータ | |
round_robin = lambda L: ((i, j) for i in L for j in L if not i is j) | |
# タプル -> 辞書リスト | |
tpl2dcts = lambda L: (dict(zip(L[0], x)) for x in L[1:]) | |
# 2要素ずつ取り出したタプルのジェネレータ | |
pairs = lambda L: ((L[x], L[x+1]) for x in xrange(0, len(L), 2)) |
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 -*- | |
class Animal(object): | |
def ouch(self): | |
return u"あ痛" | |
class Human(Animal): | |
pass | |
class Shark(Animal): |
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
[extensions] | |
sb=~/work/hgsmpl/myhgext/safe_branch.py | |
# pyファイルを置いた場所を指定して下さい。 |
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 re | |
from django import forms | |
USERID_REGEX = re.compile(r"^[\w-]+$") | |
class UserForm(forms.Form): | |
userid = forms.RegexField(max_length=64, regex=USERID_REGEX) |
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 -*- | |
from collections import defaultdict | |
from functools import partial | |
def counter(*keys): | |
"""クラスを使わずにカウンターを作る。 | |
>>> cnt = counter("foo", "bar", "baz") | |
>>> cnt.foo() | |
>>> cnt.foo() | |
>>> cnt.bar() |
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 re | |
def result(ptn, S): | |
tokens = [re.sub(ptn, ",", x) for x in S.split()] | |
print " ".join(tokens) | |
def main(): | |
"""数値文字列に3桁ごとにカンマを追加する。""" | |
cmm = r"(?<=\d)(?=(\d{3})+(\Z|\D))" |
OlderNewer