python的双向配置映射助手。

biconfigs的Python项目详细描述


Build StatusCoverageCodacy gradePython VersionPyPIPyPI StatusLicense

_ python的双向配置映射助手。

开始

frombiconfigsimportBiconfigsconfigs=Biconfigs('configs.json')# Simply change the dict, and it will automatically save the changes to file.configs['options']={'debug':True,'username':'Anthony','list':[]}# Access with simple 'x.y.z' styleconfigs.options.list.append('example')

执行后文件configs.json的内容:

{"options":{"debug":true,"list":["example"],"username":"Anthony"}}

*biconfig还支持'cson<;'cson>;``和'yaml<;'yaml>;``文件 格式。

安装

pip install biconfigs

依赖关系

无依赖关系必需

在python上测试2.62.73.33.43.5pypypypy3

文档

何时保存

  • 保存时间:item create,item delete,list reorder,value change, ``设置默认值``等

    # All the following single statement will cause savingconfigs['item']='value'configs['options']={}configs.options['list']=[]configs.options.list.append('example')configs.options['list']=[]configs.options.clear()value2=configs.setdefault('item2',45)
  • 在以下情况下不保存:项访问、分配但未更改等

    # All the following single statement will NOT cause savingvalue=configs.itemconfigs['item']='value'# The value of 'item' is not changedvalue3=configs.get('item_not_exists','default_value')

无阻塞保存

默认情况下,biconfig使用异步保存。您可以禁用 通过将async_write设置为False

# set "async_write=False" if your want to use synchronous saving# to ensure your data saved to file in time,# WARNING: In sync mode, your changes will block the incoming statement# until the file correctly saved.configs=Biconfigs('configs.json',async_write=False)configs['item']='value'# Blocking# Configure file saved already# Your code...

高频更新

通常,biconfig会立即将更改写入文件。但是 有时您可能希望经常更新值,这将导致 IO瓶颈。所以可以使用`` with`语句来防止 节省一段时间。

withconfigs:foriinrange(1000):configs['some_key']=i# This statement will execute saving process only one time when exiting "with" scope

从文件重新加载

只需使用reload函数从文件重新加载。注释reload 将丢弃biconfig对象中的所有现有数据并加载新数据 从文件)

configs.reload()

分析器

biconfig使用Prettified Json作为默认解析器。你可能想设置 parser='json'保存为压缩的json文件。

configs=Biconfigs('configs.json',parser='json')configs['item']='value'configs['debug']=False

configs.json:

{"debug":false,"item":"value"}

可用的分析器

  • json:压缩json格式
  • pretty-json:经过美化的json
  • cson:cson格式,请参阅CSON
  • yaml:yaml格式,请参阅YAML

cson

biconfig支持CSONby avakar/pycson

frombiconfigsimportBiconfigsfrombiconfigsimportparser_csonconfigs=Biconfigs('configs.cson',parser='cson')# Then use biconfigs as you normally would...

额外要求

要使用cson,您需要安装额外的要求

pip install biconfigs[cson]

或者手动安装cson

pip install cson

cson问题

请检查avakar/pycson: The Language

山药

biconfig支持YAMLby PyYAML

frombiconfigsimportBiconfigsfrombiconfigsimportparser_yamlconfigs=Biconfigs('configs.yml',parser='yaml')# Then use biconfigs as you normally would...

额外要求

要使用yaml,您需要安装额外的要求

pip install biconfigs[yaml]

或者手动安装PyYAML

pip install PyYAML

许可证

麻省理工学院

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

推荐PyPI第三方库


热门话题
在Java中从本地文件系统导入文件   spring boot如何在Java SpringBoot项目中集成Olingo(Odata)   java查找连续数组中缺少的第k个元素(超过时间限制)   java为什么在mySql中插入1/2行时会得到2/4行   java不能在静态上下文中使用它   File Observer方法的java My onEvent()部分不起作用   java Netty NioSocketChannel在多线程写入时收到中断消息   java将文件夹与父文件夹一起复制   java我的TictaToe代码出了什么问题?如何检查已采取的措施?   java Swing JTable更新   java如何将cordinates查找为int   如何使用selenium和java在firefox中打开新的空选项卡   java Gradle构建输出Jar未运行   java没有GET/WEBINF/jsp/login的映射。jsp