Skip to main content

Two way configurations mapping helper for Python.

Project description

Biconfigs
=========

|Build Status| |Coverage| |Codacy grade| |Python Version| |PyPI| |PyPI|
|License|

📄⇄🛠 Two way configurations mapping helper for Python.

Get Started
-----------

.. code:: python

from biconfigs import Biconfigs
configs = 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' style
configs.options.list.append('example')

Content of file ``configs.json`` after execution:

.. code:: json

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

Install
-------

.. code:: sh

pip install biconfigs

Dependencies
------------

**No dependencies** required 🎉

Tested on Python ``2.6``, ``2.7``, ``3.3``, ``3.4``, ``3.5``, ``pypy``,
``pypy3``

Documentation
-------------

When to save
~~~~~~~~~~~~

- Saving when: *Item create, item delete, list reorder, value change,
``get_set``, etc.*

.. code:: python

# All the following single statement will cause saving
configs['item'] = 'value'
configs['options'] = {}
configs.options['list'] = []
configs.options.list.append('example')
configs.options['list'] = []
configs.options.clear()
value2 = configs.get_set('item2', 45)

- Not saving when: *Item access, assignment but not changed, etc.*

.. code:: python

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

Non-blocking saving
~~~~~~~~~~~~~~~~~~~

By default, Biconfigs use asynchronous saving. You can disable the
feature by setting ``async_write`` to ``False``

.. code:: python

# 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 update
~~~~~~~~~~~~~~~~~~~~~

Normally, 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.

.. code:: python

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

Reload from file
~~~~~~~~~~~~~~~~

Simply use ``reload`` function to reload from file. *Note*: ``reload``
will discard all present data in Biconfigs-object and loads new ones
from file)

.. code:: python

configs.reload()

Parsers
~~~~~~~

Biconfigs use ``Prettified Json`` as default parser. You may want to set
``parser='json'`` to save as compacted json file.

.. code:: python

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

configs.json:

.. code:: json

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

**Available Parsers**

- ``json``: Compact JSON format
- ``pretty-json``: Prettified JSON
- *To be developed...*

License
-------

MIT

.. |Build Status| image:: https://img.shields.io/travis/antfu/biconfigs.svg?style=flat-square
:target: https://travis-ci.org/antfu/biconfigs
.. |Coverage| image:: https://img.shields.io/codecov/c/github/antfu/biconfigs.svg?style=flat-square
:target: https://codecov.io/gh/antfu/biconfigs
.. |Codacy grade| image:: https://img.shields.io/codacy/grade/4bf188eecc374c76b5c6ddbe93315078.svg?style=flat-square
:target: https://www.codacy.com/app/anthonyfu117/biconfigs/dashboard
.. |Python Version| image:: https://img.shields.io/pypi/pyversions/biconfigs.svg?style=flat-square
:target: https://pypi.python.org/pypi/biconfigs
.. |PyPI| image:: https://img.shields.io/pypi/v/biconfigs.svg?style=flat-square
:target: https://pypi.python.org/pypi/biconfigs
.. |PyPI| image:: https://img.shields.io/pypi/status/biconfigs.svg?style=flat-square
:target: https://pypi.python.org/pypi/biconfigs
.. |License| image:: https://img.shields.io/pypi/l/biconfigs.svg?style=flat-square
:target: https://github.com/antfu/biconfigs/blob/master/LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

biconfigs-0.1.0.zip (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

biconfigs-0.1.0-py2.py3-none-any.whl (5.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file biconfigs-0.1.0.zip.

File metadata

  • Download URL: biconfigs-0.1.0.zip
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for biconfigs-0.1.0.zip
Algorithm Hash digest
SHA256 309020ec6bafd4348ef6d88705f3fc535dc7b76346c0cdfc8f8cd20d66d4be6a
MD5 be856bf96647d3318d558ac4a1159454
BLAKE2b-256 aa08a94e60d39eacfccdcf3a1e95a4a675c82d714f3e11003aa9351a75c1f332

See more details on using hashes here.

File details

Details for the file biconfigs-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for biconfigs-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e03aea30bd614acfa6fd1938b0fc16fc64f84fa787fc20a634e4692b07b3b485
MD5 2e42eb4c301ae35e348bb72153b715a8
BLAKE2b-256 70a43e6cdf2d70f379401bbfb5b20a218f4bf59ba3a59234ba71995a604706f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page