-
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的關系
查看全部
舉報