Skip to main content

Package allow configuration and section's option values of .ini file to be called as attributes, where the section and option are separated by dot notation.

Project description

dotmapini

Package allow configuration and section's option values of .ini file to be called as attributes, where the section and option are separated by dot notation.

The focus is to work with .ini configuration data using dot notation. Just call attributes and get their values in development flow while defining them in single place.

  • No more configuration values in config['section']['option'] style, just config.section.option;
  • Can parse .ini files with dots between section names and convert them as several sections for corresponding values;
  • Parse values and convert them to Python datatypes;
    # Return parsed value datatypes:
    Union[Config, str, bool, int, float, bytes, tuple, list, dict, set, None]
    # Other types as well as Python types that aren't presented correctly in .ini
    # not implemented and will be parsed as string.
    
  • Your config is instance of collections.MutableMapping (dict's like) and have the same features;
  • Support all keyword arguments for configparser.ConfigParser in Config.load(path, **kwargs) method;
  • Less keystroke;
  • No dependencies, only stdlib.

Installation:

pip install dotmapini

Usage:

Imagine you have following .ini configuration file:

# example.ini
[APP]
debug = False

[server]
host = 127.0.0.1
port = 8080

[server.db]
host = localhost
database = test
user = username
password = password

[complex.Python.datatypes]  ; Your complex Python datatype as is
example = [{'key': ({'value', 'value2'}, [True, 1.2, 3])}]

Minimal reproducible example:

from dotmapini import Config


config = Config.load(
    path='/your/path/to/example.ini',
    allow_no_value=True  # keyword for configparser.ConfigParser(...)
    )
print(config.APP.debug)  # => False type bool
print(config.server.host)  # => '127.0.0.1' type str
print(config.server.port)  # => 8080 type int
print(config.server.db.database)  # => 'test' type str
print(config.server.db.username)  # => 'username' type str
print(config.complex.Python.datatypes.example)  # => [{'key': ({'value', 'value2'}, [True, 1.2, 3])}] type list

Of course as always you can do this:

print(config['server']['db']['host'])  # => 'localhost' type str

But for what...

IMPORTANT:

  • Uppercase strings for options will be parsed as lowercase.

    Example:

    [section]
    OPTION = ...
    

    Will be:

    config = Config.load(...)
    config.section.option  # not self.section.OPTION
    
  • Only digits in section's name not allowed.

    Example:

    [section.1.subsection]  ; A digit between dots not allowed as well
    ...
    [2]
    ...
    [3.section]
    ...
    

    Will be:

    config = Config.load(...)  # will raise DigitInSectionNameError
    
  • If you have DEFAULT section it will be added to all other sections thus it can override same named option values.

    Example:

    [DEFAULT]
    option = value
    
    [section]
    option = value2
    

    Will be:

    config = Config.load(...)
    config.section.option = value  # not value2
    
  • Be aware not to memory overflow while represent complex Python datatypes in .ini file.

Most of this stuff is the default behavior of configparser.ConfigParser.

Need to mention:

Q: Why not dotmap?
A: I want to focus to work specifically with .ini/configparser. There is no need to me to create a lot of dict's like objects.
Q: Why not types.SimpleNamesapce?
A: It can instantiate attributes and nothing else, that is not the case here. Class collections.MutableMapping provide more control/isolation when create complex custom dict's like objects, which is focus to work with .ini configuration files and modify receiving values.

LICENSE

MIT

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

dotmapini-1.0.3.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

dotmapini-1.0.3-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file dotmapini-1.0.3.tar.gz.

File metadata

  • Download URL: dotmapini-1.0.3.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for dotmapini-1.0.3.tar.gz
Algorithm Hash digest
SHA256 f8b050fbab5103deedb0d7d02c592a2a89a5e3d461247728fe89daf56168c902
MD5 1d9d09e8db41d52364163cea64e3626e
BLAKE2b-256 325aed77fc6ebba1ffc2388c1b22b9c98fcbbc5ac5481b0efb955acf0baecdef

See more details on using hashes here.

File details

Details for the file dotmapini-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: dotmapini-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for dotmapini-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 12a75a4726b6444859aa6687a416f3987d603ffae0ae000aba63af23e29c3a41
MD5 f8ea960f669b6243b6b554019ac0b784
BLAKE2b-256 f2d99f6e98e0416fb99de302c612b1e1fda95799340e1b19db74b7c0138552c2

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