安装: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. java okhttp 发送图片

    @RequestMapping(value="/demo2", method=RequestMethod.POST) @ResponseBody public String dem ...

  2. 027.Zabbix的定期备份

    一 数据库备份及恢复 1.1:全库备份 [root@zabbix01 ~]# mysqldump -uroot -p123456 --opt zabbix > zabbix.sql [root@ ...

  3. com.jcraft.jsch.JSchException: Auth fail

    背景 服务器信息: 服务器A:10.102.110.1 服务器B:10.102.110.2 需要从服务器A通过Sftp传输文件到服务器B. 应用项目中有一个功能,要通个关Sftp进行日志文件的传输,在 ...

  4. Spring-Session实现Session共享Redis集群方式配置教程

    循序渐进,由易到难,这样才更有乐趣! 概述 本篇开始继续上一篇的内容基础上进行,本篇主要介绍Spring-Session实现配置使用Redis集群,会有两种配置方式,一种是Redis-Cluster, ...

  5. iOS技术篇:sizeToFit 和 sizeThatFits 区别

    sizeToFit:会计算出最优的 size 而且会改变自己的size UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , ...

  6. Nessus漏洞扫描教程之使用Nmap工具扫描识别指纹

    Nessus漏洞扫描教程之使用Nmap工具扫描识别指纹 Nmap工具的准备工作 当用户对Nessus工具有清晰的认识后,即可使用该工具实施扫描.但是,在扫描之前需要做一些准备工作,如探测网络中活动的主 ...

  7. BZOJ.2246.[SDOI2011]迷宫探险(DP 记忆化搜索 概率)

    题目链接 求最大的存活概率,DP+记忆化. 用f[s][x][y][hp]表示在s状态,(x,y)点,血量为hp时的存活概率. s是个三进制数,记录每个陷阱无害/有害/未知. 转移时比较容易,主要是在 ...

  8. phpexcel导出excel等比例缩放图片

    list($width, $height, $type, $attr) = getimagesize($img_path); if( $width>100 || $height >100 ...

  9. LPC-Link-II Rev C JTAG

  10. Snmp学习总结(五)——WindowsServer2008安装和配置SNMP

    一.安装SNMP 在Windows Server 2008以及Windows Server 2008 R2中,SNMP是以一个服务器功能的形式存在的,SNMP的安装步骤如下所示: 1.打开[开始]→[ ...