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 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)) |
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
"""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 |
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 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 を省略してメソッドを呼び出す。 |
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
from abc import ABCMeta, abstractmethod | |
class Len(metaclass=ABCMeta): | |
@abstractmethod | |
def len(self): | |
"""Return length of object.""" | |
raise NotImplementedError() | |
def checktype(method): |
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
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): |
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
"""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, |
NewerOlder