Package: biconfigs
📄⇄🛠 Two way configurations mapping helper for Python.Get StartedfrombiconfigsimportBiconfigsconfigs=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')Content of fileconfigs.jsonafter execution:{"options":{"debug":true,"list":["example"],"username":"Anthony"}}*Biconfigs also supports `CSON <#cson>`__ and `YAML <#yaml>`__ file
formats.InstallpipinstallbiconfigsDependenciesNo dependenciesrequired 🎉Tested on Python2.6,2.7,3.3,3.4,3.5,pypy,pypy3DocumentationWhen to saveSaving when:Item create, item delete, list reorder, value change,
``setdefault``, etc.# 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)Not saving when:Item access, assignment but not changed, etc.# 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')Non-blocking savingBy default, Biconfigs use asynchronous saving. You can disable the
feature by settingasync_writetoFalse# 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...High frequency updateNormally, Biconfigs will write the changes to file immediately. But
sometime you may want to update values frequently, which will result in
a IO bottleneck. So you can use``with``statement to prevent auto
saving for a while.withconfigs:foriinrange(1000):configs['some_key']=i# This statement will execute saving process only one time when exiting "with" scopeReload from fileSimply usereloadfunction to reload from file.Note:reloadwill discard all present data in Biconfigs-object and loads new ones
from file)configs.reload()ParsersBiconfigs usePrettified Jsonas default parser. You may want to setparser='json'to save as compacted json file.configs=Biconfigs('configs.json',parser='json')configs['item']='value'configs['debug']=Falseconfigs.json:{"debug":false,"item":"value"}Available Parsersjson: Compact JSON formatpretty-json: Prettified JSONcson: CSON format, refer toCSONyaml: YAML format, refer toYAMLCSONBiconfigs supportsCSONbyavakar/pycsonfrombiconfigsimportBiconfigsfrombiconfigsimportparser_csonconfigs=Biconfigs('configs.cson',parser='cson')# Then use biconfigs as you normally would...Extra requirementsTo use CSON, you need to install extra requirementpipinstallbiconfigs[cson]Or installcsonmanually:pipinstallcsonCSON problemsPlease checkavakar/pycson: The
LanguageYAMLBiconfigs supportsYAMLbyPyYAMLfrombiconfigsimportBiconfigsfrombiconfigsimportparser_yamlconfigs=Biconfigs('configs.yml',parser='yaml')# Then use biconfigs as you normally would...Extra requirementsTo use YAML, you need to install extra requirementpipinstallbiconfigs[yaml]Or installPyYAMLmanually:pipinstallPyYAMLLicenseMIT