Skip to main content

Small package to easily parse an .ini file.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file configini-1.1.tar.gz.

File metadata

  • Download URL: configini-1.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for configini-1.1.tar.gz
Algorithm Hash digest
SHA256 d8da5091d71480ce5eea3551c11d17f568d3ad0ce788ecd84aad5511bcfdb493
MD5 86d886b80d495fa711d6d87eb3d806bd
BLAKE2b-256 047e693d99f5f54750b54409b2d4fbb8a5921aed79fd69836c95217f0e46bede

See more details on using hashes here.

File details

Details for the file configini-1.1-py3-none-any.whl.

File metadata

  • Download URL: configini-1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for configini-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aa0ef979b371334039b668edbb4a83e00e13d9721d15555baee77981a5cf8bfb
MD5 bee4ec425a1c935e640c4285373f33e2
BLAKE2b-256 0f1f936a2476fefbb3b9f5190e62579dbdcd6155e30783e5b4897437b24d1f3c

See more details on using hashes here.

Supported by

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