欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pydantic?resolve解決嵌套數(shù)據(jù)結(jié)構(gòu)生成痛點(diǎn)分析

 更新時(shí)間:2023年04月07日 11:13:47   作者:allmonday  
這篇文章主要為大家介紹了pydantic?resolve解決嵌套數(shù)據(jù)結(jié)構(gòu)生成痛點(diǎn)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

案例

以論壇為例,有個(gè)接口返回帖子(posts)信息,然后呢,來了新需求,說需要顯示帖子的 author 信息。

此時(shí)會有兩種選擇:

在 posts 的 query 中 join 查詢 author 信息,在返回 post 中添加諸如 author_id, author_name 之類的字段。

{'post': 'v2ex', 'author_name': 'tangkikodo'}

根據(jù) posts 的 ids , 單獨(dú)查詢 author 列表,然后把 author 對象循環(huán)添加到 post 對象中。

{'post':'v2ex', 'author': {'name': 'tangkikod'}}

方法 1 中,需要去修改 query, 還需要修改post的schema. 如果未來要加新字段,例如用戶頭像的話,會需要修改兩處。

方法 2 需要手動(dòng)做一次拼接。之后增減字段都是在 author 對象的范圍內(nèi)修改。

所以相對來說, 方法 2 在未來的可維護(hù)性會比較好。用嵌套對象的方式可以更好的擴(kuò)展和維護(hù)。

方法2 的返回結(jié)構(gòu)

[
  {
    "id": 1,
    "post": "v2ex",
    "author": {
      "name": "tangkikodo",
      "id": 1
    }
  },
  {
    "id": 2,
    "post": "v3ex",
    "author": {
      "name": "tangkikodo2",
      "id": 1
    }
  }
]

然而需求總是會變化,突然來了一個(gè)新的且奇怪的需求,要在 author 信息中添加數(shù)據(jù),顯示他最近瀏覽過的帖子。返回體變成了:

[
  {
    "id": 1,
    "post": "v2ex",
    "author": {
      "name": "tangkikodo",
      "recent_views": [
        {
          "id": 2,
          "post": "v3ex"
        },
        {
          "id": 3,
          "post": "v4ex"
        }
      ]
    }
  }
]

那這個(gè)時(shí)候該怎么弄呢?血壓是不是有點(diǎn)上來了。

根據(jù)之前的方法 2, 通常的操作是在獲取到authors信息后, 關(guān)聯(lián)查找author的recent_posts, 拼接回authors, 再將 authors 拼接回posts。 流程類似層層查找再層層回拼。 偽代碼類似:

# posts query
posts = query_all_posts()
# authors query
authors_ids = fetch_unique_author_id(posts)  
authors = query_author(author_ids)
recent_view_posts = fetch_recent_review_posts(author_ids)  # 新需求
recent_view_maps = calc_view_mapping(recent_view_posts)    # 新需求
# authors attach
authors = [attach_posts(a, recent_view_maps) for a in authors]
author_map = calc_author_mapping(authors)
# posts attach
posts = [attach_author(p, author_map) for p in posts]

莫名的會聯(lián)想到callback hell, 添加新的層級都會在代碼中間部分切入。

反正想想就挺麻煩的對吧。要是哪天再嵌套一層呢? 代碼改起來有點(diǎn)費(fèi)勁, 如果你此時(shí)血壓有點(diǎn)高,那請繼續(xù)往下看。

那,有別的辦法么? 這里有個(gè)小輪子也許能幫忙。

解決方法

祭出一個(gè)小輪子: allmonday/pydantic-resolve

以剛才的例子,要做的事情抽象成兩部分:

  • 定義 dataloader ,負(fù)責(zé)查詢和group數(shù)據(jù)。前半部分是從數(shù)據(jù)庫查詢,后半部分是將數(shù)據(jù)轉(zhuǎn)成 pydantic 對象后返回。 偽代碼,看個(gè)大概意思就好。
class AuthorLoader(DataLoader):
    async def batch_load_fn(self, author_ids):
        async with async_session() as session:
            # query authors
            res = await session.execute(select(Author).where(Author.id.in_(author_ids)))
            rows = res.scalars().all()
            # transform into pydantic object
            dct = defaultdict(dict)
            for row in rows:
                dct[row.author_id] = AuthorSchema.from_orm(row)
            # order by author_id
            return [dct.get(k, None) for k in author_ids]
class RecentViewPostLoader(DataLoader):
    async def batch_load_fn(self, view_ids):
        async with async_session() as session:
            res = await session.execute(select(Post, PostVisit.visitor_id)  # join 瀏覽中間表
                .join(PostVist, PostVisit.post_id == Post.id)
                .where(PostVisit.user_id.in_(view_ids)
                .where(PostVisit.created_at < some_timestamp)))
            rows = res.scalars().all()
            dct = defaultdict(list)
            for row in rows:
                dct[row.visitor_id].append(PostSchema.from_orm(row))  # group 到 visitor
            return [dct.get(k, []) for k in view_ids]
  • 定義 schema, 并且注入依賴的 DataLoaders, LoaderDepend 會管理好loader 的異步上下文緩存。
