Create dataclasses backed by configuration files.
Project description
zycelium.dataconfig
Create dataclasses backed by configuration files.
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.
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 .
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
Built Distribution
File details
Details for the file zycelium.dataconfig-0.0.1.tar.gz
.
File metadata
- Download URL: zycelium.dataconfig-0.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed36dcd572d8688bdf0acb35987238136e1082e753fc6049dadbc34be07f35a8 |
|
MD5 | f28224c8189740aa0486b89890cc0722 |
|
BLAKE2b-256 | 30a5dbfa921842345b51bee9d4daa1f75902ec52432162a292f9ebe7c6202f3d |
File details
Details for the file zycelium.dataconfig-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: zycelium.dataconfig-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c198ccec2d1dcaac6deac14a5f95a75fd27f14d96ffe70dfdfb86fe9968e4d3 |
|
MD5 | 00ebb7c0a7fbcb3801cad69a647b20cc |
|
BLAKE2b-256 | 57acaaba22bab48af73ce4d14c2cdb8b1aec7ce3189a88b8c049e9dd94488d46 |