This project helps you to import config file writen by YAML to Python data class.
Project description
YAML Data Class Config
This project helps you to import config file writen by YAML to Python Data Classes.
Advantage
- Type safe import from YAML to Data Classes
- Global access and easy unit testing
1. Type safe import from YAML to Data classes
When using pyyaml to import YAML, values be dict and list objects. Using dict or list object will cause such confuses:
- Reference non exist properties for unexpected instance type
- Typo of index or key name
To prevent these confuse, one of good way is to use object as model, and python has a good module Data Classes for this purpose.
2. Global access and easy unit testing
You will want to refer config as global because it's troublesome to pass config value as argument over and over like a bucket brigade.
However, when unit testing, if YAML file was loaded automatically on importing global definition, you will face problem that you can't replace config YAML file with the one for unit testing. YAML Data Class Config can divide timings between definition global instance and loading YAML file so you can replace YAML file for unit testing.
Quickstart
1. Install
pip install yamldataclassconfig
2. Prepare config YAML file
Put config.yml
YAML Data class Config loads config.yml
on Python execution directory by default.
property_a: 1
property_b: '2'
part_config:
property_c: '2019-06-25 13:33:30'
3. Create config class
Anywhere is OK, for example, I prefer to place on myproduct/config.py
from dataclasses import dataclass, field
from datetime import datetime
from dataclasses_json import DataClassJsonMixin
from marshmallow import fields
from yamldataclassconfig.config import YamlDataClassConfig
@dataclass
class PartConfig(DataClassJsonMixin):
property_c: datetime = field(metadata={'dataclasses_json': {
'encoder': datetime.isoformat,
'decoder': datetime.fromisoformat,
'mm_field': fields.DateTime(format='iso')
}})
@dataclass
class Config(YamlDataClassConfig):
property_a: int = None
property_b: str = None
part_config: PartConfig = field(
default=None,
metadata={'dataclasses_json': {'mm_field': PartConfig}}
)
4. Define as global
Also, anywhere is OK, for example, I prefer to place on myproduct/__init__.py
from myproduct.config import Config
CONFIG: Config = Config()
5. Call load before reference config value
from myproduct import CONFIG
def main():
CONFIG.load()
print(CONFIG.property_a)
print(CONFIG.property_b)
print(CONFIG.part_config.property_c)
if __name__ == '__main__':
main()
How do I...
Fix path to yaml file independent on the Python execution directory?
override FILE_PATH
property.
Ex:
from dataclasses import dataclass
from pathlib import Path
from yamldataclassconfig import create_file_path_field
from yamldataclassconfig.config import YamlDataClassConfig
@dataclass
class Config(YamlDataClassConfig):
some_property: str = None
# ...
FILE_PATH: Path = create_file_path_field(Path(__file__).parent.parent / 'config.yml')
Switch target YAML config file to the one for unit testing?
When setup on unit testing, you can call Config.load()
with argument.
Case when unittest:
from pathlib import Path
import unittest
from yourproduct import CONFIG
class ConfigurableTestCase(unittest.TestCase):
def setUp(self):
CONFIG.load(Path('path/to/yaml'))
Case when pytest:
from pathlib import Path
import pytest
from yourproduct import CONFIG
@pytest.fixture
def yaml_config():
CONFIG.load(Path('path/to/yaml'))
yield
def test_something(yaml_config):
"""test something"""
Use path to YAML config file as same as production when test?
fixturefilehandler
can replace config.yml with tests/config.yml.dist easily.
Please call all DeployerFactory.create
with YamlConfigFilePathBuilder
instance argument
to create ConfigDeployer.
Then, set target directory which config.yml should be placed into path_target_directory
.
Case when unittest:
from pathlib import Path
import unittest
from fixturefilehandler.factories import DeployerFactory
from fixturefilehandler.file_paths import YamlConfigFilePathBuilder
from yourproduct import CONFIG
ConfigDeployer = DeployerFactory.create(YamlConfigFilePathBuilder(path_target_directory=Path(__file__).parent.parent))
class ConfigurableTestCase(unittest.TestCase):
def setUp(self):
ConfigDeployer.setup()
CONFIG.load()
def doCleanups(self):
ConfigDeployer.teardown()
Case when pytest:
from pathlib import Path
import pytest
from fixturefilehandler.factories import DeployerFactory
from fixturefilehandler.file_paths import YamlConfigFilePathBuilder
from yourproduct import CONFIG
ConfigDeployer = DeployerFactory.create(YamlConfigFilePathBuilder(path_target_directory=Path(__file__).parent.parent))
@pytest.fixture
def yaml_config():
ConfigDeployer.setup()
CONFIG.load()
yield
ConfigDeployer.teardown()
def test_something(yaml_config):
"""test something"""
Project details
Release history Release notifications | RSS feed
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 yamldataclassconfig-1.5.0.tar.gz
.
File metadata
- Download URL: yamldataclassconfig-1.5.0.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbbaa499875b9187b1446279440aa3e8e1dc1d5878fe46a27272b0d4b70b85a4 |
|
MD5 | 98f3237682f5c0b3347332f4cd0b5741 |
|
BLAKE2b-256 | 1f181d111bfca52fdfba9f83bb01e2c914a3859dd985fb19110f8f5c0e997a72 |
File details
Details for the file yamldataclassconfig-1.5.0-py3-none-any.whl
.
File metadata
- Download URL: yamldataclassconfig-1.5.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b06a5ced890f2e18ec2a42b8e57d6659f2134554eb8402319b22c5993a5725f |
|
MD5 | 296b13419dddddcae24b7d31c6b4184b |
|
BLAKE2b-256 | caacb1460a4331bca86ed9f0b71a025586024a962a90a0b454d6dc10e63854cc |