Let's discuss some aspects of Python programming that is especially helpful while developing applications using the Raspberry Pi. One such tool is the configparser available in Python. The configparser module (https://docs.python.org/3.4/library/configparser.html) is used to read/write config files for applications.
In software development, config files are generally used to store constants such as access credentials, device ID, and so on In the context of a Raspberry Pi, configparser could be used to store the list of all GPIO pins in use, addresses of sensors interfaced via the I2C interface, and so on. Let's discuss three examples where we learn making use of the configparser module. In the first example we will create a config file using the configparser.
In the second example, we will make use of the configparser to read the config values and in the third example, we will discuss modifying config files in the final example.
Example 1:
In the first example, let's create a config file that stores information including device ID, GPIO pins in use, sensor interface address, debug switch, and access credentials:
import configparser
if __name__ == "__main__":
# initialize ConfigParser
config_parser = configparser.ConfigParser()
# Let's create a config file
with open('raspi.cfg', 'w') as config_file:
#Let's add a section called ApplicationInfo
config_parser.add_section('AppInfo')
#let's add config information under this section
config_parser.set('AppInfo', 'id', '123')
config_parser.set('AppInfo', 'gpio', '2')
config_parser.set('AppInfo', 'debug_switch', 'True')
config_parser.set('AppInfo', 'sensor_address', '0x62')
#Let's add another section for credentials
config_parser.add_section('Credentials')
config_parser.set('Credentials', 'token', 'abcxyz123')
config_parser.write(config_file)
print("Config File Creation Complete")
Let's discuss the preceding code example (available for download along with this chapter as config_parser_write.py) in detail:
- The first step is importing the configparser module and creating an instance of the ConfigParser class. This instance is going to be called config_parser:
config_parser = configparser.ConfigParser()
- Now, we open a config file called raspi.cfg using the with keyword. Since the file doesn't exist, a new config file is created.
- The config file is going to consist of two sections namely AppInfo and Credentials.
- The two sections could be created using the add_section method as follows:
config_parser.add_section('AppInfo')
config_parser.add_section('Credentials')
- Each section is going to consist of different set of constants. Each constant could be added to the relevant section using the set method. The required arguments to the set method include the section name (under which the parameter/constant is going to be located), the name of the parameter/constant and its corresponding value. For example: The id parameter can be added to the AppInfo section and assigned a value of 123 as follows:
config_parser.set('AppInfo', 'id', '123')
- The final step is saving these config values to the file. This is accomplished using the config_parser method, write. The file is closed once the program exits the indented block under the with keyword:
config_parser.write(config_file)
When the preceding code snippet is executed, a config file called raspi.cfg is created. The contents of the config file would include the contents shown as follows:
[AppInfo]
id = 123
gpio = 2
debug_switch = True
sensor_address = 0x62
[Credentials]
token = abcxyz123
Example 2:
Let's discuss an example where we read config parameters from a config file created in the previous example:
import configparser
if __name__ == "__main__":
# initialize ConfigParser
config_parser = configparser.ConfigParser()
# Let's read the config file
config_parser.read('raspi.cfg')
# Read config variables
device_id = config_parser.get('AppInfo', 'id')
debug_switch = config_parser.get('AppInfo', 'debug_switch')
sensor_address = config_parser.get('AppInfo', 'sensor_address')
# execute the code if the debug switch is true
if debug_switch == "True":
print("The device id is " + device_id)
print("The sensor_address is " + sensor_address)
The preceding example is available for download along with this chapter (config_parser_read.py). Let's discuss how this code sample works:
- The first step is initializing an instance of the ConfigParser class called config_parser.
- The second step is loading and reading the config file using the instance method read.
- Since we know the structure of the config file, let's go ahead and read some constants available under the section AppInfo. The config file parameters can be read using the get method. The required arguments include the section under which the config parameter is located and the name of the parameter. For example: The config id parameter is located under the AppInfo section. Hence, the required arguments to the method include AppInfo and id:
device_id = config_parser.get('AppInfo', 'id')
- Now that the config parameters are read into variables, let's make use of it in our program. For example: Let's test if the debug_switch variable (a switch to determine if the program is in debug mode) and print the other config parameters that were retrieved from the file:
if debug_switch == "True":
print("The device id is " + device_id)
print("The sensor_address is " + sensor_address)
Example 3:
Let's discuss an example where we would like to modify an existing config file. This is especially useful in situations where we need to update the firmware version number in the config file after performing a firmware update.
The following code snippet is available for download as config_parser_modify.py along with this chapter:
import configparser
if __name__ == "__main__":
# initialize ConfigParser
config_parser = configparser.ConfigParser()
# Let's read the config file
config_parser.read('raspi.cfg')
# Set firmware version
config_parser.set('AppInfo', 'fw_version', 'A3')
# write the updated config to the config file
with open('raspi.cfg', 'w') as config_file:
config_parser.write(config_file)
Let's discuss how this works:
- As always, the first step is initializing an instance of the ConfigParser class. The config file is loaded using the method read:
# initialize ConfigParser
config_parser = configparser.ConfigParser()
# Let's read the config file
config_parser.read('raspi.cfg')
- The required parameter is updated using the set method (discussed in a previous example):
# Set firmware version
config_parser.set('AppInfo', 'fw_version', 'A3')
- The updated config is saved to the config file using the write method:
with open('raspi.cfg', 'w') as config_file:
config_parser.write(config_file)