Created
December 22, 2023 12:47
-
-
Save lelambonzo/d8f9b8c3d2dcc8a5d4da7a7e79164aa0 to your computer and use it in GitHub Desktop.
Module to parse the configuration file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import configparser | |
def parse_config_file(file_path): | |
config = configparser.ConfigParser() | |
config.read(file_path) | |
settings = {} | |
# Parsing integers | |
for key, value in config['General'].items(): | |
if value.isdigit(): | |
settings[key] = int(value) | |
# Parsing floats | |
for key, value in config['General'].items(): | |
try: | |
settings[key] = float(value) | |
except ValueError: | |
pass | |
# Parsing booleans | |
true_values = ['true', 'yes', '1'] | |
false_values = ['false', 'no', '0'] | |
for key, value in config['General'].items(): | |
if value.lower() in true_values: | |
settings[key] = True | |
elif value.lower() in false_values: | |
settings[key] = False | |
# Parsing strings | |
for key, value in config['General'].items(): | |
if key not in settings: | |
settings[key] = value | |
return settings | |
# Using the function to parse the configuration file | |
config_settings = parse_config_file('config.ini') | |
print(config_settings) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment