python的双向配置映射助手。
biconfigs的Python项目详细描述
_ 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.6,2.7,3.3,3.4,3.5,pypy, pypy3
文档
何时保存
保存时间: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()
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问题
山药
frombiconfigsimportBiconfigsfrombiconfigsimportparser_yamlconfigs=Biconfigs('configs.yml',parser='yaml')# Then use biconfigs as you normally would...
额外要求
要使用yaml,您需要安装额外的要求
pip install biconfigs[yaml]
或者手动安装PyYAML:
pip install PyYAML
许可证
麻省理工学院