class RecentPostSchema(BaseModel):
    id: int
    name: str
    class Config:
        orm_mode = True
class AuthorSchema(BaseModel):
    id: int
    name: str
    img_url: str
    recent_views: Tuple[RecentPostSchema, ...] = tuple()
    def resolve_recent_views(self, loader=LoaderDepend(RecentViewPostLoader)):  
        return loader.load(self.id)
    class Config:
        orm_mode = True
class PostSchema(BaseModel):
    id: int
    author_id: int
    name: str
    author: Optional[AuthorSchema] = None
    def resolve_author(self, loader=LoaderDepend(AuthorLoader)):
         return loader.load(self.author_id)
    class Config:
        orm_mode = True

然后呢?

然后就沒有了,接下來只要做個(gè) post 的查詢, 再簡單地...resolve 一下,任務(wù)就完成了

posts = (await session.execute(select(Post))).scalars().all()
posts = [PostSchema.from_orm(p) for p in posts]
results = await Resolver().resolve(posts)

在拆分了 loader 和 schema 之后,對數(shù)據(jù)地任意操作都很簡單,添加任意新的schema 都不會破壞原有的代碼。

完整的案例可以查看 6_sqlalchemy_loaderdepend_global_filter.py

如果之前使用過aiodataloader 的話會知道,開發(fā)需要手動(dòng)維護(hù)loader在每個(gè)request 中的初始化過程 , 但在 pydantic-resolve 中你完全不用操心異步上下文的創(chuàng)建,不用維護(hù)DataLoader的實(shí)例化, 一切都在pydantic-resolve的管理之中。

就完事了。如果必須說有啥缺點(diǎn)的話。。必須用 async await 可能算一個(gè)。

該項(xiàng)目已經(jīng)在我司的生產(chǎn)環(huán)境中使用,并且保持了100%的測試覆蓋率。 歡迎大家嘗鮮體驗(yàn),如果遇到問題歡迎發(fā)issue,我會盡快修復(fù)。

以上就是pydantic resolve解決嵌套數(shù)據(jù)結(jié)構(gòu)生成痛點(diǎn)分析的詳細(xì)內(nèi)容,更多關(guān)于pydantic resolve嵌套數(shù)據(jù)結(jié)構(gòu)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 如何建立一個(gè)超圖詳解

    如何建立一個(gè)超圖詳解

    這篇文章主要介紹了如何建立一個(gè)超圖,如果你想學(xué)習(xí)圖像處理,這盤文章可能對你有一點(diǎn)幫助,需要的朋友可以參考下
    2021-04-04
  • python反轉(zhuǎn)(逆序)字符串的6種方法詳細(xì)

    python反轉(zhuǎn)(逆序)字符串的6種方法詳細(xì)

    這篇文章主要介紹了python反轉(zhuǎn)(逆序)字符串的6種方法詳細(xì),需要的朋友可以參考下
    2021-04-04
  • 基于python實(shí)現(xiàn)簡單網(wǎng)頁服務(wù)器代碼實(shí)例

    基于python實(shí)現(xiàn)簡單網(wǎng)頁服務(wù)器代碼實(shí)例

    這篇文章主要介紹了基于python實(shí)現(xiàn)簡單網(wǎng)頁服務(wù)器代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • python中元類用法實(shí)例

    python中元類用法實(shí)例

    這篇文章主要介紹了python中元類用法實(shí)例,對于學(xué)習(xí)Python有不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-10-10
  • Python 如何在字符串中插入變量

    Python 如何在字符串中插入變量

    這篇文章主要介紹了Python 如何在字符串中插入變量,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • pandas 將索引值相加的方法

    pandas 將索引值相加的方法

    今天小編就為大家分享一篇pandas 將索引值相加的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python中的selenium入門超詳細(xì)教程

    python中的selenium入門超詳細(xì)教程

    這篇文章主要介紹了python中的selenium入門超詳細(xì)教程,本文是在python環(huán)境下使用selenium,使用瀏覽器是Chrome,系統(tǒng)是win10系統(tǒng),需要的朋友可以參考下
    2023-11-11
  • Matplotlib實(shí)戰(zhàn)之柱狀圖繪制詳解

    Matplotlib實(shí)戰(zhàn)之柱狀圖繪制詳解

    柱狀圖,是一種使用矩形條,對不同類別進(jìn)行數(shù)值比較的統(tǒng)計(jì)圖表,這篇文章主要為大家詳細(xì)介紹了如何使用Matplotlib繪制柱狀圖,需要的可以參考下
    2023-08-08
  • 如何用tempfile庫創(chuàng)建python進(jìn)程中的臨時(shí)文件

    如何用tempfile庫創(chuàng)建python進(jìn)程中的臨時(shí)文件

    這篇文章主要介紹了如何用tempfile庫創(chuàng)建python進(jìn)程中的臨時(shí)文件,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • Python打印特殊符號及對應(yīng)編碼解析

    Python打印特殊符號及對應(yīng)編碼解析

    這篇文章主要介紹了Python打印特殊符號及對應(yīng)編碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05

最新評論