1.FastAPI是应该用于构建API的现代,快速(高性能)的 web 框架,使用Python 3.6+ 并基于标准的 Python 类型提示。关键性:

Pydantic : 前端给后端传数据/定义的数据类型,嵌套用什么格式嵌套requests.asyncio与trio后端兼容。
ASGI 协议WSGI 协议Uvicorn 与 uWSGI 为web服务器
class User(BaseModel): id: int # 没有默认值。就是必填字段 name: str = "john Snow" # 有默认值,就是选填字段 signup_ts: Optional[datetime] = None # 时间 "有默认值,选填字段" friends: List[int] = [] # 列表中元素是int类型或者可以直接转换成int类型# 传值 类型= 字典external_data = { "id": "123", "signup_ts": "2022-12-22 12:22", "friends": [1, 2, "3"] # "3" 是可以int("3")的}user = User(**external_data)print(user.id, user.friends) # 实例化后调用属性print(repr(user.signup_ts))print(user.dict())"123 [1, 2, 3]datetime.datetime(2022, 12, 22, 12, 22){'id': 123, 'name': 'john Snow', 'signup_ts': datetime.datetime(2022, 12, 22, 12, 22), 'friends': [1, 2, 3]}"print("---效验失败处理---")try: User(id=1, signup_ts=datetime.today(), friends=[1, 2, "not number"])except ValidationError as e: print(e.json())"[ { "loc": [ "friends", 2 ], "msg": "value is not a valid integer", "type": "type_error.integer" }]"print(user.dict()) # 转换成字典print(user.json()) print(user.copy()) # 这里代表浅拷贝print(User.parse_obj(obj=external_data)) # 解析print(User.parse_raw('{"id": "123","signup_ts": "2022-12-22 12:22", "friends": [1, 2, 3]}'))"{'id': 123, 'name': 'john Snow', 'signup_ts': datetime.datetime(2022, 12, 22, 12, 22), 'friends': [1, 2, 3]}{"id": 123, "name": "john Snow", "signup_ts": "2022-12-22T12:22:00", "friends": [1, 2, 3]}id=123 name='john Snow' signup_ts=datetime.datetime(2022, 12, 22, 12, 22) friends=[1, 2, 3]id=123 name='john Snow' signup_ts=datetime.datetime(2022, 12, 22, 12, 22) friends=[1, 2, 3]id=123 name='john Snow' signup_ts=datetime.datetime(2022, 12, 22, 12, 22) friends=[1, 2, 3]"path = Path('pydantic_tutorial.json')path.write_text('{"id":"123", "signup_ts":"2020-12-22 12:22", "friends":[1, 2, "3"]}')print(User.parse_file(path))"id=123 name='john Snow' signup_ts=datetime.datetime(2020, 12, 22, 12, 22) friends=[1, 2, 3]"print(user.schema())print(user.schema_json())#user_data = {"id": "error", "signup_ts": "2020-12-22 12 22", "friends": [1, 2, 3]} # id必须是strprint(User.construct(**user_data)) # 不效验数据直接创建模型类,不建议在construct方法中传入未经验证的数据#print(User.__fields__.keys()) # 定义模型类的时候,所有字段都注明类型,字段顺序就不会乱。"{'title': 'User', 'type': 'object', 'properties': {'id': {'title': 'Id', 'type': 'integer'}, 'name': {'title': 'Name', 'default': 'john Snow', 'type': 'string'}, 'signup_ts': {'title': 'Signup Ts', 'type': 'string', 'format': 'date-time'}, 'friends': {'title': 'Friends', 'default': [], 'type': 'array', 'items': {'type': 'integer'}}}, 'required': ['id']}{"title": "User", "type": "object", "properties": {"id": {"title": "Id", "type": "integer"}, "name": {"title": "Name", "default": "john Snow", "type": "string"}, "signup_ts": {"title": "Signup Ts", "type": "string", "format": "date-time"}, "friends": {"title": "Friends", "default": [], "type": "array", "items": {"type": "integer"}}}, "required": ["id"]}name='john Snow' signup_ts='2020-12-22 12 22' friends=[1, 2, 3] id='error'dict_keys(['id', 'name', 'signup_ts', 'friends'])"class Sound(BaseModel): sound: strclass Dog(BaseModel): birthday: date weight: float = Optional[None] sound: List[Sound] # 不同的狗有不同的叫声。递归模型(Recursive Model)就是指一个嵌套一个dogs = Dog(birthday=date.today(), weight=6.66, sound=[{"sound": "wang wang ~"}, {"sound": "ying ying ~"}])print(dogs.dict())"{'birthday': datetime.date(2022, 6, 9), 'sound': [{'sound': 'wang wang ~'}, {'sound': 'ying ying ~'}]}"from sqlalchemy import Column, Integer, Stringfrom sqlalchemy.dialects.postgresql import ARRAYfrom sqlalchemy.ext.declarative import declarative_baseBase = 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 CompanyMode(BaseModel): id: int public_key: constr(max_length=20) # constr = 限制 name: constr(max_length=63) domains: List[constr(max_length=255)] class Config: # 子类 orm_mode = Trueco_orm = CompanyOrm( id=123, public_key = 'foobar', name='Testing', domains=['example.com', 'imooc.com'])print(CompanyMode.from_orm(co_orm))"id=123 public_key='foobar' name='Testing' domains=['example.com', 'imooc.com']"