Skip to main content

LightConfig is a simple library to make user read or write config

Project description

The LightConfig library enables you to get a easy way to create or read config file

Installation

LightConfig is conveniently available via pip:

pip install lightconfig

or installable via git clone and setup.py

git clone https://github.com/daassh/LightConfig.git
python setup.py install

Usage

Import

from lightconfig import LightConfig

Create

cfg = LightConfig("config.ini")

(if specific path not exists, the path file will be create)

Read / Write

Usually, you can read/write config by both attribute and item

read/write by attribute

>>> cfg.section.option = 'value'
>>> cfg.section.option
'value'

read/write by item

>>> cfg['section']['option'] = 'value'
>>> cfg['section']['option']
'value'

write by section

>>> cfg.section1 = {'option1': 'value1'}
>>> cfg['section2'] = {'option2': 'value2'}
>>> cfg
{'section1': {'option1': 'value1'}, 'section2': {'option2': 'value2'}}

But in some situation, you can only use read/write by item:

section/option name can’t be python variable name

wrong

>>> cfg.section-2.2option = 'value2'
  ...
SyntaxError: invalid syntax

right

>>> cfg['section-2']['2option'] = 'value2'

section/option name is keys or __dict__

keys and __dict__ are inner method of LightConfig (keys used to make LightConfig object dictable, __dict__ used to visit other inner method of LightConfig), so when using keys or __dict__ as section/option name, you can only access it by item:

wrong

>>> cfg.keys.option3 = 'value3'
  ...
AttributeError: 'method' object has no attribute 'option3
>>> cfg.keys
<bound method LightConfig.keys of ...>
>>> cfg.__dict__.option4 = 'value4'
  ...
AttributeError: 'dict' object has no attribute 'option4'
>>> cfg.__dict__
{'_config_path': '', '_try_encoding': {}, ...}

right

>>> cfg['keys'].option3 = 'value3'
>>> cfg['keys']
{'option3': 'value3'}
>>> cfg['__dict__'].option4 = 'value4'
>>> cfg['__dict__']
{'option4': 'value4'}

Delete

use del to delete section/option:

>>> del cfg.section.option
>>> 'option' in cfg.section
False
>>> del cfg['section']
>>> 'section' in cfg
False

Dictable

use dict to convert LightConfig or LightConfig.Section object to dict:

>>> type(dict(cfg))
<class 'dict'>
>>> type(dict(cfg.section))
<class 'dict'>

Iterable

>>> for section_name, section_info in cfg:
...     print(section_name)
...     for option, value in section_info:
...         print('  {}={}'.format(option, value))
section1
  option1=value1
section2
  option2=value2

Project details


Download files

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

Files for lightconfig, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size lightconfig-0.1.2-py2.py3-none-any.whl (7.2 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page