Skip to content

Instantly share code, notes, and snippets.

from itertools import combinations
from math import factorial
def count_combinations(n: int, r: int):
return int(factorial(n) / (factorial(r) * factorial(n-r)))
team_count = 4
# 0 1 2 3 4 5 6 7 8 9 10 11
@hannal
hannal / fruits.html
Last active June 26, 2017 08:58
Django Template이 키나 속성에 대해 조용히 넘겨주지 않고 VariableDoesNotExist 내는 경우.
{{ apple|default:lemon.dictattr.color }} {# VariableDoesNotExist exception 발생 #}
<hr />
{% with lemon.dictattr.color as colour %}
{{ apple|default:colour }} {# 문제 없음 #}
{% endwith %}
from datetime import datetime
def hello(now=datetime.now()):
print(now)
class BaseModel(models.Model):
class Meta:
abstract = True
class A(BaseModel):
pass
class Number(int):
def __add__(self, value):
return self.__int__() + value * 10
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 을 안 쓰는 경우
@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('')
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 / 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"}
}
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: