安装:pip install munch

用法参考:https://github.com/Infinidat/munch

Munch is a dictionary that supports attribute-style access, a la JavaScript.意思是支持"a.b"的写法获取属性值

In []: from munch import Munch

In []: b=Munch()

In []: b.hello='world'

In []: b
Out[]: Munch({'hello': 'world'}) In []: b.hello
Out[]: 'world' In []: b.hello+="!" In []: b
Out[]: Munch({'hello': 'world!'}) In []: b.fool=Munch(lol=True) In []: b
Out[]: Munch({'hello': 'world!', 'fool': Munch({'lol': True})}) In []: b.fool
Out[]: Munch({'lol': True}) In []: b.fool.lol
Out[]: True In []: b['fool']
Out[]: Munch({'lol': True}) In []: b['fool']['lol']
Out[]: True In []: b['fool'].lol
Out[]: True

自然也是支持字典的各种用法:

In []: b.keys()
Out[]: dict_keys(['hello', 'fool']) In []: b.update({'ponies':'are prettty!'},hello=) In []: b
Out[]: Munch({'hello': , 'ponies': 'are prettty!', 'fool': Munch({'lol': True})}) In []: print(repr(b))
Munch({'hello': , 'ponies': 'are prettty!', 'fool': Munch({'lol': True})}) In []: [ (k,b[k]) for k in b ]
Out[]: [('hello', ), ('ponies', 'are prettty!'), ('fool', Munch({'lol': True}))] In []: "The {knights} who say {ni}!".format(**Munch(knights='lolcats', ni='can haz'))
Out[]: 'The lolcats who say can haz!' In []: "The {knights} who say {ni}!".format(**{'knights':'test','ni':'wo'})
Out[]: 'The test who say wo!'

使用json和yaml实现序列化:

In []: b = Munch(foo=Munch(lol=True), hello=, ponies='are pretty!')

In []: import json

In []: json.dumps(b)
Out[]: '{"hello": 42, "ponies": "are pretty!", "foo": {"lol": true}}' In []: import yaml In []: yaml.dump(b)
Out[]: '!munch.Munch\nfoo: !munch.Munch {lol: true}\nhello: 42\nponies: are pretty!\n' In []: yaml.safe_dump(b)
Out[]: 'foo: {lol: true}\nhello: 42\nponies: are pretty!\n'

Default Values

DefaultMunch instances return a specific default value when an attribute is missing from the collection. Like collections.defaultdict, the first argument is the value to use for missing keys:

>>> undefined = object()
>>> b = DefaultMunch(undefined, {'hello': 'world!'})
>>> b.hello
'world!'
>>> b.foo is undefined
True DefaultMunch.fromDict() also takes the default argument: >>> undefined = object()
>>> b = DefaultMunch.fromDict({'recursively': {'nested': 'value'}}, undefined)
>>> b.recursively.nested == 'value'
True
>>> b.recursively.foo is undefined
True Or you can use DefaultFactoryMunch to specify a factory for generating missing attributes. The first argument is the factory: >>> b = DefaultFactoryMunch(list, {'hello': 'world!'})
>>> b.hello
'world!'
>>> b.foo
[]
>>> b.bar.append('hello')
>>> b.bar
['hello']

dict扩展munch,支持yaml文件的更多相关文章

  1. SpringBoot源码学习系列之@PropertySource不支持yaml读取原因

    然后,为什么@PropertySource注解默认不支持?可以简单跟一下源码 @PropertySource源码: 根据注释,默认使用DefaultPropertySourceFactory类作为资源 ...

  2. SpringBoot系列之@PropertySource读取yaml文件

    SpringBoot系列之@PropertySource支持yaml文件读取 最近在做实验,想通过@PropertySource注解读取配置文件的属性,进行映射,习惯上用properties都是测试没 ...

  3. php重新编译,gd扩展支持jpeg文件

    晚上写东西的时候,报了一个错误: Call to undefined function imagecreatefromjpeg() 没有开启 jpeg 支持?原来是默认安装的 gd 扩展默认不支持 j ...

  4. SpringBoot自定义注解@YamlPropertySource加载yml或者yaml文件(扩展了@PropertySource)

    1:概述 SpringBoot的@PropertySource注解只支持加载 properties结尾的文件.当使用@ConfigurationProperties 注解配合@EnableConfig ...

  5. YAML文件简介

    编程免不了要写配置文件,怎么写配置也是一门学问. YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便. 本文介绍 YAML 的语法,以 JS-YAML 的实现为例.你可以去 ...

  6. day5模块学习--yaml文件处理

    yaml文件处理(http://pyyaml.org/wiki/PyYAMLDocumentation)     摘要: 本文讲的是yaml在python上的使用教程详解, YAML是一种容易人类阅读 ...

  7. kubernetes实战篇之helm示例yaml文件文件详细介绍

    系列目录 前面完整示例里,我们主要讲解helm打包,部署,升级,回退等功能,关于这里面的文件只是简单介绍,这一节我们详细介绍一下这里面的文件,以方便我们参照创建自己的helm chart. Helm ...

  8. yaml文件解析详解

    前言 yaml文件是什么?yaml文件其实也是一种配置文件类型,相比较ini,conf配置文件来说,更加的简洁,操作也更加简单,同时可以存放不同类型的数据,不会改变原有数据类型,所有的数据类型在读取时 ...

  9. 使用ruamel.yaml库,解析yaml文件

    在实现的需求如下: 同事提供了一个文本文件,内含200多个host与ip的对应关系,希望能在k8s生成pod时,将这些对应关系注入到/etc/hosts中. 网上看文档,这可以通过扩充pod中的hos ...

随机推荐

  1. 018 jquery中的事件

    一:事件 1.Dom的两种加载方式 2.程序 略 二:事件绑定 1.事件绑定介绍 2.程序一(使用click的原始方式) <!DOCTYPE html> <html> < ...

  2. 多线程学习笔记二之JUC组件

    目录 概述 JUC锁框架图 使用内置锁还是JUC显示锁? 概述   为了对共享资源提供更细粒度的同步控制,JDK5新增了java.util.concurrent(JUC)并发工具包,并发包新增了Loc ...

  3. Redis高可用之主从复制实践(四)

    0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...

  4. 链表用途&&数组效率&&链表效率&&链表优缺点

    三大数据结构的实现方式 数据结构 实现方式 栈  数组/单链表 队列  数组/双端链表 优先级队列 数组/堆/有序链表 双端队列 双向链表 数组与链表实现方式的比较 数组与链表都很快 如果能精确预测栈 ...

  5. Android Socket

    Android Socket 参考资料 菜鸟教程 怎么理解TCP的面向连接和UDP的无连接 https://www.cnblogs.com/xiaomayizoe/p/5258754.html htt ...

  6. 无线网络实体图生成工具airgraph-ng

    无线网络实体图生成工具airgraph-ng   airgraph-ng是aircrack-ng套件提供的一个图表生成工具.该工具可以根据airodump工具生成的CSV文件绘制PNG格式的图.绘制的 ...

  7. Nginx配置直接php

    nginx配置文件: server { listen 80; server_name localhost; access_log /var/log/nginx/log/host.access.log ...

  8. 使用CefSharp在.Net程序中嵌入Chrome浏览器(九)——性能问题

    在使用CEF的过程中,我发现了一个现象:WPF版的CEF比Chrome性能要差:一些有动画的地方会掉帧(例如,CSS动画,全屏图片拖动等),视频播放的效果也没有Chrome流畅. 查了一下相关资料,发 ...

  9. STM32F4 Timer Internal Trigger Connection

    The Timers can be cascaded to make more complex timing relationships, or longer periods. Internally ...

  10. Oracle绝对值函数

    1.绝对值:abs() select abs(-2) value from dual; 2.取整函数(大):ceil() select ceil(-2.001) value from dual;(-2 ...