Skip to main content

simple handling of user configuration

Project description

pyscfg - Python Simple Configuration

A simple solution to store/load/modify configurations for your Python project.

version: 0.2.0
contributors: suppetia

Setup

installing via pip

pip install pyscfg

installing from source

git clone https://github.com/suppetia/pyscfg.git
cd pyscfg
python setup.py install

Getting started

This library allows to work with configurations which are supposed to endure a restart of a program (nearly) as simple as working with dictionaries. Access/update/remove configurations stored in a file using the "bracket-syntax" like using a dictionary and the library will handle all the I/O and keep the configs-file up to date.
At this state of development YAML and JSON files are supported to store the configurations.
! All configuration keys must be strings and if they contain a dot (".") they are interpreted as multilevel keys.

To get started initialize a SimpleConfigs instance:

from pyscfg import SimpleConfigs

# uses file 'configs.yml' (default) to store the configurations
cfg = SimpleConfigs()
# To store the configs in a specific file, pass the path to configuration file.
cfg = SimpleConfigs("data/configs.yml")

If data/configs.yml looks like

a:
    b: value
    c: value2
d: value3

'value' can be accessed using any of the following ways

cfg["a.b"]
cfg["a"]["b"]
cfg.get("a.b")
cfg["a"].get("b")
cfg.get("a").get("b")
cfg.get("a")["b"]

Calling

cfg["a.d"]

will raise a KeyError while

cfg["a"].get("d")                # returns None
cfg["a"].get("d", default=None)  # returns None
cfg["a"].get("d", default="foo") # returns "foo"

returns the default value if the key is not stored in the configs.

To extract a group of configs (a layer in the configs file) as a dictionary use

cfg["a"].as_dict()  # or
dict(cfg["a"])

In the same way updating or adding new configuration keys works:

cfg["a.b"] = "new_value"
cfg["a"]["b"] = "new_value"
...
cfg["e"] = "another new value"
...

# to add a group of parameters use a dict:
cfg["f"] = {"g": 1, "h": 1e10}

After these changes the data/configs.yml file will look like this:

a:
    b: new value
    c: value2
d: value3
e: another new value

To remove a key use one of the following ways:

cfg.remove("e")
del cfg["e"]

# to retrieve the item to be removed or to safely remove a key use dict-like pop-method
cfg.pop("e")                             # returns "another new value"
cfg.pop("non existing key")              # returns None
cfg.pop("non existing key", default=42)  # returns 42

Default values stored in a dictionary can be passed to the constructor. These values will be assigned if the keys aren't in the configurations already. The configurations file is updated.
This might be useful to create a configurations file for an app for the first time and these configs might be changed by the user later.

from pyscfg import SimpleConfigs


defaults = {
    "foo": "John",
    "bar": "Doe",
    "num": 0
}

# uses file 'configs.yml' (default) to store the user configurations
cfg = SimpleConfigs(defaults=defaults)

print(cfg["num"]) # prints 0

if user_chooses_this_option:
    cfg["num"] = 1
else:
    cfg["num"] = -1

print(cfg["num"]) # prints 1 or -1

You can also write your defaults into a configuration file (supported: YAML, JSON) and pass it to the constructor.

# uses file 'default_cfg.yml' in subdirectory 'data' to load the default configuration values
cfg = SimpleConfigs(defaults="data/default_cfg.yml")

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

pyscfg-0.2.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

pyscfg-0.2.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file pyscfg-0.2.1.tar.gz.

File metadata

  • Download URL: pyscfg-0.2.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for pyscfg-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b6201ff76c46b9492f1f3a2cfd89b0cb474dc506f144da370a565aee2307e072
MD5 e12d9859016090f761bc938f8e832c17
BLAKE2b-256 39f9d0213856007dc506f0c7111762f0e1eb51cb12810381d9af2cf2e61a64b0

See more details on using hashes here.

File details

Details for the file pyscfg-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyscfg-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for pyscfg-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e0b8bba31cd2e4606dcbea885f5732c48e4a284151e34f201ceb41ed6b24f6d
MD5 ab58b86fa25aa24c8b39c14336c04b05
BLAKE2b-256 6724cc400a168e73f0c8455ede955317bd67ed77304782a4025460d11a9b2ead

See more details on using hashes here.

Supported by

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