Skip to main content

zycelium.dataconfig

Create dataclasses backed by configuration files.

Python package

  • Development Status :: 3 - Alpha

Usage

Use defaults:

Create a new python script and name it example.py

from zycelium.dataconfig import dataconfig


@dataconfig
class Config:
    name: str = "World"

config = Config().load()

print(f"Hello, {config.name}!")

Create a config.ini file in the same directory as example.py

name = "DataConfig"

Finally, from the same directory, run python example.py , your console session should look something like this:

$ python example.py
Hello, DataConfig!

The defaults here are:

  • Config file name: "config.ini"
  • Paths to look for the config file (current working directory): ["."]

Specify file-name for configuration:

from zycelium.dataconfig import dataconfig


@dataconfig(file="custom_config.ini")
class Config:
    name: str = "World"

config = Config().load()

print(f"Hello, {config.name}!")

In this example, we specify the file-name on this line: @dataconfig(file="custom_config.ini") with keyword arguments file="custom_config.ini" passed to @dataconfig().

Specify file-lookup-paths:

from zycelium.dataconfig import dataconfig


@dataconfig(paths=[".", "examples", "/usr/local/etc"])
class Config:
    name: str = "World"

config = Config().load()

print(f"Hello, {config.name}!")

Here, we pass paths=[".", "examples"] to @dataconfig() to specify the paths on filesystem where dataconfig should look for the default "config.ini" file. We can also specify the filename along with the paths. Paths can be relative to current working directory or absolute.

Save configuration to file:

from zycelium.dataconfig import dataconfig

FILE_NAME = "newconfig.ini"

@dataconfig(file=FILE_NAME)
class Config:
    name: str = "World"

config = Config()
config.save()

print(f"Saved config to file: {FILE_NAME}.")

Here, we set the config-file-name while creating the class, when save() is called, it will create the file and save contents of Config.

If we try running the same example again, we will get an error:

FileExistsError: File newconfig.ini exists, refusing to overwrite.

This is to protect us from accidentally overwriting an existing config file. To overwrite it, pass overwrite=True to save() like this:

config.save(overwrite=True)

Frozen configuration:

from zycelium.dataconfig import dataconfig


@dataconfig(frozen=True)
class Config:
    name: str = "World"

config = Config().load(replace=True)

print(f"Hello, {config.name}!")

To load a frozen config, we need to pass replace=True to load(), if we forget, we get the error:

dataclasses.FrozenInstanceError: cannot assign to field 'name'

Once loaded, we cannot overwrite the configuration.

Use with Click Integration for CLI apps:

Here, dataconfig will generate options for click CLI framework, one to add defaults to all options with names that exist in the dataconfig class, overridden by values found in the configuration file. These options can be overridden by passing values as usual to the command line.

There's also a new option added to the command: "--conf", which can be used to specify a different configuration file to load defaults.

And finally, any changes made in the command line are applied to the dataconfig object, but not saved to the configuration file unless the save() method is called later.

Frozen dataconfig does not work with commandline integration.

import click
from zycelium.dataconfig import dataconfig


@dataconfig
class Config:
    name: str = "World"


config = Config()
# No need to load() config when using click_option()


@click.command()
@click.option("--name")
@config.click_option()
def main(name):
    print(f"Hello, {name}!")
    print(f"Hello, {config.name}!")


main()

For more examples:

Read through the tests/ directory, where you will find the expected usage and how and why dataconfig can fail.

Install

From PyPI

pip install zycelium.dataconfig

From source:

git clone https://github.com/zycelium/dataconfig.git
cd dataconfig
pip install -e .

Metadata

Release files for zycelium.dataconfig 0.1.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 zycelium.dataconfig 0.1.1
File Size Uploaded
zycelium.dataconfig-0.1.1.tar.gz 5.2 kB Details

Built distribution (wheel)

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

Total release size: 10.5 kB

Release files / zycelium.dataconfig-0.1.1.tar.gz

Download URL zycelium.dataconfig-0.1.1.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
912e2ca6a0c8f1bc69fee4ec602757e37b799482149b3a46971aab64e21c9a7b
BLAKE2b-256 checksum
How to use checksums
502f27a27c9a1f089c4d24e84c1bc3466d7e9c9ee147ead00bf8ce835d628b56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.10

Release files / zycelium.dataconfig-0.1.1-py3-none-any.whl

Download URL zycelium.dataconfig-0.1.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67d29563d770b14551309153b68b24019a95d48042aeeb358a542cfcb4b774d4
BLAKE2b-256 checksum
How to use checksums
b17f01bb6b06c247d7c5d8ac8fc856d331eed92333b663468ba9a34ed2512b11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.10

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

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