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

python munch庫的使用解析

 更新時間:2021年05月25日 16:29:11   作者:寫代碼的明哥  
munch是Bunch包的分支,提供類似的功能。99%的工作都是由他完成的,而前叉的創(chuàng)建主要是因為對原始代碼的修復(fù)和維護(hù)缺乏響應(yīng)能力。Munch是支持屬性樣式訪問的字典,本文將講解munch庫的使用

字典是 Python 中基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu)之一,字典的使用,可以說是非常的簡單粗暴,但即便是這樣一個與世無爭的數(shù)據(jù)結(jié)構(gòu),仍然有很多人 "看不慣它" 。
也許你并不覺得,但我相信,你看了這篇文章后,一定會和我一樣,對原生字典開始有了偏見。
我舉個簡單的例子吧
當(dāng)你想訪問字典中的某個 key 時,你需要使用字典特定的訪問方式,而這種方式需要你鍵入 一對中括號 還有 一對引號

>>> profile = dict(name="iswbm")
>>> profile
{'name': 'iswbm'}
>>> profile["name"]
'iswbm'

是不是開始覺得忍無可忍了?
如果可以像調(diào)用對象屬性一樣使用 . 去訪問 key 就好了,可以省去很多多余的鍵盤擊入,就像這樣子

>>> profile.name
'iswbm'

是的,今天這篇文章就是跟大家分享一種可以直接使用 . 訪問和操作字典的一個黑魔法庫 -- munch。

1. 安裝方法

使用如下命令進(jìn)行安裝

$ python -m pip install munch

2. 簡單示例

munch 有一個 Munch 類,它繼承自原生字典,使用 isinstance 可以驗證

>>> from munch import Munch
>>> profile = Munch()
>>> isinstance(profile, dict)
True
>>>

并實現(xiàn)了點式賦值與訪問,profile.name 與 profile['name'] 是等價的

>>> profile.name = "iswbm"
>>> profile.age = 18
>>> profile
Munch({'name': 'iswbm', 'age': 18})
>>>
>>> profile.name
'iswbm'
>>> profile["name"]
'iswbm'

3. 兼容字典的所有操作

本身 Munch 繼承自 dict,dict 的操作也同樣適用于 Munch 對象,不妨再來驗證下
首先是:增刪改查

# 新增元素
>>> profile["gender"] = "male"
>>> profile
Munch({'name': 'iswbm', 'age': 18, 'gender': 'male'})

# 修改元素
>>> profile["gender"] = "female"
>>> profile
Munch({'name': 'iswbm', 'age': 18, 'gender': 'female'})

# 刪除元素
>>> profile.pop("gender")
'female'
>>> profile
Munch({'name': 'iswbm', 'age': 18})
>>>
>>> del profile["age"]
>>> profile
Munch({'name': 'iswbm'})

再者是:一些常用方法

>>> profile.keys()
dict_keys(['name'])
>>>
>>> profile.values()
dict_values(['iswbm'])
>>>
>>> profile.get('name')
'iswbm'
>>> profile.setdefault('gender', 'male')
'male'
>>> profile
Munch({'name': 'iswbm', 'gender': 'male'})

4. 設(shè)置返回默認(rèn)值

當(dāng)訪問一個字典中不存在的 key 時,會報 KeyError 的錯誤

