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
# coding: utf-8 | |
import sys | |
import re | |
def lex(characters, token_exprs): | |
pos = 0 | |
tokens = [] | |
while pos < len(characters): | |
match = None |
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
#运行用户 | |
user nobody; | |
#启动进程,通常设置成和cpu的数量相等 | |
worker_processes 1; | |
#全局错误日志及PID文件 | |
#error_log logs/error.log; | |
#error_log logs/error.log notice; | |
#error_log logs/error.log info; |
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
# coding: utf-8 | |
u""" | |
1.4 字符串对齐 | |
字符串方法 ljust rjust center 参数: int 对齐符号数目 str 可以选的对齐占位符,默认为空格 | |
""" | |
# 1.9 简化string translate方法使用流程 | |
import string |
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 logging, logging.config | |
import sys | |
LOGGING = { | |
'version': 1, | |
'handlers': { | |
'console': { | |
'class': 'logging.StreamHandler', | |
'stream': sys.stdout, | |
} |
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
# coding: utf-8 | |
u""" | |
线性数据结构, 栈, 队列, deques, 容器结构, 数据项之间存在相对的位置 | |
""" | |
class Stack(object): | |
u""" | |
栈 先进后出 |
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
class ClassPropertyDescriptor(property): | |
u""" | |
class property descriptor | |
""" | |
def __get__(self, obj, objtype=None): | |
if self.fget is None: | |
raise AttributeError("unreadable attribute") | |
return self.fget.__get__(obj, objtype)() |
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
# 关联字段用select_related不能用正常使用only选取关联对象的某些字段,可以变通一下 | |
a = Articles.objects.only('title').annotate(blog_name=F('blog__name')).first() | |
a.title | |
a.blog_name | |
# Django group by | |
from django.db.models import Count | |
Members.objects.values('designation').annotate(dcount=Count('designation')) |
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
class DynamicFieldsModelSerializer(serializers.ModelSerializer): | |
""" | |
A ModelSerializer that takes an additional `fields` argument that | |
controls which fields should be displayed. | |
""" | |
def __init__(self, *args, **kwargs): | |
# Don't pass the 'fields' arg up to the superclass | |
fields = kwargs.pop('fields', None) |
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 django.contrib.auth.models import Permission, User | |
def get_user_permissions(user): | |
if user.is_superuser: | |
return Permission.objects.all() | |
return Permission.objects.filter(Q(group__user=user)|Q(user=user)).distinct() |
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 django.db.models import Aggregate | |
from django.db.models.sql import aggregates | |
def custom_aggregation(select_query): | |
class SqlAggregate(aggregates.Aggregate): | |
sql_function = '' | |
sql_template = select_query | |
class VisitorRate(Aggregate): |