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嵌套循环打印一个数字,旁边有相同数量的星号   java JodReports替代方案   java如何将模型类parcelable对象从片段传递到活动?   java JCS缓存删除功能不删除特定元素   如何打开。Java的dll文件。   java在这种情况下如何选择聚合根?   java从CellEditorListener获取编辑的TreeNode   Java到web服务和mysql   从openssl ec在Java中创建公钥时出现加密无效密钥异常   Java数组:添加多个数组的元素时忽略空数组   java在初始化后向fragment发送数据   JButtons的java数组正在返回void   若联接列不包含特定值,则java JPA条件生成器仅返回实体   java ActionBar问题:NPE   java Portlet是被广泛使用还是非常罕见?