Skip to main content

flexmod

A python module for other modules to allow flexible (yet not error-prone) configuration.

Story

Suppose you wrote a package and you want to allow users to set package-level configs:

import awesomepackage
from awesomepackage.foo import bar

# user can change module param on the fly
awesomepackage.config["logging"]["verbose"] = True

# package behavior is now different from default
bar()

This is simple, but maybe not any parameter can be changed at any time. For example:

awesomepackage.config["metric"]["length"] = "foot"

Having flexible units may be helpful for different locales, but changing metric units in the middle of a program can lead to consistency issues.

flexmod lets you:

  • specify configs that are auto-locked (i.e. no further changes) when used
  • add custom preprocessor functions to entered config values
    • this is useful when reading config from a text file
  • add validation functions to check user-supplied config values

Usage

Define configurations in your module using flexmod classes

# mypackage/__init__.py
from flexmod import ConfigValue, AutolockedConfigValue, Config, ConfigIndex

config = ConfigIndex(
    [
        Config(
	    "interface",
	    [
	        # example of a config that stays the same throughout a program
	    	AutolockedConfigValue(
		    # name of the config paramater
		    "language",
		    # hint
		    "The language of module interface (logs, warnings, etc.)",
		    # default value
		    "en-us",
		    # validate the config value
		    validation=lambda x: x in ["en-us", "fr-fr"],
		),
	        # example of a config that can change dynamically
	    	ConfigValue(
		    "verbosity",
		    "The granularity to which the module reports / complains",
		    1,
		    # specify a preprocessor function if needed
		    preprocessor=int,
		),
	    ],
	),
        Config(
	    "foo",
	    [
	    	AutolockedConfigValue(
		    "bar",
		    "Any other config parameter",
		    "",
		),
	    ],
	),
    ]
)
# mypackage/message.py
import mypackage

def hello_world():
    """
    Example function that uses a module-level config.
    """
    lang = mypackage.config["interface"]["language"]
    if lang == "en-us":
        print("Hello, world!")
    elif lang == "fr-fr":
        print("Bonjour, tout le monde!")
    else:
        pass

Package user: customize your module on the fly

Your user will not need to be aware of flexmod.

import mypackage
from mypackage.message import hello_world

# change module param on the fly
mypackage.config["interface"]["language"] = "fr-fr"

# this gets "Bonjour, tout le monde!"
hello_world()

# after an autolocked config is read, it cannot be changed
mypackage.config["interface"]["language"] = "en-us"
# AssertionError: language is locked from updates.

Users can also supply an configparser-style ini file.

  • Unlike in default configparser, booleans, integers and floats will be autodetected and converted.
# in custom.ini
[interface]
language = fr-fr
verbosity = 2

[foo]
bar = anything
import mypackage
from mypackage.message import hello_world

# change module param on the fly
mypackage.config.load_override("custom.ini")

# this gets "Bonjour, tout le monde!"
hello_world()

Metadata

Release files for flexmod 0.1.2

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

Source distribution (sdist)

Source distribution for flexmod 0.1.2
File Size Uploaded
flexmod-0.1.2.tar.gz 4.8 kB Details

Built distribution (wheel)

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

Total release size: 9.8 kB

Release files / flexmod-0.1.2.tar.gz

Download URL flexmod-0.1.2.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d5558a13177c76299d6cf068ecb26f0bbc2c91696d5fc68726dce6648dfb4564
BLAKE2b-256 checksum
How to use checksums
32dc117351bd8965911cd5e0d8c0e5242f1b304994682e6d9b7ba24de2a80e3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.5

Release files / flexmod-0.1.2-py3-none-any.whl

Download URL flexmod-0.1.2-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dff10d91c79a98a035c66562431159b18604bbbf455fa0aed969a060722974fb
BLAKE2b-256 checksum
How to use checksums
4097c8de182e30d27d440c99ebcdad7329e0a98bac60dc691de2dd8511d813b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.5

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

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