Package: configini
configiniSmall package to easily parse an .ini file.Basic usageRead valuesconfig.ini:[test]str=testint=1float=1.0005dec=1.5bool=nolist=["test"]dict={"list": [1, 2, 3]}datetime=2021-04-02T05:44:20date=2021-04-02time=05:44config.py:importconfiginiconfigini.read('filename.ini')classConfig:str_value=configini.String('test','str')int_value=configini.Integer('test','int')float_value=configini.Float('test','float')dec_value=configini.Decimal('test','dec')bool_value=configini.Boolean('test','bool')list_value=configini.List('test','list')dict_value=configini.Dict('test','dict')datetime_value=configini.DateTime('test','test_datetime')date_value=configini.DateTime('test','test_date',format='%Y-%m-%d')time_value=configini.DateTime('test','test_time',format='%H:%M:%S')Then you can use your config variables like this:main.py:# Import the config filefromconfigimportConfig# Do whatever you want with it.some_variable=Config.str_valueNote:With theBooleanmethod, 1, 'true', 'yes' will be cast to True,Note:Only use double quote for strings inside list, the json.loads() method is used for parsing to list.Default valuesDefault values will be used when the value inside the configurations file is empty.
You can change the default value by passing thedefaultparameter.configini.String('chapter','key',default='Hello world')Custom valuesIf you have a custom value that you want automatically to be cast to the right data type,
You can use the parent 'Variable' method with parameter data_type to any method to cast your value.An example with uuid:configini.Variable('chapter','key',data_type=UUID)Environment variablesConfigini also supports .env variables.In the following example the chapter is called 'foo', and the key is 'bar'.
It will first try and fetch the environment variable called 'FOO_BAR' before the .ini variables..env:FOO_BAR=alphaconfig.ini:[foo]bar=betaoutput:>>>configini.Variable('foo','bar')'alpha'If you want to use a different environment variable name then the chapter_key format you can use the environment_var parameter:.env:BONG=alphaoutput:>>>configini.Variable('foo','bar',environment_var='BONG')'alpha'Ignore errorsIf you don't want to deal with invalid formatting in your .ini file.
Set the flagconfigini.ignore_errorsto True:configini.ignore_errors=TrueData typesStringCasts any value to a string, If value is empty None is returned.configini.String('chapter','key',default=None,environment_var=None)IntegerCasts any value to an int, If value is empty None is returned.Note: Will raiseValueError, if value is not a valid number, whenconfigini.ignore_errorsis set to False.configini.Integer('chapter','key',default=None,environment_var=None)FloatCasts any value to a float, If value is empty None is returned.Note: Will raiseValueError, if value is not a valid number, whenconfigini.ignore_errorsis set to False.configini.Float('chapter','key',default=None,environment_var=None)DecimalCasts any value to a decimal.Decimal, If value is empty None is returned.Note: Will raiseValueError, if value is not a valid number, whenconfigini.ignore_errorsis set to False.configini.Decimal('chapter','key',default=None,environment_var=None)BooleanCasts any value to a bool.Returns True if value is 1, 'true' or 'yes'.Returns False if value is 0, 'false' or 'no'.Else value will be None.configini.Boolean('chapter','key',default=None,environment_var=None)ListCasts any value to a list.Note: Will raisejson.decoder.JSONDecodeError, if value is not a valid number, whenconfigini.ignore_errorsis set to False.Note: Method usesjson.loadsmethod to convert string to list, so only use double quotes.configini.List('chapter','key',default=[],environment_var=None)DictCasts any value to a list.Note: Will raisejson.decoder.JSONDecodeError, if value is not a valid number, whenconfigini.ignore_errorsis set to False.Note: Method usesjson.loadsmethod to convert string to list, so only use double quotes.configini.List('chapter','key',default={},environment_var=None)DateTimeCasts any value to a datetime.datetime, If value is empty None is returned.Use the format parameter to change how the datetime is converted.Note: Will raiseValueError, if value is not a valid number, whenconfigini.ignore_errorsis set to False.configini.Decimal('chapter','key',format='%Y-%m-%dT%H:%M:%S',default=None,environment_var=None)