Django==1.11
djangorestframework==3.7.3
支持YAML docstrings
支持python3
vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"https://reg-mirror.qiniu.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
重启 docker
服务: service docker restart
# pip3 install selenium beautifulsoup4
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=chrome_options, executable_path='/usr/bin/chromedriver')
driver.get(url='url')
self.driver.execute_script('window.scrollBy(0,500)')
html = self.driver.page_source
soup = BeautifulSoup(html, 'lxml')
给正在运行的Docker容器动态绑定卷组(动态添加Volume)
subprocess.Popen 实时获取docker events事件()
-
对象字段值变化差异更新 BaseModel(不包含外键对象)
-
版本号version控制实现乐观锁,version字段类型只允许整型或浮点型(时间戳类型)
通过 {domain}/{uri}?s={size}实现获取指定大小缩略图
原图:localhost/u/1523562/avatar
缩略图:localhost/u/1523562/avatar?s=200
缩略图:localhost/u/1523562/avatar?s=100
打印调用日志:打出所有的调用请求、参数和返回值,或写入日志
检测文件改变自动重启: autoreload, 更新代码不用重启服务,使用werkzeug的run_with_reloader函数实现
import logging
import logging.config
import configs
# 应用初始化时加载logger配置
logging.config.dictConfig(configs.LOGGER_CONFIG)
# 各文件使用
logger = logging.getLogger(__name__)
logger.info('teste')
try:
do_something()
except Exception as e:
logger.error('error', exc_info=True)
process_error()
output: [2020-10-07 15:47:43 +0000] [118] [INFO] Redis cluster started
根据id倒序(即创建时间倒叙)分页,适用简单场景
使用max获取最大id,从最后一条数据向前分页
通过id倒序向前推进使用 <=
和 limit
结合的方式优化分页加载
eg: select * from yourtable where id <= seek_id order by id desc limit 20;
from pagination.pagination import WholeTableIdReversePagination
queryset = YourModel.objects.all()
pagination = WholeTableIdReversePagination()
result = list(pagination.paginate_queryset(queryset, request))
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'pagination.pagination.WholeTableIdReversePagination',
}
GET https://api.example.org/accounts/?page_seek_id=100
or
GET https://api.example.org/accounts/
HTTP 200 OK
{
"count": 20
"next": "https://api.example.org/accounts/?page_seek_id=80",
"previous": none,
"results": [
…
]
}
from pagination.utils import QueryWrapper
sql = 'select id, username, first_name from auth_user'
queryset = QueryWrapper(sql)
count = queryset.count()
data = queryset.all()
# 在Django中使用
from django.core.paginator import Paginator
pages = Paginator(queryset, per_page=10)
page = pages.page(page_no) # 获取某页数据
# 在django rest framework中使用
page = self.paginate_queryset(queryset)
results = self.get_paginated_response(page).data
print(results)
>>>
{
"count": 25,
"next": "http://127.0.0.1:8888/test/?page=2",
"previous": null,
"results": [{
"id": 11349230,
"username": "张三",
"phone": "1440182340944",
}, {
"id": 11344204,
"username": "李四",
"phone": "1440182333431",
},..
}
ref: http://mirrors.163.com/.help/
# http://mirrors.163.com/.help/debian.html
curl -s http://mirrors.163.com/.help/sources.list.buster > /etc/apt/sources.list
pip install kombu==4.6.11
Usages:
# step1: define routing_key
TEST_ACTION_ROUTING_KEY = 'order.test'
# step2: define message handler func
# consumer: handle_test_action 位于 test.handler.test
# queue: test.handler.test.handle_test_action
def handle_test_action(message):
body = message.body
pass
# step3: register message handle
# bind queue to exchange by routing_key
class MessageHandler(object):
def __init__(self):
self.handle_map = {
TEST_ACTION_ROUTING_KEY: handle_test_action,
}
# step4: run consumer server
mq_server = MQServer(rabbitmq_conn, MessageHandler())
t = Thread(target=mq_server.run, args=[])
t.setDaemon(True)
t.start()
# optional run producer
mq_producer = MQProducer(rabbitmq_conn, RABBITMQ_EXCHANGE)
# step5: publish message
mq_producer.publish(TEST_ACTION_ROUTING_KEY, message)
or
mq_server.publish(TEST_ACTION_ROUTING_KEY, message)
ref: https://pushiqiang.blog.csdn.net/article/details/124734320
依赖 aria2, Linux 环境使用
./download.sh https://www.xxx.com/snapshots.tar