dict扩展munch,支持yaml文件
安装: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文件的更多相关文章
- SpringBoot源码学习系列之@PropertySource不支持yaml读取原因
然后,为什么@PropertySource注解默认不支持?可以简单跟一下源码 @PropertySource源码: 根据注释,默认使用DefaultPropertySourceFactory类作为资源 ...
- SpringBoot系列之@PropertySource读取yaml文件
SpringBoot系列之@PropertySource支持yaml文件读取 最近在做实验,想通过@PropertySource注解读取配置文件的属性,进行映射,习惯上用properties都是测试没 ...
- php重新编译,gd扩展支持jpeg文件
晚上写东西的时候,报了一个错误: Call to undefined function imagecreatefromjpeg() 没有开启 jpeg 支持?原来是默认安装的 gd 扩展默认不支持 j ...
- SpringBoot自定义注解@YamlPropertySource加载yml或者yaml文件(扩展了@PropertySource)
1:概述 SpringBoot的@PropertySource注解只支持加载 properties结尾的文件.当使用@ConfigurationProperties 注解配合@EnableConfig ...
- YAML文件简介
编程免不了要写配置文件,怎么写配置也是一门学问. YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便. 本文介绍 YAML 的语法,以 JS-YAML 的实现为例.你可以去 ...
- day5模块学习--yaml文件处理
yaml文件处理(http://pyyaml.org/wiki/PyYAMLDocumentation) 摘要: 本文讲的是yaml在python上的使用教程详解, YAML是一种容易人类阅读 ...
- kubernetes实战篇之helm示例yaml文件文件详细介绍
系列目录 前面完整示例里,我们主要讲解helm打包,部署,升级,回退等功能,关于这里面的文件只是简单介绍,这一节我们详细介绍一下这里面的文件,以方便我们参照创建自己的helm chart. Helm ...
- yaml文件解析详解
前言 yaml文件是什么?yaml文件其实也是一种配置文件类型,相比较ini,conf配置文件来说,更加的简洁,操作也更加简单,同时可以存放不同类型的数据,不会改变原有数据类型,所有的数据类型在读取时 ...
- 使用ruamel.yaml库,解析yaml文件
在实现的需求如下: 同事提供了一个文本文件,内含200多个host与ip的对应关系,希望能在k8s生成pod时,将这些对应关系注入到/etc/hosts中. 网上看文档,这可以通过扩充pod中的hos ...
随机推荐
- chrome书签(收藏栏)的导入导出
- 010.KVM虚机冷迁移
一 实验环境 原虚机名称:vm01-centos6.8 原虚机所在宿主机:kvm-host-2 迁移后虚机名称:vm01-cloud-centos6.8 迁移后虚机所在宿主机:kvm-host-2 二 ...
- rsync基础
参考资料:骏马金龙的rsync系列.该博主的博文质量很好,推荐大家关注. 环境 操作系统:CentOS Linux release 7.5.1804 (Core) 软件:rsync version ...
- 【BZOJ-3532】Lis 最小割 + 退流
3532: [Sdoi2014]Lis Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 704 Solved: 264[Submit][Status] ...
- 【Tsinsen-A1486】树(王康宁) 点分治 + Trie
A1486. 树(王康宁) 时间限制:1.0s 内存限制:512.0MB 总提交次数:455 AC次数:97 平均分:52.62 查看未格式化的试题 提交 试题讨论 试题来源 ...
- VMware 使用本机代理上网
灰机使用方法 VMware 安装方法 首先解决主机的配置 1.查询本机 IP 地址,使用 ipconfig /all 2.更改小灰机的设置 3.虚拟机设置 4.Ubuntu 设置
- ICE::Handle 使用崩溃问题
简单例子如下: #include "Ice/Ice.h" #include "IceUtil/IceUtil.h" #include "Printer ...
- 何时调用C++复制构造函数和拷贝构造函数(转)
1. 何时调用复制构造函数 复制构造函数用于将一个对象复制到新创建的对象中.也就是说,它用于初始化过程中,而不是常规的赋值过程中.类的复制构造函数原型通常如下: class_name(const cl ...
- .net中实现RSS方法
引用 如何在.net动态网站中实现RSS呢?主要思想是编写一个能够自动按照RSS格式生成xml文档的通用类.具体步骤如下: 步骤一:创建RSS通用类 C#代码 using System; usin ...
- Technical Information ARM-related JTAG / SWD / SWV / ETM Target Interfaces
https://www.computex.co.jp/eg/products/pdf/technical_pdf/arm_if01_gijutsu_eng.pdf