Skip to main content

Small package to easily parse an .ini file.

Project description

configini

Small package to easily parse an .ini file.

Basic usage

Read values

config.ini:

[test]
str=test
int=1
float=1.0005
dec=1.5
bool=no
list=["test"]
dict={"list": [1, 2, 3]}
datetime=2021-04-02T05:44:20
date=2021-04-02
time=05:44

config.py:

import configini

configini.read('filename.ini')

class Config:
    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 file
from config import Config

# Do whatever you want with it.
some_variable = Config.str_value

Note: With the Boolean method, 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 values

Default values will be used when the value inside the configurations file is empty. You can change the default value by passing the default parameter.

configini.String('chapter', 'key', default='Hello world')

Custom values

If 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 variables

Configini 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=alpha

config.ini:

[foo]
bar=beta

output:

>>> 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=alpha

output:

>>> configini.Variable('foo', 'bar', environment_var='BONG')
'alpha'

Ignore errors

If you don't want to deal with invalid formatting in your .ini file. Set the flag configini.ignore_errors to True:

configini.ignore_errors = True

Data types

String

Casts any value to a string, If value is empty None is returned.

configini.String('chapter', 'key', default=None, environment_var=None)

Integer

Casts any value to an int, If value is empty None is returned.
Note: Will raise ValueError, if value is not a valid number, when configini.ignore_errors is set to False.

configini.Integer('chapter', 'key', default=None, environment_var=None)

Float

Casts any value to a float, If value is empty None is returned.
Note: Will raise ValueError, if value is not a valid number, when configini.ignore_errors is set to False.

configini.Float('chapter', 'key', default=None, environment_var=None)

Decimal

Casts any value to a decimal.Decimal, If value is empty None is returned.
Note: Will raise ValueError, if value is not a valid number, when configini.ignore_errors is set to False.

configini.Decimal('chapter', 'key', default=None, environment_var=None)

Boolean

Casts 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)

List

Casts any value to a list.
Note: Will raise json.decoder.JSONDecodeError, if value is not a valid number, when configini.ignore_errors is set to False. Note: Method uses json.loads method to convert string to list, so only use double quotes.

configini.List('chapter', 'key', default=[], environment_var=None)

Dict

Casts any value to a list.
Note: Will raise json.decoder.JSONDecodeError, if value is not a valid number, when configini.ignore_errors is set to False. Note: Method uses json.loads method to convert string to list, so only use double quotes.

configini.List('chapter', 'key', default={}, environment_var=None)

DateTime

Casts 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 raise ValueError, if value is not a valid number, when configini.ignore_errors is set to False.

configini.Decimal('chapter', 'key', format='%Y-%m-%dT%H:%M:%S', default=None, environment_var=None)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

configini-1.1.tar.gz (5.7 kB view hashes)

Uploaded Source

Built Distribution

configini-1.1-py3-none-any.whl (5.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page