profig模块

作者: wit92 | 来源:发表于2020-06-12 22:47 被阅读0次

profig是一个简单的Python配置库。

1.profig模块特点
  • 自动值转换。
  • 部分嵌套。
  • 类似Dict的访问。
  • 单文件模块,没有依赖项。
  • 可扩展的输入/输出格式。
  • 内置支持INI文件和Windows注册表。
  • 保留INI文件的排序和注释。配置文件
  • 完整的Unicode支持。
  • 支持Python 2.7+和3.2+。
2.安装
 pip install profig
3.应用举例

配置文件server.cfg内容如下

[server]
host = 192.168.1.1
port = 9090

首先,我们指定要预期的默认值和类型:

>>> cfg = profig.Config('server.cfg')
>>> cfg.init('server.host', 'localhost')
>>> cfg.init('server.port', 8080)

然后,我们将当前状态与配置文件的状态同步:

>>> cfg.sync()

正如所料,我们可以直接访问更新的值而无需过度努力:

本地主机有,就用本地主机文件的内容;本地主机没有, 就用程序现写进去的

>>> cfg['server.host']
'192.168.1.1'

或者按部分。请注意,端口选项的类型保留:

>>> server_cfg = cfg.section('server')
>>> server_cfg['port']
9090
4.常用API接口

(1).init(key, default, type=None, comment=None)

将键初始化为给定的默认值。
如果类型未提供,则默认值的类型将被使用。
如果已经为key部分设置了一个值,则会强制它键入。
如果提供了注释,则可以以与活动一致的方式将其写入配置文件Format。

Initializes key to the given default value.
If type is not provided, the type of the default value will be used.
If a value is already set for the section at key, it will be coerced to type.
If a comment is provided, it may be written out to the config file in a manner consistent with the active Format.

(2).read(*sources, **kwargs)

读取配置值。
如果提供了来源,请从这些来源中读取。否则,写入源中sources。一种用于格式 源可以使用设置格式。

Reads config values.
If sources are provided, read only from those sources. Otherwise, write to the sources in sources. A format for sources can be set using format.

(3)sync(*sources, **kwargs)

从源读取并将任何更改写回第一个源。
如果提供了源,则仅同步这些源。否则,同步源sources。
format可用于覆盖用于从源读取/写入的格式。

Reads from sources and writes any changes back to the first source.
If sources are provided, syncs only those sources. Otherwise, syncs the sources in sources.
format can be used to override the format used to read/write from the sources.

(4)get(key, default=None)

如果key存在,则返回该值。 否则,返回默认值。
如果未给出default,则默认为None,因此此方法永远不会引发异常。

If key exists, returns the value. Otherwise, returns default.
If default is not given, it defaults to None, so that this method never raises an exception.

相关文章

  • python常用模块!!

    os模块: stat模块: sys模块: hashlib,md5模块: random模块: types模块: at...

  • 2018-08-19

    Angular 2 技能图谱 模块 自定义模块 根模块 特性模块 共享模块 核心模块 内置模块 Applicati...

  • 【时间管理100讲】精髓全在这里啦

    理论模块 精力管理。 行动管理。 学习模块。 高空模块。 反思模块。 运动模块。 阅读模块。 旅行模块。 人际关系...

  • python基础学习(三)

    常用模块 String模块 数学模块 随机模块 OS模块 os.path模块 re模块 常用函数及操作 列表操作 ...

  • day10-异常处理和pygame显示

    一、异常处理 1.模块 导入模块(自定义模块,第三方模块)import 模块 ---->模块.内容from 模块 ...

  • 重点知识复习(异常处理)

    1.模块 导入模块(自定义模块,第三方模块,系统其他模块)import 模块 ----> 模块.内容from 模...

  • Python常用模块

    Python常用模块之time模块 Python常用模块之os模块 Python常用模块之sys模块 Python...

  • nodejs-模块

    nodejs模块 一、nodejs模块分类 1.核心模块 Core Module、内置模块、原生模块 fs模块 p...

  • Python不同网络模块网页源代码的获取

    requests模块 或者使用 selenium模块 BeautifulSoup模块 urllib模块

  • day10 pygame

    一、异常处理1.模块导入模块(自定义模块,第三方模块)import 模块 ---->模块.内容from 模块 im...

网友评论

    本文标题:profig模块

    本文链接:https://www.haomeiwen.com/subject/nxktxktx.html