Skip to content

Instantly share code, notes, and snippets.

# works on Python 3
def check_argument_type(func):
def wrapper(*args):
if (
not hasattr(func, '__annotations__') or
len(func.__annotations__) == 0
):
return func(*args)
@hannal
hannal / namedtuple.py
Last active August 29, 2015 14:24
namedtuple typename
from collections import namedtuple
Point_type = namedtuple('Point', ('x', 'y'))
point_instance = Point_type(1, 10)
print(type(Point_type))
print(type(point_instance))
print(point_instance.__class__.__name__)
print(isinstance(point_instance, Point_type))
c_num = 10
d_num = 0
def do_something(a, b, c=0, d=0):
return a + b + c + d
if d_num > 0:
print(do_something(1, 2, c=c_num, d=d_num))
else:
@hannal
hannal / python-utf8.sublime-build
Created November 15, 2015 03:58
Sublime 빌드창에 한글 잘 나오는 빌드 설정
{
"shell_cmd": "python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf8"}
}
class LikeDict(dict):
def __getattribute__(self, attr):
return self[attr]
def __setattr__(self, attr, value):
self[attr] = value
class LikeDict2(object):
def __getitem__(self, key):
@hannal
hannal / more_http_methods_for_django.py
Last active February 15, 2016 14:02
django에 put, patch, delete method 추가.
from django.http import QueryDict
from django.http.multipartparser import MultiValueDict
class MoreMethodsMiddleware(object):
def process_request(self, request):
method = request.META.get('REQUEST_METHOD', '').upper()
request.PUT = QueryDict('')
request.DELETE = QueryDict('')
from datetime import datetime
from django.db import models
from django.utils import timezone
class Post(models.Model):
# created_at = models.DateTimeField(auto_now_add=True) # 원래 필드
# updated_at = models.DateTimeField(auto_now=True) # 원래 필드
# created_at = models.DateTimeField(default=datetime.now) # TIMEZONE 을 안 쓰는 경우
class Number(int):
def __add__(self, value):
return self.__int__() + value * 10
class BaseModel(models.Model):
class Meta:
abstract = True
class A(BaseModel):
pass
from datetime import datetime
def hello(now=datetime.now()):
print(now)