I have an yaml file example
logging:
formatter:"abc"
handler:""
network:
type:"udp"
udp:
ip:"12.3.32.0"
port:"20002"
when i start python program, i am passing config.yaml as command line args using argparse module, i am trying to load yaml file as below
main.py
def load_config(self):
parser = argparse.ArgumentParser(description='my yaml script')
parser.add_argument('-f','--filepath', help='yaml file path')
self.args = parser.parse_args()
with open(args.filepath, 'r') as yamlStream:
try:
self.config = yaml.safe_load(yamlStream)
except yaml.YAMLError as exc:
print(exc)
and when i want to ready loggin i am passing
logger.load_config(self.config["logging"])
logger.py
def load_config(self, config):
self.config = config
logging.config.dictConfig(self.config)
but i do not want to load in the beginning when i start main, i want to pass only file path then load blocks of logging, network of yaml config whenever needed.
There is nothing wrong with reading the whole YAML file and passing the whole structure to the modules. If you pass only the file name, you will read the whole YAML file multiple times which does not really make sense unless you expect it to change between reads.
For readability, you may want to define a class holding the configuration, like this:
import argparse, yaml
class Config:
def __init__(self, filepath):
with open(filepath, 'r') as yamlStream:
raw = yaml.safe_load(yamlStream)
self.logging, self.network, self.udp = raw["logging"], raw["network"], raw["udp"]
parser = argparse.ArgumentParser(description='my yaml script')
parser.add_argument('-f','--filepath', help='yaml file path')
args = parser.parse_args()
config = Config(args.filepath)
Initializing the logger would look like
# called with the Config object, i.e. logger.load_config(config)
def load_config(self, config):
# do you really need to store this?
self.config = config
# access the relevant section
logging.config.dictConfig(config.logging)
By passing the main config object around, you can easily access other sections as you describe in the comments.