Skip to content

Instantly share code, notes, and snippets.

View zhu327's full-sized avatar
🥰
Out sick

Timmy zhu327

🥰
Out sick
View GitHub Profile
@zhu327
zhu327 / paser.py
Created November 16, 2017 07:50
imp语言解释器实现 http://python.jobbole.com/82206/
# coding: utf-8
import sys
import re
def lex(characters, token_exprs):
pos = 0
tokens = []
while pos < len(characters):
match = None
@zhu327
zhu327 / nginx.conf
Created June 5, 2017 06:08
nginx config file desc
#运行用户
user nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;
#全局错误日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
@zhu327
zhu327 / cookbook.py
Created May 2, 2017 09:16
Python cookbook
# coding: utf-8
u"""
1.4 字符串对齐
字符串方法 ljust rjust center 参数: int 对齐符号数目 str 可以选的对齐占位符,默认为空格
"""
# 1.9 简化string translate方法使用流程
import string
@zhu327
zhu327 / log.py
Created April 20, 2017 03:12
python logging to stdout
import logging, logging.config
import sys
LOGGING = {
'version': 1,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'stream': sys.stdout,
}
@zhu327
zhu327 / structure.py
Last active April 4, 2018 08:04
data structure
# coding: utf-8
u"""
线性数据结构, 栈, 队列, deques, 容器结构, 数据项之间存在相对的位置
"""
class Stack(object):
u"""
栈 先进后出
@zhu327
zhu327 / classproperty.py
Created January 3, 2017 08:54
Python class property decorator only support getattr
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)()
@zhu327
zhu327 / django_orm.py
Last active December 22, 2016 02:54
snippet of django orm
# 关联字段用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'))
@zhu327
zhu327 / serializer.py
Last active May 27, 2017 10:16
DRF Serializer
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)
@zhu327
zhu327 / permission.py
Last active November 28, 2016 08:11
get user all permissions Django
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()
@zhu327
zhu327 / custom_aggregation.py
Created November 14, 2016 09:55
simple django custom aggregation
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):