I like to use Python as a configuration file. I have added some security features using AST module to prevent injecting malicious code to config files.
- Python >= 3.6 (fstrings)
pip3 install mpcspy
git clone https://github.com/goktug97/mpcspy
cd mpcspy
python3 setup.py install
- Config File
#!/usr/bin/env python3
import dataclasses
import numpy as np
@dataclasses.dataclass
class Robot(object):
width: float = 1.2 # [m]
height: float = 0.5 # [m]
max_angular_velocity: float = np.radians(40.0) # [rad/s]
- Reading Config
import mpcspy
config = mpcspy.read_config(config_file = 'config',
allowed_modules={'numpy': ['radians'],
'dataclasses': ['dataclass']},
allowed_functions=[],
verbose=True)
print(config.Robot.width)
print(config.Robot.height)
print(config.Robot.max_angular_velocity)
- Config File
#!/usr/bin/env python3
from os import path
dataset_path = path.join('./data/')
- Reading Config
import mpcspy
config = mpcspy.read_config(config_file = 'config',
allowed_modules={'os': ['path'],
'path': ['join']},
allowed_functions=[],
verbose=True)