-
Python的类型提示type hints
Pydantic是一个基于Python类型提示来定义数据验证,序列化和
文档(使用SON模式)库
Starlette是一种轻量级的ASGl框架/工具包,是构建高性能
Asyncio服务的理想选择
查看全部 -
语言特点:
1、性能优越;
2、开发效率高;
3、减少认为bug50%以上;
查看全部 -
sqlalchemy
查看全部 -
版本问题,与教师版本最好保持一致
查看全部 -
需要注意框架的版本与包的版本是否一致,有可能版本不一致导致BUG产生
查看全部 -
注意python第三方包版本不一致出现的兼容性问题
查看全部 -
pycharm 中使用anoconda python 编辑器,但是python console直接显示error exit 1:
import _ssl DLL load fail error
解决方法:
From anaconda3\Library\bin copy below files and paste them in anaconda3/DLLs:
- libcrypto-1_1-x64.dll
- libssl-1_1-x64.dllhttps://stackoverflow.com/questions/54175042/python-3-7-anaconda-environment-import-ssl-dll-load-fail-error
查看全部 -
pycharm 直接使用new project 创建project,使用virtualenv方式 指定anoconda的python解释器作为project的python解释器。
在使用pip install的时候,出现SSL错误:
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/aiofiles/
Could not fetch URL https://pypi.org/simple/aiofiles/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/aiofiles/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement aiofiles==0.6.0 (from versions: none)
ERROR: No matching distribution found for aiofiles==0.6.0。
原因是在windows powershell中使用anoconda的pip命令,需要在环境变量中添加如下三项:
D:\Anaconda3
D:\Anaconda3\Scripts
D:\Anaconda3\Library\bin(https://stackoverflow.com/questions/45954528/pip-is-configured-with-locations-that-require-tls-ssl-however-the-ssl-module-in)
powershell中添加环境变量方法:
$env:Path += ";D:\Anaconda3"
(https://stackoverflow.com/questions/714877/setting-windows-powershell-environment-variables)
查看环境变量方法:dir env:
添加后一切正常。
查看全部 -
代码没问题,但是运行报错了
框架有包的版本兼容问题
查看全部 -
思路清晰,讲解细致,通俗易懂,项目贯穿知识点,十分!!! 希望能在出一版大型项目的教程查看全部
-
https://github.com/liaogx/fastapi-tutorial.git
查看全部 -
版本
查看全部 -
from datetime import datetime
from pathlib import Path
from typing import List, Optional
from pydantic import BaseModel, ValidationError, constr
from sqlalchemy import Column, Integer, String
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.declarative import declarative_base
print("\033[31m1. --- Pydantic的基本用法。Pycharm可以安装Pydantic插件 ---\033[0m")
class User(BaseModel):
id: int
name: str = "john snow"
signup_ts: datetime
friends: List[int] = []
external_data = {
"id": "123",
"signup_ts": "2021-08-09 11:12:30",
"friends": ["1", 2, 4]
}
# if __name__ == '__main__':
user = User(**external_data)
print(user.id, user.name)
print(user.dict())
print("\033[31m2. --- 校验失败处理 ---\033[0m")
try:
u = User(id=1, signup_ts=datetime.today(), friends=['not a number', 1])
print(u)
except ValidationError as e:
print(e.json())
print("\033[31m3. --- 模型类的属性和方法 ---\033[0m")
print(user.dict())
print(user.json())
print(user.copy())
# 类方法
print("类方法")
print(User.parse_obj(external_data))
print(User.parse_raw('{"id": 123, "name": "john snow", "signup_ts": "2021-08-09T11:12:30", "friends": [1, 2, 4]}'))
file = Path("pydanic_tutorial.json")
file.write_text('{"id": 123, "name": "john snow", "signup_ts": "2021-08-09T11:12:30", "friends": [1, 2, 4]}')
print(User.parse_file(file))
print(user.schema())
print(user.schema_json())
# 不校验属性, 直接构造
print(User.construct(id="sdf", signup_ts=datetime.today(), friends=['not a number', 1]))
print(User.__fields__.keys())
print("\033[31m4. --- 递归模型 ---\033[0m")
class Sound(BaseModel):
sound: str
class Dog(BaseModel):
name: str
weight: Optional[float] = None
sound: List[Sound]
# dog = Dog(name="hello kitty", weight=1.8, sound=[{"sound": "wangwang~"}, {"sound": "yingying ~"}])
# print(dog.dict())
dog1 = Dog(name="hello kitty", weight=1.8, sound=[Sound(sound="wangwang~"), Sound(sound="yingying~")])
print(dog1.dict())
print("\033[31m5. --- ORM模型:从类实例创建符合ORM对象的模型 ---\033[0m")
Base = declarative_base()
class CompanyOrm(Base):
__tablename__ = 'companies'
id = Column(Integer, primary_key=True, nullable=False)
public_key = Column(String(20), index=True, nullable=False, unique=True)
name = Column(String(63), unique=True)
domains = Column(ARRAY(String(255)))
class CompanyModel(BaseModel):
id: int
public_key: constr(max_length=20)
name: constr(max_length=63)
domains: List[constr(max_length=255)]
class Config:
orm_mode = True
co_orm = CompanyOrm(
id=123,
public_key='foobar',
name='Testing',
domains=['example.com', 'foobar.com'],
)
print(CompanyModel.from_orm(co_orm))查看全部 -
ASGI协议的服务
Uvicorn
Hypercorn
Daphne
WSGI协议的服务
uWSGI
Gunicorn
查看全部 -
Starlette, Pydantic和Python的关系
查看全部
举报