Small package to apply YAML config to methods and functions
Project description
confyml
Project to allow easy application of config to python methods and functions.
The package allows the user to provide keyword arguments to any decorated function/method. If keyword arguments are passed to the function/method directly in the code this will overwrite any configured behaviour. By having this, one can easily support configurability in applications without the need for code changes or unwieldy config classes that also require code change to alter.
install
Install like any other python package, from pypi
pip install confyml
usage
At the definition of classes, methods and functions, import confyml
and apply the decorator to allow the function to
become configurable.
# example_module.py
from confyml import confyml
config = confyml.get_config()
class ExampleClass:
@config.apply
def __init__(self, kwarg=None):
self.kwarg = kwarg
@config.apply
def example_method(self, arg1, kwarg_2=None):
return arg1, self.kwarg, kwarg_2
@config.apply()
def example_function(arg2, kwarg_3=None):
return arg2, kwarg_3
Create a yaml
file to hold the definition. Default mode for confyml
is mcf
: modules, classes and functions; where
the yaml file has a structure like the one below.
# example.yaml
example_module:
ExampleClass:
kwarg: 'class_kwarg'
example_method:
kwarg_2: 'method_kwarg'
example_function:
kwarg_3: 'function_kwarg'
In the calling script, the application that uses the classes and functions, set the config file to follow.
# main.py
from confyml import confyml
import example_module
confyml.set_config('example.yaml')
e = example_module.ExampleClass()
print(e.example_method('arg1'))
print(example_module.example_function('arg2'))
$ python main.py
arg1 class_kwarg method_kwarg
arg2 function_kwarg
The use should be one config per application.
Keyword arguments provided to the function/method directly will overwrite the config file's behaviour.
# main.py
from confyml import confyml
import example_module
confyml.set_config('example.yaml')
e = example_module.ExampleClass(kwarg='direct_kwarg')
print(e.example_method('arg1'))
print(example_module.example_function('arg2', 'direct_func_kwarg'))
$ python main.py
arg1 direct_kwarg method_kwarg
arg2 direct_func_kwarg
modes
The config can also be provided with a mode
argument,
# main.py
from confyml import confyml
confyml.set_config('<yaml_filepath>', mode='<mode>')
to set the level
of the config. By default the mode is mcf
, which supports a yaml structure of
module:
Class:
<defined_kwargs>
method:
<defined_kwargs>
function:
<defined_kwargs>
mode cf
- classes and methods and/or functions
Class:
<defined_kwargs>
method:
<defined_kwargs>
function:
<defined_kwargs>
mode mf
- modules and functions
module:
function:
<defined_kwargs>
and finally mode f
- functions only
function:
<defined_kwargs>
Use the mode that best suits your application.
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 confyml-2.1.5.tar.gz
.
File metadata
- Download URL: confyml-2.1.5.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e676ffacd3bfc10da2dd5b08a4b7c11b17446b7fdb38aefc01dfe24857c85cff |
|
MD5 | 027827e33cd23520790d74e007f254bb |
|
BLAKE2b-256 | c68e73bfa5a853185823e923a41cbc334ad8df0e89f51336611e0598c7719310 |
File details
Details for the file confyml-2.1.5-py3-none-any.whl
.
File metadata
- Download URL: confyml-2.1.5-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11a64e8116d98df0576af424f173dd4b9184917d154ae28a3b491a9d6b424312 |
|
MD5 | d78ed31f6e09f6de4f4911364c427796 |
|
BLAKE2b-256 | b614ce1f6944b299213e8e6bb37124036e52598653057798996c750a7cf5d936 |