ini_parser.py
日本語版はこちら → README.ja.md
This module is a utility that extends Python's built-in configparser module. It provides enhanced functionality for reading from and writing to INI files, including support for type conversion and default value interpolation.
function
It can get and set configuration information in ini format.
This library is an enhanced parser for INI configuration files that allows you to define type information and default values in advance. When a value is missing from the INI file, the parser automatically fills in the predefined default value. Additionally, string values in the INI file are automatically converted to the specified types (e.g., int, bool, List[str], etc.) based on the provided type definitions.
Usage
import ini_cfg_parser as ini
from typing import List
def get_ini_dict_val(section: str) -> ini.IniDict:
return {
section: {
'lst_language': {'type': List[str], 'inf': ['English','Japanese','Russian','Korean']},
'langage': {'type': int, 'inf': 0},
'CygwinDirectory': {'type': str, 'inf': r'c:\uty\cygwin'},
'ComPort': {'type': int, 'inf': 1},
'lst_Parity': {'type': List[str], 'inf': ['even','odd','none','mark','space']},
'parity': {'type': int, 'inf': 0},
'DataBit': {'type': int, 'inf': 8},
'StopBit': {'type': int, 'inf': 1},
},
}
encoding = "utf8"
ini_file = "config.ini"
section = 'setting1'
default_ini = get_ini_dict_val(section)
ini_parser = ini.IniParser(ini_file, default_ini, encoding)
lst_language = ini_parser.get(section, 'lst_language')
print(f"lst_language={lst_language},type={type(lst_language)}")
langage = ini_parser.get(section, 'langage')
print(f"langage={langage},type={type(langage)}")
print(f"langage setting={lst_language[langage]},type={type(lst_language[langage])}")
CygwinDirectory = ini_parser.get(section, 'CygwinDirectory')
print(f"CygwinDirectory={CygwinDirectory},type={type(CygwinDirectory)}")
ComPort = ini_parser.get(section, 'ComPort')
print(f"ComPort={ComPort},type={type(ComPort)}")
lst_Parity = ini_parser.get(section, 'lst_Parity')
print(f"lst_Parity={lst_Parity},type={type(lst_Parity)}")
parity = ini_parser.get(section, 'parity')
print(f"parity={parity},type={type(parity)}")
print(f"parity setting={lst_Parity[parity]},type={type(lst_Parity[parity])}")
DataBit = ini_parser.get(section, 'DataBit')
print(f"DataBit={DataBit},type={type(DataBit)}")
StopBit = ini_parser.get(section, 'StopBit')
print(f"StopBit={StopBit},type={type(StopBit)}")
If the ini file does not exist, the following config.ini will be created.
# config.ini
[DEFAULT]
lst_language = English,Japanese,Russian,Korean
langage = 0
cygwindirectory = c:\uty\cygwin
comport = 1
lst_parity = even,odd,none,mark,space
parity = 0
databit = 8
stopbit = 1
[setting1]
lst_language = English,Japanese,Russian,Korean
langage = 0
cygwindirectory = c:\uty\cygwin
comport = 1
lst_parity = even,odd,none,mark,space
parity = 0
databit = 8
stopbit = 1
The result of executing the above script.
lst_language=['English', 'Japanese', 'Russian', 'Korean'],type=<class 'list'>
langage=0,type=<class 'int'>
langage setting=English,type=<class 'str'>
CygwinDirectory=c:\uty\cygwin,type=<class 'str'>
ComPort=1,type=<class 'int'>
lst_Parity=['even', 'odd', 'none', 'mark', 'space'],type=<class 'list'>
parity=0,type=<class 'int'>
parity setting=even,type=<class 'str'>
DataBit=8,type=<class 'int'>
StopBit=1,type=<class 'int'>
overview
- The
ini_cfg_parserinstance is initialized with the parameter get_ini_dict_val of type IniDict. - The get_ini_dict_val parameter is a user-defined dictionary structure that specifies default values and their expected types.
# @param[in] ini_path : Path information for the ini file.(type=str)
# @param[in] get_ini_dict_val: This parameter is a user-defined dictionary structure that specifies default values and their expected types.(type=IniDict)
# @param[in] encoding : The type of character code encoding.(type=str)
ini_parser = ini.IniParser(ini_path=ini_file, get_ini_dict_val=default_ini, encoding=encoding)
Section structure
IniDict = Dict[str, Dict[str, IniItem]]
Type of each ini item
class IniItem(TypedDict):
type: IniType
inf: IniValue
Defining a type alias
IniType = Union[Type[str], Type[int], Type[float], Type[bool], Type[List[str]], Type[List[int]], Type[List[float]], Type[List[bool]]]
IniValue = Union[str, int, float, bool, List[str], List[int], List[float], List[bool]]
Error message output
When an error occurs in this library, the function die_print() is used to display a message.
Operation Mode: There are four operation modes.
nSysExit: Executessys.exit(1)to terminate the script.nTkInter: Usestkinterto display a message in a dialog. Then executessys.exit(1)to terminate the script.nException: Raises exceptionIniParserError. The error message is passed as the error information of the exception.(Default)nTkInterException: Usestkinterto display a message in a dialog and then raises the exceptionIniParserError.
Setting the Mode:
Use set_die_mode() to set the mode.
How to set it up:
ini.IniParser.set_die_mode(ini.DieMode.nSysExit)
Tested Environments
| OS | Python Version |
|---|---|
| Windows 11 Pro (64-bit) | 3.8.10 (64-bit) |
| Windows 11 Pro (64-bit) | 3.8.10 (32-bit) |
| WSL2 Ubuntu-24.04 LTS | 3.12.3 (64-bit) |
license
MIT License
author
pukkunk
Metadata
Release files for ini-cfg-parser 0.1.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ini_cfg_parser-0.1.11.tar.gz | 19.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ini_cfg_parser-0.1.11-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 37.8 kB
Release files / ini_cfg_parser-0.1.11.tar.gz
| Download URL | ini_cfg_parser-0.1.11.tar.gz |
|---|---|
| Size | 19.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
db4b1f885671ae9003f9ad0c03fe825a6c37613e09a72280fbd647b2e19c1d41
|
|
BLAKE2b-256 checksum How to use checksums |
303adab262c39cf1ba6780d2b52d73dce338dfbcab41ba6c02dd52d2478c4775
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|
Release files / ini_cfg_parser-0.1.11-py3-none-any.whl
| Download URL | ini_cfg_parser-0.1.11-py3-none-any.whl |
|---|---|
| Size | 18.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
14686ddfeb9723b4cf41488afd7465cc8936dc8bb51d256969634cf288af894d
|
|
BLAKE2b-256 checksum How to use checksums |
82f4f4f3a17f334e0eb21deb7aef3edf731147b924e898978149c37644ec02ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|