#334 Tornado Remote IP
WebDev Python Tornado 2019-09-20由于 Tornado 部署在 Nginx 后面,通过 self.request.remote_ip
总是只能拿到 Nginx 地址。
#333 Django 3.0 发布,开始支持异步功能
Python Django 2019-09-16Django 项目组试图在保持向后兼容的基础之上,对阻塞部分进行改造,使之支持异步(通过装饰器的方式)。
包括 Session、Auth、ORM 与 Handlers 等。
ASGI 模式将 Django 作为原生异步应用程序运行,原有的 WSGI 模式将围绕每个 Django 调用运行单个事件循环,以使异步处理层与同步服务器兼容。
在这个改造的过程中,每个特性都会经历以下三个实现阶段:
- Sync-only,只支持同步,也就是当前的情况
- Sync-native,原生同步,同时带有异步封装器
- Async-native,原生异步,同时带同步封装器
Django 3.0 开始提供运行 ASGI 应用支持,让 Django 逐渐具备异步功能。做了这一改动后,Django 现在会感知到异步事件循环,并将阻止从异步上下文调用标记为 “异步不安全” 的代码(例如 ORM 操作),如果开发者之前使用的是异步代码,则可能会触发。如果看到 SynchronousOnlyOperation 错误,可以仔细检查代码并将数据库操作移到同步子线程中。
其它方面,Django 现在支持 MariaDB 10.1 及更高版本;新的 ExclusionConstraint 类可以在 PostgreSQL 上添加排除约束;输出 BooleanField 的表达式现在可以直接在 QuerySet 过滤器中使用,而无需先注解然后对注解进行过滤;自定义枚举类型 TextChoices、IntegerChoices 和 Choices 现在可用作定义 Field.choices 的方法。
需要特别注意的是:自从 2.2 之后,Django 将不再支持 Python 3.5。
#332 Django 版本历史
Django 2019-09-16Ver | Date | Notes |
---|---|---|
1.0 | 2008-09-03 | |
1.1 | 2009-07-29 | |
1.2 | 2010-05-17 | |
1.3 | 2011-03-23 | |
1.4 | 2012-03-23 | LTS |
1.5 | 2013-02-26 | |
1.6 | 2013-11-06 | |
1.7 | 2014-09-02 | |
1.8 | 2015-04-01 | LTS 2018-04-01 |
1.9 | 2015-12-01 | |
1.10 | 2016-08-01 | |
1.11 | 2017-04-04 | LTS 2020-04-01 |
2.0 | 2017-12-02 | |
2.1 | 2018-08-01 | |
2.2 | 2019-04-01 | LTS 2022-04-01 |
3.0 | 2019-12-02 | |
3.1 | 2020-08-04 | |
3.2 | 2021-04-06 | LTS 2024-04-01 |
4.0 | 2021-12-07 | |
4.1 | 2022-08 | |
4.2 | 2023-04 | LTS 2026-04-01 |
5.0 | 2023-12 |
#331 AsyncIO 异步执行命令 [编辑中]
Python asyncio 2019-08-31:) 本文正在编辑中,暂时不提供浏览...
#330 MySQL 数据类型总结
DB MySQL 2019-08-25数值
-
bit
[1 - 64] -
tinyint
1 smallint
2mediumint
3int
4-
bigint
8 -
float
4 浮点(单精度) double
8 浮点(双精度)decimal(m, d)
max(m, d) + 2 定点
别名 Synonyms:
integer
->int
bool
/boolean
->tinyint(1)
dec
/numeric
/fixed
->decimal
double precision
/real
->double
PS: 如果开启 REAL_AS_FLOAT
模式,REAL
就变成了 FLOAT
的别名。
字符串/文本
char
-
varchar
-
tinytext
text
mediumtext
-
longtext
-
JSON
MySQL 5.7 加入
二进制
binary
-
varbinary
-
tinyblob
blob
mediumblob
longblob
时间
类型 | 长度 | 备注 |
---|---|---|
year |
1 | 四位数:1901 ~ 2155 两位数:00 ~ 69 (2000 ~ 2069) 70 ~ 99 (1970 ~ 1999) |
date |
3 | 1000-01-01 ~ 9999-12-31 |
time |
3 | -838:59:59.999999 ~ 838:59:59.999999 即:-34d23h ~ 34d23h |
datetime |
8 | 1000-01-01 00:00:00.000000 ~ 9999-12-31 23:59:59.999999 |
timestamp |
4 | 1970-01-01 00:00:00 ~ 2038-01-19 11:14:07 |
- 用字符串(
varchar
)表示时间,除了失去数据库这一层的数据类型保证之外,还会在时间比较运算场景下,失去索引命中可能。 - 用整型数
bigint
(8 字节)表示时间,可以扩大timestamp
时间范围,但要自己维护数据(其实还是比较好维护的)。 - 带时区的时间:
timestamp
- 带精度的时间:
time
、datetime
、timestamp
,精度最大为 6,表示毫秒。比如:timestamp(6)
其他
enum
set
- Geometry 系列
货币
- 以分为单位
Integer
Decimal
,不要用float
,double
参考资料与拓展阅读
#329 PowerShell 在 Ubuntu 下的安装和试用
Linux PowerShell 开发工具 2019-08-22PowerShell 的最近的一次更新修复了其作为 SSH 的登录 Shell 的一些问题。
Win 10 对 NFS,SSH 的支持推进也是有目共睹。
随着 MS 对 Windows 的策略调整,估计以后 Windows 服务器也会变多。
- https://docs.microsoft.com/zh-cn/powershell/scripting/install/installing-powershell-core-on-linux
- https://docs.microsoft.com/zh-cn/powershell/scripting/overview
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of products
sudo apt-get update
# Enable the "universe" repositories
sudo add-apt-repository universe
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh
PS 常用命令
# 获取本地 IP
(invoke-webrequest -uri "http://ifconfig.me/ip").content
get-nettcpconnection | where-object {$_.State -eq "Listen"}
get-nettcpconnection -localport 49667
get-process -id (get-nettcpconnection -localport 49667).owningprocess
stop-process -id (get-nettcpconnection -localport 11).owningprocess
"# README" | out-file README.md
type README.md
#328 Shell 编程备忘录
Linux Shell 2019-08-21PS: Shell 编程时总是不能区分什么是 Shell 语法,什么是系统命令。
基础
x=abc
echo hello world
echo 'Just output $x'
echo "The value of x: $x"
echo ${x:?变量未设置}
echo ${x:+变量已设置}
echo ${x:-默认值}
变量
Shell 就是标准的弱类型,所有的数据都是按照字符串的形式存储,然后根据上下文决定如何处理。
x='hello world' # 等于号两边不能有空格
local x='hello world' # 本地作用域,函数体中使用
echo $a
echo ${a}
unset a
readonly a # 将变量设置成只读,不能修改,不能 unset
readonly b=1
# 没有提供方法取消 readonly 属性,但是我看到 SO 上有提供非常规方法来做这件事情
字符串变量
单引号不支持变量和转义(也就是说字符串中不能出现单引号)!
x="hello world"
echo $x
echo ${#x}
echo ${x/world/markjour} # 字符串替换
# 子字符串/字符串切片
echo ${x::5} # hello
echo ${x:7:8} # world
echo ${x::4} # hell
echo ${x::-4} # hello w
特殊变量
$0
$1
...
$n
# 所有参数
$*
$@
$# # 参数个数
$$ # 进程号
$! # 后台进程号
$? # 上一个命令的退出状态
$- # himBHs 当前 Shell 的选项
数组
arr=(zhao qian sun li)
arr[4]=zhou
arr[5]=wu
arr[6]=zheng
arr[7]=wang
echo ${arr[0]} # zhao
echo ${arr[@]} # zhao qian sun li zhao wu zheng wang
# 获取数组元素个数
echo ${#arr} # 4 定义时的长度,后来新加的不算,也就是说这个值定义变量的时候就定了
echo ${#arr[@]} # 8
条件
if condition:
then
:
else:
:
fi
[ ]
[[ ]]
test
循环
for var in list:
do
:
done
数学计算
test
逻辑
数学比较
字符串比较
文件相关
bcdfgkpurwxse
白菜豆腐干,靠谱如我,羞涩
最常用的:
-e
检查路径是否存在-d
检查是否为目录-f
检查是否为文件-r
检查文件是否可读-w
检查文件是否可写-x
检查文件是否可执行-s
检查文件是否为空
参考资料与拓展阅读
- StackOverflow, Unset readonly variable in bash
- How To Unix For Loop 1 to 100 Numbers
- Linux Handbook, Bash Beginner Series #4: Using Arrays in Bash
- StackOverflow, Questions tagged [bash]
#327 Requests 的日志配置
requests logging 2019-08-09import http.client
import logging
import requests
LOG_LEVEL = logging.DEBUG
LOG_FORMAT = '%(asctime)s [%(thread)s] [%(name)s:%(funcName)s#%(lineno)d] %(levelname)s %(message)s'
logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT)
LOG_HTTP = logging.getLogger("http.client")
def httpclient_log(*args):
LOG_HTTP.log(logging.DEBUG, " ".join(args))
http.client.print = httpclient_log
http.client.HTTPConnection.debuglevel = 1
#326 获取文件添加到 git 的时间
开发工具 Git 2019-08-01git log --diff-filter=A --follow --format="%h %ad %ce" -1 -- requirements.txt | cat
# 6a0eebf 2019-05-22 19-47-09 bobo@example.com