Skip to main content

localconfig

A simplified interface to ConfigParser using dot notion with data type / comment support.

Feature Summary

  • Simple access to config using dot notion and iterators

  • Full compatibility with ConfigParser ini formats (as that is used as the backend)

  • Data type support by intelligently guessing the data types based on value on read.

  • Multiple config source input (read from string, file pointer, file, or list of them)

  • Full comment support / retention on save

  • Lazy reading of config sources for performance (only read when a config value is accessed)

Quick Start Tutorial

To install:

pip install localconfig

Let’s say we have a script named program with the following config in ~/.config/program:

[Web Server]
# Server host
host = 0.0.0.0

# Server port
port = 8080

# Debug logging
debug = off

To read the config, simply do:

from localconfig import config

start_server(config.web_server.host, config.web_server.port, config.web_server.debug)

# Or use get method:
# start_server(config.get('Web Server', 'host'),
#              config.get('Web Server', 'port'),
#              config.get('web_server', 'debug'))  # Yes, 'web_server' also works here!
#
# Or if the config is in docstring, read from it:
# config.read(__doc__)
#
# Or if the config file is elsewhere:
# config.read('/etc/path/to/config.ini')  # Non-existing file is ignored
#
# Or read from a list of sources
# config.read(['string config', file_path, file_pointer, io.StringIO('config')])
#
# Or create another instance for another config:
# from localconfig import LocalConfig
# config2 = LocalConfig('/etc/path/to/another/config.ini')

Configs are read in the order they are called using config.read(), but the config file passed to the LocalConfig() constructor (defaults to ~/.config/$script_name) will be read last before the first access to config values, which allows us to read configs from various locations, like default configs from a string that is checked in with the code, while allowing them to be overrriden from the config file that is passed to the constructor.

Now, let’s do some inspection:

# Iterate over sections and their keys/values
for section in config:
  print(section)                   # Web Server

  for key, value in config.items(section):
    print(key, value, type(value)) # host 0.0.0.0 <type 'str'>
                                   # port 8080 <type 'int'>
                                   # debug False <type 'bool'>

sections = list(config)            # ['Web Server']

# Iterate over keys/values
for key, value in config.web_server:
  print(key, value, type(value))    # Same output as above config.items()

items = list(config.web_server)    # [('host', '0.0.0.0'), ('port', 8080), ('debug', False)]
items = dict(config.web_server)    # {'host': '0.0.0.0', 'port': 8080, 'debug': False}

# Check if a section or key is set - any non-existing section or key defaults to None.
if config.web_server or config.no_such_section:
  pass

if config.web_server and (config.web_server.port or config.web_server.no_such_key):
  pass

To add a section and set a value:

config.add_section('App Server', comment='Settings for application server')
config.app_server.host = 'localhost'

# Use `set` if you want to set a comment
config.set('App Server', 'port', 9090, comment='App server port')

# Set value for the DEFAULT section (default value for all other sections)
config.env = 'prod'

To write the config:

config.save()

# Or simply get the config as a string:
# config_str = str(config)
#
# Or save to a different location:
# config.save('/path/to/save/to.ini')

If we open ~/.config/program now, we would see:

[DEFAULT]

env = prod


[Web Server]

# Server host
host = 0.0.0.0

# Server port
port = 8080

# Debug logging
debug = off


# Settings for application server
[App Server]

host = localhost

# App server port
port = 9090

Supported Data Types

Data type is guessed based on the value and converted on read.

The following types are supported:

Type

Example Value

int

1 0b1101 0o70 0xFF

float

2.0

bool

true false yes no on off (case insensitive)

None

none (case insensitive)

str

Any other value not matched by above

Remote Config

Check out: https://pypi.python.org/pypi/remoteconfig

More

Metadata

Release files for localconfig 1.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for localconfig 1.1.4
File Size Uploaded
localconfig-1.1.4.tar.gz 19.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for localconfig 1.1.4
File Interpreter ABI Platform
localconfig-1.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 28.3 kB

Release files / localconfig-1.1.4.tar.gz

Download URL localconfig-1.1.4.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b71464eca5ef77c722f56ad548df5efd079e3d96aa777f511d1a18114d7b7221
BLAKE2b-256 checksum
How to use checksums
501a01037aab418fd7834b0604599eeed3ff5e0891b3692ae974164275e8d53b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.8

Release files / localconfig-1.1.4-py3-none-any.whl

Download URL localconfig-1.1.4-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51ba3469c0b488f7fb3100a4092382d6cf7c4191ca7fc75abba7cb40239e133d
BLAKE2b-256 checksum
How to use checksums
7d15ed0b22bf270fc96ff2b4ab67ea3ea532f680182f6177094912a7f73550fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.8

Release history Release notifications | RSS feed

This release

1.1.4 This release

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

1 release file

1.0.2

1 release file

1.0.1

1 release file

0.4.2

1 release file

0.4.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page