亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

高性能 FastAPI 框架入門精講

  • Python的類型提示type hints

    Pydantic是一個基于Python類型提示來定義數據驗證,序列化和

    文檔(使用SON模式)庫

    Starlette是一種輕量級的ASGl框架/工具包,是構建高性能

    Asyncio服務的理想選擇

    查看全部
  • 語言特點:

    1、性能優越;

    2、開發效率高;

    3、減少認為bug50%以上;

    查看全部
  • sqlalchemy


    626723b00001959509600540.jpg
    查看全部
  • 版本問題,與教師版本最好保持一致

    查看全部
  • 需要注意框架的版本與包的版本是否一致,有可能版本不一致導致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.dll

    https://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:


    添加后一切正常。

    查看全部
  • http://img1.sycdn.imooc.com//6195ebc70001426c13670770.jpg

    代碼沒問題,但是運行報錯了

    框架有包的版本兼容問題

    查看全部
  • 思路清晰,講解細致,通俗易懂,項目貫穿知識點,十分?。?! 希望能在出一版大型項目的教程
    查看全部
    0 采集 收起 來源:課程總結

    2021-11-15

  • 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的關系

    http://img1.sycdn.imooc.com//61221cfb000199b614080882.jpg

    查看全部
首頁上一頁1234下一頁尾頁

舉報

0/150
提交
取消
課程須知
任何想學習Python開發的同學,尤其是需要高效率完成高并發、高性能項目的同學都可以學習
老師告訴你能學到什么?
FastAPI 框架特性及性能優勢 如何定義各種請求參數和驗證 模板渲染和靜態文件配置 FastAPI 的表單數據處理 全面學習 FastAPI 依賴注入系統 FastAPI 的安全、認證和授權 大型工程應該如何目錄結構設計 FastAPI 的中間件開發方法和規范 跨域資源共享的原理和實現方式

微信掃碼,參與3人拼團

微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復購買,感謝您對慕課網的支持!