好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > python munch庫的使用解析

python munch庫的使用解析

熱門標簽:河北防封卡電銷卡 應電話機器人打電話違法嗎 地圖標注線上如何操作 電銷機器人的風險 400電話辦理哪種 天津電話機器人公司 手機網頁嵌入地圖標注位置 開封語音外呼系統代理商 開封自動外呼系統怎么收費

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

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

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

>>> profile.name
'iswbm'

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

1. 安裝方法

使用如下命令進行安裝

$ python -m pip install munch

2. 簡單示例

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

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

并實現了點式賦值與訪問,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. 設置返回默認值

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

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

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

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

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

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

5. 工廠函數自動創建key

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

>>> 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 格式的字符串對象
轉換成 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"}'

轉換成 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

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

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

您可能感興趣的文章:
  • python 實用工具狀態機transitions
  • 簡單理解Python中基于生成器的狀態機
  • 狀態機的概念和在Python下使用狀態機的教程
  • 淺談python中常用的excel模塊庫
  • Python 中拼音庫 PyPinyin 用法詳解
  • 教你使用Python pypinyin庫實現漢字轉拼音
  • Python爬蟲基礎之selenium庫的用法總結
  • python爬蟲之selenium庫的安裝及使用教程
  • python狀態機transitions庫詳解

標簽:宿遷 常州 蘭州 成都 六盤水 駐馬店 江蘇 山東

巨人網絡通訊聲明:本文標題《python munch庫的使用解析》,本文關鍵詞  python,munch,庫,的,使用,解析,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python munch庫的使用解析》相關的同類信息!
  • 本頁收集關于python munch庫的使用解析的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 《禁忌2》HD高清| 吃奶呻吟嗯啊高潮动态漫画| 香港精点一极毛片免费看| 中国男男同志gayxxxx| 欧美专区在线| 小妖精含紧一点喂饱你| 久久久久久精品免费免费浪潮av| 饭岛爱高清无删减年青女教师| 宝宝进去就不疼了视频直播| 潘甜甜七夕果冻传媒在线观看| tube100欧美性| 双性大乳浪受高辣h文np总攻 | jk制服黑色丝袜被啪视频| 天干天干天夜夜爽啪| xxxxx1819| 永久在线免费| 人人草人人爱| 国产精品亚洲AV三去一片红| 精品秘?一区性综合三区| 717影院理论午夜伦HD| 久久久久久久久网站| 欧美午夜剧场| 国产香蕉网| 男女下面蹭蹭视频| 在线观看国产精品色花堂| 精品国产三级在线观看| 电影扒开粉嫩小泬猛然进去| 最大成人vr无码网站在线播放| 免费动漫无遮挡曰批视频软件| 婷婷国产偷v国产偷v亚洲| 跪在白丝女王脚下添臭脚| 久久久久久久久精品麻豆电影| 91洗浴技师嫖妓在线播放| 亚洲情a成黄在线观看| 免费岛国片在线观看x片喷水| 水蜜桃8844| 做爰无遮挡全过程免费的软件| 国产片毛片| 国产精品综合久成人| 免费无码无遮挡永久色情聊天小说| 这么大怎么放进去呀下一句|