>>> profile = {}
>>> profile["name"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'name'

對于這種情況,通常我們會使用 get 來規(guī)避

>>> profile = {}
>>> profile.get("name", "undefined")
'undefined'

當(dāng)然你在 munch 中仍然可以這么用,不過還有一種更好的方法:使用 DefaultMunch,它會在你訪問不存在的 key 時,給你返回一個設(shè)定好的默認(rèn)值

>>> from munch import DefaultMunch
>>> profile = DefaultMunch("undefined", {"name": "iswbm"})
>>> profile
DefaultMunch('undefined', {'name': 'iswbm'})
>>> profile.age
'undefined'
>>> profile
DefaultMunch('undefined', {'name': 'iswbm'})

5. 工廠函數(shù)自動創(chuàng)建key

上面使用 DefaultMunch 僅當(dāng)你訪問不存在的 key 是返回一個默認(rèn)值,但這個行為并不會修改原 munch 對象的任何內(nèi)容。
若你想訪問不存在的 key 時,自動觸發(fā)給原 munch 中新增你想要訪問的 key ,并為其設(shè)置一個默認(rèn)值,可以試一下 DefaultFactoryMunch 傳入一個工廠函數(shù)。

>>> from munch import DefaultFactoryMunch
>>> profile = DefaultFactoryMunch(list, name='iswbm')
>>> profile
DefaultFactoryMunch(list, {'name': 'iswbm'})
>>>
>>> profile.brothers
[]
>>> profile
DefaultFactoryMunch(list, {'name': 'iswbm', 'brothers': []})

6. 序列化的支持

Munch 支持序列化為 JSON 或者 YAML 格式的字符串對象
轉(zhuǎn)換成 JSON

>>> from munch import Munch
>>> munch_obj = Munch(foo=Munch(lol=True), bar=100, msg='hello')
>>>
>>> import json
>>> json.dumps(munch_obj)
'{"foo": {"lol": true}, "bar": 100, "msg": "hello"}'

轉(zhuǎn)換成 YAML

>>> from munch import Munch
>>> munch_obj = Munch(foo=Munch(lol=True), bar=100, msg='hello')
>>> import yaml
>>> yaml.dump(munch_obj)
'!munch.Munch\nbar: 100\nfoo: !munch.Munch\n  lol: true\nmsg: hello\n'
>>>
>>> print(yaml.dump(munch_obj))
!munch.Munch
bar: 100
foo: !munch.Munch
  lol: true
msg: hello

>>>

建議使用 safe_dump 去掉 !munch.Munch

>>> print(yaml.safe_dump(munch_obj))
bar: 100
foo:
  lol: true
msg: hello

以上就是關(guān)于 munch 的使用全解,替換原生字典絕無問題,munch 的進(jìn)一步封裝使得數(shù)據(jù)的訪問及操作更得更加 Pythonic 了,希望有一天這個特性能夠體現(xiàn)在原生的字典上。

到此這篇關(guān)于python munch庫的使用解析的文章就介紹到這了,更多相關(guān)python munch庫的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python操作MongoDB數(shù)據(jù)庫的方法示例

    Python操作MongoDB數(shù)據(jù)庫的方法示例

    這篇文章主要介紹了Python操作MongoDB數(shù)據(jù)庫的方法,結(jié)合實例形式分析了Python命令行模式下操作MongoDB數(shù)據(jù)庫實現(xiàn)連接、查找、刪除、排序等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • 解決python3.6 右鍵沒有 Edit with IDLE的問題

    解決python3.6 右鍵沒有 Edit with IDLE的問題

    這篇文章主要介紹了解決python3.6 右鍵沒有 Edit with IDLE的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python集合的基礎(chǔ)操作

    Python集合的基礎(chǔ)操作

    這篇文章主要介紹了Python集合的基礎(chǔ)操作,Python中的集合和數(shù)學(xué)上的集合基本是沒有區(qū)別的,是無序的,即不可以使用索引訪問的,集合中是不能出現(xiàn)重復(fù)元素的。想著情了解具體內(nèi)容的小伙伴可以參考下面文章內(nèi)容
    2021-11-11
  • 十分鐘利用Python制作屬于你自己的個性logo

    十分鐘利用Python制作屬于你自己的個性logo

    這篇文章主要給大家介紹了關(guān)于十分鐘如何利用Python制作屬于你自己的個性logo的相關(guān)資料,主要利用的是詞云實現(xiàn)這個效果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友下面來一起看看吧
    2018-05-05
  • python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解

    python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解

    這篇文章主要介紹了python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python中的配對函數(shù)zip()解讀

    Python中的配對函數(shù)zip()解讀

    這篇文章主要介紹了Python中的配對函數(shù)zip()解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 將python2.7添加進(jìn)64位系統(tǒng)的注冊表方式

    將python2.7添加進(jìn)64位系統(tǒng)的注冊表方式

    今天小編就為大家分享一篇將python2.7添加進(jìn)64位系統(tǒng)的注冊表方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程

    python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程

    這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • fastcgi文件讀取漏洞之python掃描腳本

    fastcgi文件讀取漏洞之python掃描腳本

    這篇文章主要介紹了fastcgi文件讀取漏洞之python掃描腳本,需要的朋友可以參考下
    2017-04-04
  • python文件操作的簡單方法總結(jié)

    python文件操作的簡單方法總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于python文件操作的簡單方法知識點,有需要的朋友們可以學(xué)習(xí)下。
    2019-11-11

最新評論