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})}
Metadata
Release files for yayc 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| yayc-0.0.2.tar.gz | 4.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| yayc-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.6 kB
Release files / yayc-0.0.2.tar.gz
| Download URL | yayc-0.0.2.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3f34e42ed71d7508ae2ab6f3d585a8ab939970e7310710037acacd7ad8491527
|
|
BLAKE2b-256 checksum How to use checksums |
d9043a0be128ee466fbf3f0bfeb7ef3e551bee5652e3f795f8befc2830a594f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.1
|
Release files / yayc-0.0.2-py3-none-any.whl
| Download URL | yayc-0.0.2-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
99686b7a2f262234adf434856947b40c6a1e31057a81ca38354e2e9ba4949b86
|
|
BLAKE2b-256 checksum How to use checksums |
191e67ef4b5c76403a6ae2b8f1a43bfdfe23caf5c148ed3038c5ad7f5e398e74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.1
|