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.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

lightconfig-0.1.3-py2.py3-none-any.whl (7.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file lightconfig-0.1.3-py2.py3-none-any.whl.

File metadata

  • Download URL: lightconfig-0.1.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/2.7.12

File hashes

Hashes for lightconfig-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8a786b7ec19092169397b9f89fe79f5b1fe4b3bea79eb08dcf84e6a7bfdc430d
MD5 ea233b2017f89b5dba2886f1ff5fb9e0
BLAKE2b-256 8d424e6333bd4f14243ffe8334f83a651d782de08c0ca00997d1d29b756fc971

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