Skip to main content

ModuleConf

A minimal module-specific configuration tool for fast experimenting in python.

Installation

pip3 install moduleconf

Module and Module specific configuration

For each module, we may have some configuration classes that only apply to that module. However, we often need to experiment with a variety of alternate modules that may contain serveral different module configurations, especially for machine learning, where we can have a huge number of different modules to be combined together, each with a very different set of parameters. This simple library helps with automatically loading modules based on the configuration file and also automatically generating templates for creating a new configuration file.

Basic Usage

In moduleA.py, we defined a Model and its Configuration class/prototype

class Config:
    def __init__(self):
        self.height = 5
        self.width = 3
        
class Model:
    def __init__(self, conf = Config()):
        self.height = conf.height
        self.width = conf.width
    
    def describe(self):
        return ("I am a rectangle h: {} w: {}".format(self.height, self.width))

In moduleB.py. Similarly:

class Config:
    def __init__(self):
        self.radius= 4

class Model:
    def __init__(self, conf=Config()):
        self.radius = conf.radius

    def describe(self):
        return("I am a circle r: {}".format(self.radius))

We then generate the config template for use:

python3 -m moduleconf.generate model:moduleA:Config >> confA.json

python3 -m moduleconf.generate model:moduleB:Config >> confB.json

You can contain a list of key:module:moduleconfigClass tuples and it will automatically combine them into a single configuration file.

It will generate confA.json and confB.json, each containing specific options according to the config prototy defined in the module:

In confA.json:

{
	"model": {
		"module": "moduleA",
		"configClassName": "Config",
		"config": {
			"height": 5,
			"width": 3
		}
	}
}

In confB.json:

{
	"model": {
		"module": "moduleB",
		"configClassName": "Config",
		"config": {
			"radius": 4
		}
	}
}

Then the conf file can be parsed into a ModuleConf object. For the ModuleConf object, we can use conf.module to refer to the module the configuration where has been defined, and we can access the configuration object by conf.config:

import moduleconf
import json
import sys
import os


# add path to import the module from
sys.path.append(os.path.join(os.path.dirname(__file__), '.'))

confPath = "./confA.json"
# or
confPath = "./confB.json"

with open(confPath, 'r') as f:
    conf = moduleconf.parse(json.load(f))

    # load the model
    model = conf["model"].module.Model(conf["model"].config)
    print(model.describe())

Make sure the module defined should be importable in the current search path. Here it adds the module path to the import path.

Support for configuration class containing complex type

It currently supports nested configuration classes and deserialization will be performed according to the prototype configuration object provided. For complex classes requiring special handling for serialization/deserialization, just implement toDict/loadDict method inside the class, and they can be used as configuration classes as well.

Support for nested configuration file

STUB

Metadata

Release files for moduleconf 0.1.4

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

Built distribution (wheel)

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

Release files / moduleconf-0.1.4-py3-none-any.whl

Download URL moduleconf-0.1.4-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
47fd48f5cd566e3ab198106a66dfbe63e4b9eae924f8db58d66df7aba25e5c39
BLAKE2b-256 checksum
How to use checksums
91791303dd4b1037b3d59b119af2123af11c1cedb8b1f5aef8df774d073e7320
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.1.4 This release

1 release file

0.1.3

1 release file

0.1.2

2 release files

0.1.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