libconf

2.0.1 · active · verified Sun Apr 12

libconf is a pure-Python reader/writer for configuration files in libconfig format, commonly used in C/C++ projects. Its interface is designed to be similar to Python's standard `json` module, providing `load()`, `loads()`, `dump()`, and `dumps()` methods. The library is currently at version 2.0.1 and appears to be actively maintained.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a libconfig string into a Python object, access its elements using attribute or dictionary-style access, modify values, and then dump the modified configuration back into a libconfig formatted string. It also shows the use of `LibconfInt64` for explicit control over integer suffixes. For file operations, `libconf.load()` and `libconf.dump()` are available, similar to the `json` module.

import libconf
import io

# Example libconfig string
config_string = """
version = 2;
window : {
  title = "My Application";
  position = { x = 10; y = 20; w = 800; h = 600; };
};
capabilities = (
  true,
  1234567890123456789L,
  [1, 2, 3]
);
"""

# Load configuration from a string
config = libconf.loads(config_string)
print("Loaded Config:", config)
print("Window Title:", config.window.title)

# Modify and dump configuration back to a string
config.window.title = "Updated App Title"
config.capabilities = ('new_list_item', 42)

# Demonstrate LibconfInt64 for explicit long suffix
from libconf import LibconfInt64
config['large_number_forced_L'] = LibconfInt64(123)

dumped_string = libconf.dumps(config, indent=2)
print("\nDumped Config:\n", dumped_string)

view raw JSON →