Yet another YAML configuration library
Project description
yayc
Yet another YAML-based configurator library. Meant for BurstCube and the COSI Telescope, but maybe useful for other projects.
Reading a config file
from yayc import Configurator
import yayc.test_data
c = Configurator.open(yayc.test_data.path/"config_example.yaml")
c['section:param_int']
1
c['section:param_float']
1.0
c['section:param_string']
'a'
c['section:param_list']
[0, 1, 2, 3]
c['section:param_list2']
['a', 'b', 'c']
Overriding a parameter
Usually you have command line programs like this
my_app -c config_example.yaml --foo bar
You might want to let the user to change the behavior, but also don't want to set a bunch of flags for each option. It's also cumbersome to modify the config file for each run.
You can have a generic flag that the user can use to change any behavior, e.g.
my_app -c config_example.yaml --override "section:param_int = 2" "section:param_string = b"
You can parse this using argparse and pass the input to --override to the Configurator object:
override_input = ["section:param_int = 2", "section:param_string = b"]
c.override(*override_input)
c["section:param_int"]
2
c['section:param_string']
'b'
Relative paths
It is usually desirable to keep other configuration files together with the yaml file and other logs. this makes it easier to reproduce results. To facilitate this, always use paths relative to the config file, not relative to where you executed the progam. Then use this:
c.absolute_path(c['section:param_path'])
PosixPath('/Users/israel/work/software/yayc/yayc/test_data/my_file.txt')
Logs
It's good practice to dump the config file, after any override, to the logs:
with open('test.log', 'w') as f:
f.write(c.dump())
Dynamic initialization
This is a little more advanced. In general, you can initialize any object on the fly if you know the module, the name of the class and the input parameters. We can use this to dynamically instantiate an object of an arbitrary class:
# Creating dummy classes in the current module (__main__)
import __main__
class ClassA:
def __init__(self, *args, **kwargs):
self._args = args
self._kwargs = kwargs
def __repr__(self):
return f"ClassA(args = {self._args}, kwargs = {self._kwargs})"
class ClassB:
def __init__(self, *args, **kwargs):
self._args = args
self._kwargs = kwargs
def __repr__(self):
return f"ClassB(args = {self._args}, kwargs = {self._kwargs})"
Initialize the objet on the fly
objects = {label:getattr(__main__, params['class'])(*params['args'], **params['kwargs']) for label,params in c['dynamic_init'].items()}
print(objects)
{'objectA': ClassA(args = (1, 'a'), kwargs = {'foo': 2, 'bas': 'b'}), 'objectB': ClassB(args = (), kwargs = {'foo': 3})}
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yayc-0.0.2.tar.gz.
File metadata
- Download URL: yayc-0.0.2.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f34e42ed71d7508ae2ab6f3d585a8ab939970e7310710037acacd7ad8491527
|
|
| MD5 |
6eb6b9931569a494e0aa9dc2524440bc
|
|
| BLAKE2b-256 |
d9043a0be128ee466fbf3f0bfeb7ef3e551bee5652e3f795f8befc2830a594f8
|
File details
Details for the file yayc-0.0.2-py3-none-any.whl.
File metadata
- Download URL: yayc-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99686b7a2f262234adf434856947b40c6a1e31057a81ca38354e2e9ba4949b86
|
|
| MD5 |
cd184d86ac151be25ce6a32125151848
|
|
| BLAKE2b-256 |
191e67ef4b5c76403a6ae2b8f1a43bfdfe23caf5c148ed3038c5ad7f5e398e74
|