SkillAgentSearch skills...

CommandlineConfig

A library for users to write (experiment in research) configurations in Python Dict or JSON format, read and write parameter value via dot . in code, while can read parameters from the command line to modify values. 一个供用户以Python Dict或JSON格式编写(科研中实验)配置的库,在代码中用点.读写属性,同时可以从命令行中读取参数配置并修改参数值。

Install / Use

/learn @NaiboWang/CommandlineConfig

README

中文文档

点此查看中文文档

Easy-to-use Commandline Configuration Tool

A library for users to write (experiment in research) configurations in Python Dict or JSON format, read and write parameter value via dot . in code, while can read parameters from the command line to modify values.

标签 Labels: Python, Command Line, commandline, config, configuration, parameters, 命令行,配置,传参,参数值修改。

Github URL: https://github.com/NaiboWang/CommandlineConfig

Reserved Fields

The following fields are reserved and cannot be used as parameter names: config_name.

New Features

v2.2.*

  • Support infinite level nesting of parameters in dictionary
  • Automatic version checking
  • Support parameter value constrained to specified value (enumeration)
  • Support for tuple type
  • Support reading configuration from local JSON file
  • Support for setting parameter help and printing parameter descriptions via command line -h
  • Documentation updates, provide simple example

Simple Example

# Install via pip
pip3 install commandline_config

# import package
from commandline_config import Config

# Define configuration dictionary
config = {
  "index":1,
  "lr": 0.1,
  "dbinfo":{
    "username": "NUS"
  }
}

# Generate configuration class based on configuration dict
c = Config(config)

# Print the configuration of the parameters
print(c)

# Read and write parameters directly via dot . and support multiple layers.
c.index = 2
c.dbinfo.username = "ZJU"
print(c.index, c.dbinfo.username, c["lr"])

# On the command line, modify the parameter values with --
python example.py --index 3 --dbinfo.username XDU

# Get the parameter descriptions via the help method in the code, or on the command line via -h or -help (customization required, see detailed documentation below for details)
c.help()

python example.py -h

Catalogue

Usage

Please submit issue

If you encounter any problems during using with this tool, please raise an issue in the github page of this project, I will solve the bugs and problems encountered at the first time.

Meanwhile, welcome to submit issues to propose what functions you want to add to this tool and I will implement them when possible.

Installation

There are two ways to install this library:

    1. Install via pip:
    pip3 install commandline_config
    

    If already installed, you can upgrade it by the following command:

    pip3 install commandline_config --upgrade
    
    1. Import the commandline_config.py file directly from the /commandline_config folder of the github project into your own project directory, you need to install the dependency package prettytable:
    pip3 install prettytable
    

    Or install via requirements.txt:

    pip3 install -r requirements.txt
    

Configuration Way

    1. Import library:
    from commandline_config import Config
    
    1. Set the parameter name and initial value in JSON/Python Dict format, and add the parameter description by # comment. Currently supports nesting a dict inside another dict, and can nest unlimited layers.
    preset_config = {
          "index": 1,  # Index of party
          "dataset": "mnist",
          'lr': 0.01,  # learning rate
          'normalization': True,
          "pair": (1,2),
          "multi_information": [1, 0.5, 'test', "TEST"],  # list
          "dbinfo": {
              "username": "NUS",
              "password": 123456,
              "retry_interval_time": 5.5,
              "save_password": False,
              "pair": ("test",3),
              "multi":{
                  "test":0.01,
              },
              "certificate_info": ["1", 2, [3.5]],
          }
      }
    

    That is, the initial configuration of the program is generated. Each key defined in preset_config dict is the parameter name and each value is the initial value of the parameter, and at the same time, the initial value type of the parameter is automatically detected according to the type of the set value.

    The above configuration contains seven parameters: index, dataset, batch, normalization, pair, multi_information and dbinfo, where the type of the parameter index is automatically detected as int, the default value is 1 and the description is "Index of party".

    Similarly, The type and default value of the second to fifth parameter are string: "mnist"; float:0.01; bool:True; tuple:(1,2); list:[1,0.5,'test', "TEST"].

    The seventh parameter is a nested dictionary of type dict, which also contains 7 parameters, with the same type and default values as the first 7 parameters, and will not be repeated here.

    1. Create a configuration class object by passing preset_config dict to Config in any function you want.
    if __name__ == '__main__':
        config = Config(preset_config)
        # Or give the configuration a name:
        config_with_name = Config(preset_config, name="Federated Learning Experiments")
    
        # Or you can store the preset_config in local file configuration.json and pass the filename to the Config class.
        config_from_file = Config("configuration.json")
    

    This means that the configuration object is successfully generated.

    1. Configuration of parameters can be printed directly via print function:
    print(config_with_name)
    

    The output results are:

    Configurations of Federated Learning Experiments:
    +-------------------+-------+--------------------------+
    |        Key        |  Type | Value                    |
    +-------------------+-------+--------------------------+
    |       index       |  int  | 1                        |
    |      dataset      |  str  | mnist                    |
    |         lr        | float | 0.01                     |
    |   normalization   |  bool | True                     |
    |        pair       | tuple | (1, 2)                   |
    | multi_information |  list | [1, 0.5, 'test', 'TEST'] |
    |       dbinfo      |  dict | See sub table below      |
    +-------------------+-------+--------------------------+
    
    Configurations of dict dbinfo:
    +---------------------+-------+---------------------+
    |         Key         |  Type | Value               |
    +---------------------+-------+---------------------+
    |       username      |  str  | NUS                 |
    |       password      |  int  | 123456              |
    | retry_interval_time | float | 5.5                 |
    |    save_password    |  bool | False               |
    |         pair        | tuple | ('test', 3)         |
    |        multi        |  dict | See sub table below |
    |   certificate_info  |  list | ['1', 2, [3.5]]     |
    +---------------------+-------+---------------------+
    
    Configurations of dict multi:
    +------+-------+-------+
    | Key  |  Type | Value |
    +------+-------+-------+
    | test | float | 0.01  |
    +------+-------+-------+
    

    Here the information of all parameters will be printed in table format. If you want to change the printing style, you can modify it by config_with_name.set_print_style(style=''). The values that can be taken for style are: both, table, json which means print both table and json at the same time, print only table, and json dictionary only.

    E.g.:

    # Only print json 
    config_with_name.set_print_style('json')
    print(config_with_name)
    
View on GitHub
GitHub Stars1.9k
CategoryEducation
Updated23d ago
Forks239

Languages

Python

Security Score

100/100

Audited on Feb 26, 2026

No findings