View fruits.html
{{ apple|default:lemon.dictattr.color }} {# VariableDoesNotExist exception 발생 #}
<hr />
{% with lemon.dictattr.color as colour %}
{{ apple|default:colour }} {# 문제 없음 #}
{% endwith %}
View obj_created_first_call.py
from datetime import datetime
def hello(now=datetime.now()):
print(now)
View subclasses.py
class BaseModel(models.Model):
class Meta:
abstract = True
class A(BaseModel):
pass
View add_overriding.py
class Number(int):
def __add__(self, value):
return self.__int__() + value * 10
View django_datetimefield_fixture.py
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 을 안 쓰는 경우
View more_http_methods_for_django.py
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('')
View like_dict.py
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):
View python-utf8.sublime-build
{
"shell_cmd": "python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf8"}
}
View unpacking_keyword_args.py
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:
View namedtuple.py
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))