This post builds on the third step to create a Raspberry Pi Cluster Node to store our configuration settings in a config file. Here we move all the configuration settings in our script into a useful .cfg file using the python ConfigParser.
Why use Configuration Files?
When developing a system there will always be variables you will want to tweak based on the machine its running on. These are typically simple settings such as the machine name, connection settings or passwords.
Ideally these settings are not hardcoded into our code but are outside the codebase. One major reason for doing this is because you do not want to hardcode passwords into your codebase. So developers typically save these important settings in configuration files. The scripts then read these configuration files to find the details they need.
In our previous script we hardcoded the IP address and socket port number used. This means that if we wanted to change the IP or port we would need to modify the code. This doesn’t make the script very flexible.
In this tutorial I am going to remove the hard-coded IP address and port number and use a configuration file.
Using the ConfigParser Module
Python has the
ConfigParser module that reads a file and loads the settings. This module loads files in the ini format. These are basic text files with specific formatting that allows a program to read in settings.
An example file format, as taken from the python docs, is shown below:
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
old_passwords = 1
The file is split up into sections, denoted by the square brackets. Each section then has a series of variables which may be set to a value with the equals sign.
We are going to use this format with the ConfigParser python module.
Changing out code to use ConfigParser
First I am going to create the config parser configuration file. For this I have chosen to have two sections, one for the master and one for the slaves. The example file is below.
socket_port = 12345
socket_port = 12345
master_ip = 127.0.0.1
Now I have the file, the first thing I need to do is load up the config file in my scripts. This is performed by importing ConfigParser and creating a new instance of the config as follows:
config = ConfigParser.ConfigParser()
I place the import statement with all the other import statements at the top of the file. Then I create an instance of the ConfigParser and tell it to read my config file. This reads in the file and all configuration options.
Now it has loaded my config file up I can get out specific parts of the configuration
master_ip = config.get("slave", "master_ip")
socket_port = config.getint("slave", "socket_port")
In the above code I have used two different methods of the config variable defined above. Both of these functions take two parameters, the section name of the config and the config name. The most basic one is
get which gets the config value as a string.
getint gets the value of the config but also converts it to an integer. I have used
getint for the port of the socket as this is required to be an integer. In addition to this there is also
getboolean which converts the value to a float and boolean respectively.
For the future I am going to store more values in the configuration settings. This means I can get away from hardcoding variables in my script and make it more flexible.
The full code is available on Github, any comments or questions can be raised there as issues or posted below.