Skip to main content

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")

Metadata

Release files for pyscfg 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyscfg 0.2.1
File Size Uploaded
pyscfg-0.2.1.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyscfg 0.2.1
File Interpreter ABI Platform
pyscfg-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / pyscfg-0.2.1.tar.gz

Download URL pyscfg-0.2.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b6201ff76c46b9492f1f3a2cfd89b0cb474dc506f144da370a565aee2307e072
BLAKE2b-256 checksum
How to use checksums
39f9d0213856007dc506f0c7111762f0e1eb51cb12810381d9af2cf2e61a64b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.9

Release files / pyscfg-0.2.1-py3-none-any.whl

Download URL pyscfg-0.2.1-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e0b8bba31cd2e4606dcbea885f5732c48e4a284151e34f201ceb41ed6b24f6d
BLAKE2b-256 checksum
How to use checksums
6724cc400a168e73f0c8455ede955317bd67ed77304782a4025460d11a9b2ead
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.9

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page