一个毛巾包装的configparser api。

figs的Python项目详细描述


图是一个库,用于轻松读取类似于ini的配置文件。无花果 利用python标准库中的ConfigParser模块。

我个人不太喜欢ConfigParserapi,所以我写了这篇文章。这个 想法是开发人员在使用 这个库,即一个直观的api。

用法

如果您熟悉PyYaml或标准库的json模块, 你应该很熟悉下面的内容。

要加载配置,请使用load/loads函数。以下 返回相同的值:

>>> # Takes a filename
>>> conf = figs.load('config.ini')

>>> # or a file-like object
>>> conf = figs.load(open('config.ini'))

>>> # Takes a string to be parsed
>>> conf = figs.loads('''\
        [universe]
        answer = 42
        is_active = yes
        status = expanding
        ''')

并且,到dump配置:

>>> # Takes a filename
>>> figs.dump(conf, 'config.ini')

>>> # or a file-like object
>>> figs.dump(conf, open('config.ini'))

>>> # Dump to string
>>> figs.dumps(conf)
[universe]
answer = 42
is_active = yes
status = expanding

>>> # You can also dump just a section
>>> figs.dumps(conf.universe)
answer = 42
is_active = yes
status = expanding

这些是fig模块中您应该关注的唯一功能 和。

一旦你有了配置对象,你是如何使用它的?惊喜惊喜!不管怎样你 感觉舒适:)

措辞

我知道,你只需要从配置文件中获取一个属性的dict就可以了 它。让我们看看你能不能猜出这是怎么做到的?:

>>> # Returns a dict like {'section-name': <Section object>}
>>> dict(conf)

>>> # Returns a dict like {'key': <TypeableStr object>}
>>> dict(conf.universe)

您应该记住,dicton不会not自动执行dict 在它的截面对象上。TypeableStr类是^{tt10}的子类$ 添加了一些方法(as_boolas_intas_float)。

不过,如果你想要听写,你也可以这样做。

>>> figs.as_dict(conf)

>>> # or when loading
>>> conf = figs.load('config.ini', as_dict=True)

loads方法还接受as_dict参数。请注意 as_dict作为关键字参数。

访问

>>> conf.universe.answer
u'42'
>>> conf.universe.answer.as_int
42
>>> conf.universe.is_active
u'yes'
>>> conf.universe.is_active.as_bool
True
>>> conf.universe.status
u'expanding'
>>> conf.universe['status']
u'expanding'

与上面所示的as_int类似,还有as_bool(布尔值 转换过程类似于ConfigParser.getbooleandoes)和 as_float

检查是否存在

>>> 'universe' in conf
True
>>> 'multiverse' in conf
False
>>> 'answer' in conf.universe
True
>>> 'is_active' in conf.universe
True
>>> 'is-active' in conf.universe
False

修改配置

设置新选项…:

>>> conf.universe.is_active = False
>>> conf.universe.planet_maker = 'Magrathea'
>>> conf.universe['earth-owners'] = 'mice'
>>> figs.dumps(conf)
[universe]
answer = 42
is_active = false
status = expanding
planet_maker = Magrathea
earth-owners = mice

…关于新章节:

>>> conf.multiverse.is_active = True
>>> figs.dumps(conf)
[universe]
answer = 42
is_active = false
status = expanding

[multiverse]
is_active = true

删除

这个api很无聊,不是吗?:

>>> del conf.universe.answer
>>> del conf.multiverse

现在怎么办?

好吧,如果你有生活,继续生活吧。说真的,没有别的了 正在读取配置文件。

作者

沙拉特先生(http://sharats.me)。@sharat87在twitter上。

许可证

麻省理工学院许可证(http://mit.sharats.me)。

贡献

代码可在github repository找到。克隆。修改。发送拉取请求。 如果修改相当大,我希望您先打开一个github issue 讨论一下。

变更日志

0.1.2
移动到github。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
junit有没有办法在Java中重新初始化静态类?   在浏览器中点击应用程序时java Play框架挂起   文件Java错误中的NullPointerException   使用Java中的SNMP查找网络中计算机的登录名   java包装服务器引导程序已弃用,有什么替代方案?   当客户在等待理发时,java信号量值是否存在问题?   java如何使用JavaMail仅下载特定类型的附件   如何在java中将十进制转换为十六进制   java Slick2D粒子系统不会生成粒子   java检测更改事件来自何处   将Java集合类型参数类设置为数组   java如何从eclipse导出为可运行JAR文件?   java EntityManager对象未注入Glassfish和Spring   swing从actionPerformed和actionListener Java返回字符串   java在给定另一个等价键对象的情况下获取映射项的当前键   无论输入如何,java网络都会产生相同的输出