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 ...
随机推荐
- sublime安装package control 的方法
1.安装sublime3后,点击菜单 view>show console,复制以下代码按回车即可,安装时稍微有些卡,需要耐心等待. import urllib.request,os; pf = ...
- C#简单的通用分页
通用分页技术分析 需要返回不同的类型的数据--采用泛型实现该操作 需要提供不同的方法 上一页 上一页 第一页 最后一页 跳转到指定页 Demo 代码 using System; using Syste ...
- Django Model._meta API
Model._meta API是Django ORM的核心,它使得lookups.queries.forms.admin这些模块通过每个model类的_meta的属性可以了解每个model的情况. 1 ...
- 在mysql中使用group by和order by取每个分组中日期最大一行数据
转载自:https://blog.csdn.net/shiyong1949/article/details/78482737 在mysql中使用group by进行分组后取某一列的最大值,我们可以直接 ...
- hdu 5784 How Many Triangles 计算几何,平面有多少个锐角三角形
How Many Triangles 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5784 Description Alice has n poin ...
- Linux下Nginx的监控
一.安装Nginx 使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好. 安装make: yum -y install gcc automake ...
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror(转)
dlopen 基本定义 功能:打开一个动态链接库 包含头文件: #include <dlfcn.h> 函数定义: void * dlopen( const char * pathn ...
- Calculate CAN bit timing parameters
Calculate CAN bit timing parameters TSYNC_SEG === 1 TSEG1 = Prop_Seg + Phase_Seg1 TSEG2 = Phase_Seg2 ...
- ubuntu中chown设置文件权限
参考文献: http://yanwen.org/doc/chown.html http://www.cppblog.com/deercoder/articles/110129.html 可以通过ls ...
- echarts 怎样去掉白色边框线 和怎样去除背景中的网格
echarts怎样去掉白色边框线: echarts怎样去除背景中的网格线