Skip to content

Getting started

Installation

The loam package is available on PyPI, you can install it with pip:

shell
python3 -m pip install -U loam

Basic usage

The following code is a very simple example showing how to use loam to create a configuration object with no config file nor argument parsing management.

from dataclasses import dataclass
from typing import Optional

from loam.base import entry, Section, ConfigBase

# Dataclasses define the options and their default values.
@dataclass
class SectionA(Section):
    option_a: str = "foo"
    option_b: int = 42
    option_c: str = "bar"

# You can attach metadata to each option, such as an explanation
@dataclass
class SectionB(Section):
    option_d: int = entry(val=0, doc="some number")
    # you can have the same option name living in two different sections
    option_a: float = entry(val=3.14159, doc="some float")

# A ConfigBase dataclass groups the sections
@dataclass
class Config(ConfigBase):
    section_a: SectionA
    section_b: SectionB

conf = Config.default_()

# You can access options value and modify them
assert conf.section_a.option_a == "foo"
conf.section_b.option_d = 3