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, |
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
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
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
"""Emulating Object Oriented Programming with dict and function. | |
### 0. reference | |
1) This code was written by refering below content. | |
Python のクラスシステム - http://www.shido.info/py/python7.html | |
### 1. emulating 3 concepts, class, instance and method. | |
1) class -> dict | |
2) instance -> dict | |
3) method -> function |
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
"""Compare two list.""" | |
def equal_list(lst1, lst2): | |
lst1 = lst1.copy() | |
for element in lst2: | |
try: | |
lst1.remove(element) | |
except ValueError: | |
return False |
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 random | |
import time | |
N = 100000 | |
lst1 = [random.randint(0, 9) for _ in range(N)] | |
lst2 = lst1.copy() | |
# insert | |
start1 = time.time() |
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
"""Check the obj is immutable or not by adding new attribute or sequence. | |
USAGE: | |
isimmutable(var) | |
WARNING: | |
This code cannot precisely check immutability, | |
because Python has the mechanisms to cutomize attribute access, | |
such as property and descriptor. |
OlderNewer