get_ini
Module for intelligent INI file parsing with automatic type conversion.
This module extends Python’s configparser to provide automatic type inference for configuration values. It supports:
Standard INI file parsing
Automatic conversion of strings to appropriate Python types
Safe evaluation of complex data structures (lists, dicts, etc.)
Fallback to string values when type conversion fails
Functions
Read and parse an INI file with automatic type conversion. |
Classes
Enhanced ConfigParser with automatic type inference. |
- class get_ini.MyConfigParser(defaults=None, dict_type=<class 'dict'>, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section='DEFAULT', interpolation=<object object>, converters=<object object>)[source]
Enhanced ConfigParser with automatic type inference.
This class extends the standard ConfigParser to automatically convert string values to appropriate Python types using ast.literal_eval. It safely handles both simple types (int, float, bool) and complex types (lists, dicts, tuples).
The conversion is attempted using ast.literal_eval, which safely evaluates strings containing Python literals. If conversion fails, the original string value is returned unchanged.
- get(section, option, *, raw=False, vars=None, fallback=<object object>)[source]
Get an option value with automatic type conversion.
This method extends the standard ConfigParser.get() by attempting to convert string values to appropriate Python types.
- Parameters:
- Returns:
The option value converted to its appropriate Python type, or the original string if conversion fails
- Return type:
any
- get_ini.get_ini(filename)[source]
Read and parse an INI file with automatic type conversion.
This function reads an INI configuration file and returns a parser that automatically converts values to appropriate Python types. It uses MyConfigParser to handle type inference, making it ideal for scientific applications with complex configuration needs.
- Parameters:
filename (
str
) – Path to the INI configuration file- Returns:
Configured parser instance with loaded and type-converted values
- Return type:
Notes
The function supports all standard INI file features plus: - Section-based organization - Key-value pairs with automatic type conversion - Complex data structures (lists, dicts, etc.) - Fallback to string values when conversion fails