Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading YAML config file in python and using variables

Say I have a yaml config file such as:

test1:
    minVolt: -1
    maxVolt: 1
test2:
    curr: 5
    volt: 5

I can read the file into python using:

import yaml

with open("config.yaml", "r") as f:
    config = yaml.load(f)

Then I can access the variables with

config['test1']['minVolt']

Style-wise, what is the best way to use variables from the config file? I will be using the variables in multiple modules. If I simply access the variables as shown above, if something is renamed, I will need to rename every instance of the variable.

Just wondering what the best or common practices for using variables from a config file in different modules.

like image 644
camerausb Avatar asked Jul 15 '16 20:07

camerausb


People also ask

How to read and write to a YAML file in Python?

For reading and writing to the YAML file, we first need to install the PyYAML package by using the following command. Let’s now dive into read and write operations to the YAML file. We can write data to the YAML file using yaml.dump () method which takes python object and file pointer as parameters.

Can I use YAML as a configuration file?

Although most will only read YAML as a configuration file, it can be very handy to write YAML as well. Example use cases could be: To save state of your program in an easy to read file (instead of using something like Pickle) If you need to convert YAML to JSON, you can simply parse the YAML as we did above.

How to read scalar values from YAML file in Python dictionary?

FullLoader handles the conversion from YAML scalar values to the Python dictionary. To read the value from the file, we write the below code import yaml with open ("tutswiki.yaml", "r") as yamlfile : data = yaml. load (yamlfile, Loader = yaml. FullLoader) print ("Read successful") print (data)

Should I use YAML or PyYaml?

My preference is working with yaml configuration because I usually find very handy and easy to use and I really like that yaml files are also used in e.g. docker-compose configuration so it is something most are familiar with. For yaml parsing I use the PyYAML Python library.


1 Answers

You can do this:

class Test1Class:
    def __init__(self, raw):
        self.minVolt = raw['minVolt']
        self.maxVolt = raw['maxVolt']

class Test2Class:
    def __init__(self, raw):
        self.curr = raw['curr']
        self.volt = raw['volt']

class Config:
    def __init__(self, raw):
        self.test1 = Test1Class(raw['test1'])
        self.test2 = Test2Class(raw['test2'])

config = Config(yaml.safe_load("""
test1:
    minVolt: -1
    maxVolt: 1
test2:
    curr: 5
    volt: 5
"""))

And then access your values with:

config.test1.minVolt

When you rename the values in the YAML file, you only need to change the classes at one place.

Note: PyYaml also allows you to directly deserialize YAML to custom classes. However, for that to work, you'd need to add tags to your YAML file so that PyYaml knows which classes to deserialize to. I expect that you do not want to make your YAML input more complex.

like image 180
flyx Avatar answered Oct 06 '22 21:10

flyx