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中的json IoT模块GET请求   在Java8中,如何使用lambda表达式中的条件?   Java 7中的io,如何通过一个实际示例演示文件之间的差异。存在和文件。不存在?   在Android Studio中使用Java从url获取JSON?   返回k个最小元素的java数组   java如何让我的安卓应用通过facebook登录并获得foursquare等好友列表   java如何在布局中以编程方式生成文本视图   在java中更改系统日期和时间的datetime代码段   java Hibernate和SQLite3,一对多关系映射   java Observable转换成kotlin问题   java在单独的线程中将VBO发送到GPU   属性文件中值的spring Java验证程序注释   java Spring启动:找不到javassist   java SimpleDataFormat时间解析   无法在linux上为Java程序正确设置PATH变量   将文件上载到FTP服务器时发生java异常   基于java TestNG的Selenium测试不能并行运行   java DAO类实例与DAO EJB   tomcat6部署*。使用Java Web Start(JNLP)的war应用程序