Skip to content

Instantly share code, notes, and snippets.

View domodomodomo's full-sized avatar

domodomodomo domodomodomo

View GitHub Profile
def usage():
n = 2**2 * 3**2 * 5**2
print('# n')
print(n)
print('## factorize')
print(factorize(n))
print('## divisor')
for d in divisorize((factorize(n))):
print(d, num(d))
@domodomodomo
domodomodomo / ids.py
Last active May 18, 2018 05:01
Show object's identity recursively.
"""Show object's identity recursively.
sample code 1 ... difference between copy and deepcopy
sample code 2 ... reason why [[0]*3]*3 is not good for 2d list initialization
"""
def ids(identifier: object) -> dict:
"""Return the “identity” of an object, recursively."""
# Effective Python item 26
def classmethod(method):
def decorated_method(self, *args, **kwargs):
cls = type(self)
return method(cls, *args, **kwargs)
return decorated_method
def staticmethod(method):
def decorated_method(self, *args, **kwargs):
# self を省略してメソッドを呼び出す。
from abc import ABCMeta, abstractmethod
class Len(metaclass=ABCMeta):
@abstractmethod
def len(self):
"""Return length of object."""
raise NotImplementedError()
def checktype(method):
import timeit
def sample_code():
lst, v = [1, 5, 3, 4, 5, 6, 7, 5, 5, 10], 5
remove_equal(lst, v)
print(lst, v) # [1, 3, 4, 6, 7, 10] 5
def remove_equal_listcomprehension(lst: list, v):
@domodomodomo
domodomodomo / django_random_username.py
Last active January 31, 2018 21:08 — forked from jcinis/gist:2866253
Generate a random username for Django
"""Make a random username for Django, such as 'adf1-2dfa-dfc4-fa9a'."""
import random
import string
from django.contrib.auth import get_user_model
AuthUser = get_user_model()
def make_random_username(length=16,