安装: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. Github如何撤销提交并清除痕迹

    1.在命令行工具中进入项目目录 cd /Users/mac.manon/workspace/QuickCodes 2.sudo git reset --hard HEAD~4 根据提示输入本系统登录密 ...

  2. 【记录】group_concat_max_len

    这几天在帮别人定位一个问题,结果定位了半天都没有结果.redis中取出来的数据很奇怪,每次都不一样,而且总是取不完全. 我以为是redis的内存不够,导致数据丢失,但是不应该啊,这么点数据,也不至于内 ...

  3. mongodb spring 配置文件

    在使用spring-data-mongodb中,需要配置spring文件,如下: mongodb.xml <?xml version="1.0" encoding=" ...

  4. Activity-Flag标志位

    Activity-Flag标志位 学习自 <Android开发艺术探索> 标志位漫谈 var intent: Intent = Intent(this, Test2Activity::cl ...

  5. Vakuum开发笔记02 核心与安全问题

    3.judger核心设计 评测系统最重要部分就是评测核心了(judger).核心judger负责了编译.执行.检查三大部分,也就是评测系统的灵魂所在,因此judger设计的好坏,直接影响到整个评测系统 ...

  6. 喵哈哈村的魔法考试 Round #7 (Div.2) 题解

    喵哈哈村的魔法考试 Round #7 (Div.2) 注意!后四道题来自于周日的hihocoder offer收割赛第九场. 我建了个群:欢迎加入qscoj交流群,群号码:540667432 大概作为 ...

  7. Codeforces Round #370 (Div. 2) B. Memory and Trident 水题

    B. Memory and Trident 题目连接: http://codeforces.com/contest/712/problem/B Description Memory is perfor ...

  8. java读取记事本文件第一个字符遇到的一个坑

    记事本数据是这样的: Faq_faqTitle=常见问题_标题Faq_faqKeyword=关键字Faq_faqDescription=FAQ描述...... 文件编码:utf-8有签名 然后用jav ...

  9. 【原】用IDEA远程Debug Tomcat服务

    [环境参数] Web容器:Tomcat 8.0 IDE:IDEA 2018.1.5 [具体步骤] 1.配置Tomcat容器参数 编辑$CATALINA_HOME/bin/catalina.sh脚本,加 ...

  10. hdoj-2066-一个人的旅行(迪杰斯特拉)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...