Skip to content

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. Một cái cung người dùng lấy Python Dict hoặc JSON cách thức biên soạn ( nghiên cứu khoa học trung thực nghiệm ) phối trí kho, ở số hiệu có ích điểm. Đọc viết thuộc tính, đồng thời có thể tòng mệnh lệnh hành trung đọc lấy tham số phối trí cũng sửa chữa tham số giá trị.

License

Notifications You must be signed in to change notification settings

NaiboWang/CommandlineConfig

Repository files navigation

Tiếng Trung hồ sơ

Điểm này xem xét tiếng Trung hồ sơ

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 viadot.in code, while can read parameters from the command line to modify values.

Nhãn Labels: Python, Command Line, commandline, config, configuration, parameters, mệnh lệnh hành, phối trí, truyền tham, tham số giá trị sửa chữa.

Github URL:https://github /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
pip3installcommandline_config

# import package
fromcommandline_configimportConfig

# 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 --
Pythonexample.py--index3--dbinfo.usernameXDU

# 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()

Pythonexample.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_configfolder of the github project into your own project directory, you need to install the dependency packageprettytable:
    pip3 install prettytable

    Or install viarequirements.txt:

    pip3 install -r requirements.txt

Configuration Way

    1. Import library:
    fromcommandline_configimportConfig
    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, andcan 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 inpreset_configdict 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 parameterindexis automatically detected asint,the default value is1and 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 passingpreset_configdict toConfigin 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 viaprintfunction:
    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 byconfig_with_name.set_print_style(style='').The values that can be taken forstyleare:both,table,jsonwhich 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)
    print("----------")
    # Print table and json at the same time
    config_with_name.set_print_style('table')
    print(config_with_name)

    The output results are:

    Configurations of Federated Learning Experiments:
    {'index': 1, 'dataset': 'mnist', 'lr': 0.01, 'normalization': True, 'pair': (1, 2), 'multi_information': [1, 0.5, 'test', 'TEST'], 'dbinfo': 'See below'}
    
    Configurations of dict dbinfo:
    {'username': 'NUS', 'password': 123456, 'retry_interval_time': 5.5, 'save_password': False, 'pair': ('test', 3), 'multi': 'See below', 'certificate_info': ['1', 2, [3.5]]}
    
    Configurations of dict multi:
    {'test': 0.01}
    
    ----------
    
    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 |
    +-------------------+-------+--------------------------+
    {'index': 1, 'dataset': 'mnist', 'lr': 0.01, 'normalization': True, 'pair': (1, 2), 'multi_information': [1, 0.5, 'test', 'TEST'], 'dbinfo': 'See 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]] |
    +---------------------+-------+---------------------+
    {'username': 'NUS', 'password': 123456, 'retry_interval_time': 5.5, 'save_password': False, 'pair': ('test', 3), 'multi': 'See below', 'certificate_info': ['1', 2, [3.5]]}
    
    Configurations of dict multi:
    +------+-------+-------+
    | Key | Type | Value |
    +------+-------+-------+
    | test | float | 0.01 |
    +------+-------+-------+
    {'test': 0.01}
    

Configuration parameters read and write method

Write method

Configuration parameter values can be written in three ways.

    1. To receive command line arguments, simply pass--index 1on the command line to modify the value ofindexto1.Also, the considerations for passing values to different types of arguments are:
    • When passing bool type, you can use0orFalseforFalse,1orTrueorno value after the parameterforTrue:--normalization 1or--normalization Trueor--normalizationall can set the value of parameternormalizationin the configuration toTrue.
    • When passing list type, empty array and multi-dimensional arrays can be passed.
    • To modify the value in the nested dict, please use--nested-parameter-name.sub-parameter-name.sub-parameter-name.….sub-parameter-name valueto modify the value in the nested object, such as--dbinfo.password 987654to change the value of thepasswordparameter in thedbinfosubobject to987654;--dbinfo.multi.test 1to change the value of thetestparameter in themultidict which is indbinfosubobject to ```. Currently this tool can supports unlimited layers/levels of nesting.
    • Note thatthe argument index must be in thepreset_configobject defined above:
    Pythontest.py--dbinfo.password987654--dbinfo.multi.test1--index0--datasetemnist--normalization0--multi_information[\'sdf\',1,\ "3.3\ ",,True,[1,[]]]
    1. Useconfig.index = 2directly in the code to change the value of the parameterindexto2.Again, list type parameters can be assigned as empty or multidimensional arrays. For nested objects, you can useconfig.dbinfo.save_password=Trueto modify the value of thesave_passwordparameter in sub dictdbinfotoTrue.
    1. Way 1 and 2 will trigger type checking, that is, if the type of the assigned value and the type of the default value in the predefined dictpreset_configdoes not match, the program will report an error, therefore, if you do not want to force type checking, you can useconfig[ "index" ] = "sdf"to force the value of the parameter index to the stringsdf(not recommended, it will cause unexpected impact).

Reading method

Read the value of the parameterdatasetdirectly by means ofconfig.datasetorconfig[ "dataset" ].

print(config.dataset,config["index"])

The value of an argumentawill be read by this order: the last value modified byconfig.a = *> the value of--a 2specified by the command line > the initial value specified by"a":1defined by preset_config.

For the list type, if a multidimensional array is passed, the information can be read via standard slice of Python:

config.dbinfo.certificate_info=[1,[],[[2]]]
print(config.dbinfo.certificate_info[2][0][0])

For parameters in a single nested object, there are four ways to read the values of the parameters, all of which can be read successfully:

print(config.dbinfo.username)
print(config[ "dbinfo" ].password)
print(config.dbinfo[ "retry_interval_time" ])
print(config[ "dbinfo" ][ "save_password" ])

Pass configuration to functions

Simply pass the above config object as a parameter to the function and call it:

defprint_dataset_name(c):
print(c.dataset,c["dataset"],c.dbinfo.certificate_info)

print_dataset_name(c=config)

Copy configuration

A deep copy of the configuration object can be made by thedeepcopymethod:

fromcopyimportdeepcopy
copy_config=deepcopy(config)
# Modify new configuration's parameter value, will not affect the orignal configuration
copy_config.index=15

Store configuration parameters to local file or database

The entire parameter configuration can be stored to a local file or uploaded to a remote server such as mongodb, simply byconfig.save()storing the configuration as aconfig name (or config if there is no name).jsonfile in the directory, or you can specify the file name and path as follows:

config.save("config/test_config.json")

Then we successfully save the configuration to the localconfiguration.jsonfile inside theconfigfolder. The file content is as follows:

{
"index":1,
"dataset":"mnist",
"lr":0.01,
"normalization":true,
"pair":[1,2],
"multi_information":[1,0.5,"test","TEST"],
"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]]
}
}

To store it into the database such asmongodb,you need to get the json sequence first corresponding to the parameters with theinfo = config.get_config()command, and serialize it with thejsonlibrary.

For example, to store theconfig_with_nameconfiguration tomongodb:

importpymongo
myclient=pymongo.MongoClient('mongodb://username:example:27017/',connect=False)
mydb=myclient['exps']
table=mydb["table"]
# Get the configurations
configuration=config.get_config()
# Insert configuration dict into mongodb table
table.insert_one(configuration)

# Or make configuration as part of a bigger dict
all_info={
"exp_time":"20220925",
"configuration":configuration
}
table.insert_one(all_info)

Note that tuples are not supported by JSON, so whether stored locally or in a database, tuple arguments will be converted to lists.

Advanced options

Restrict parameter input values to fixed enum types

Set advanced options, such as enumerating Enum types, by passing theoptionsparameter of theConfigargument to theConfigclass.

option={}
config=Config(preset_config,options=option)

If you want to limit the value of a parameter to a certain range, you can do so by configuring:

advanced_options={
'lr':{
"enum":[0.001,15.5,0.01,0.1]# restrict the lr value to one of 0.001, 15.5, 0.01, 0.1
},
'index':{
"enum":[1,2,3]# Restrict the index value to 1, 2 and 3
},
"dbinfo":{
"username":{
"enum":["XDU","ZJU","NUS"]# restrict the dbinfo.username field to XDU, ZJU and NUS
},
"multi":{
"test":{
"enum":[1,0.1,0.01,15]# 3 layers nested
}
}
},
}

config=Config(preset_config,options=advanced_options)

If enum is set, the following three ways to set a parameter to a value other than the qualified/speficied value will all report an error.

    1. The initial value ofindexis set to a value other than1,2,3inpreset_config:
    preset_config={
    "index":4,
    }
    1. The command line passes unqualified/unspecified values for thelrargument
    Python example.py --lr 0.02
    1. The code changes the value ofdbinfo.usernameto a value other thanXDU, ZJU and NUS.
    config.dbinfo.username="UEST"

    The output are:

    AttributeError: Can notsetvalue 4 because the key'index'hassetenum list and you the value 4 is notinthe enum list [1, 2, 3]!
    
    AttributeError: Can notsetvalue 0.02 because the key'lr'hassetenum list and you the value 0.02 is notinthe enum list [0.001, 15.5, 0.01, 0.1]!
    
    AttributeError: Can notsetvalue nus because the key'username'hassetenum list and you the value nus is notinthe enum list ['XDU','ZJU','NUS']!

Print parameter help descriptions

Set parameter descriptions

Set the parameter description helpers by specifying thehelpersparameter in theConfigclass.

helpers={
"index":"index of information",
"dbinfo_help":"information dict for database",
"dbinfo":{
"username":"username for database",
"multi":{
"test":"test information"
}
}
}

config=Config(preset_config,helpers=helpers)

Note that since thedbinfoparameter is adict,if you want to set the parameter description fordbinfo,you need to set adbinfo_helpparameter to write the description in thehelpersdictionary, i.e. add_helpafter the dict parameter name to set the parameter description for the dict field.

Print parameter help

Two ways to print parameter descriptions, by passing-hor-helpon the command line, or by calling thehelp()function in code.

config_with_name.help()

or

Python example.py -h
#OR
Python example.py -help

Note that it is only one short slash-and no other command line arguments be added to get help instructions, and the output of both methods is:

Parameter helps for Federated Learning Experiments:
+-------------------+-------+-------------------------------+
| Key | Type | Comments |
+-------------------+-------+-------------------------------+
| index | int | index of information |
| dataset | str | - |
| lr | float | - |
| normalization | bool | - |
| pair | tuple | - |
| multi_information | list | - |
| dbinfo | dict | information dict for database |
+-------------------+-------+-------------------------------+

Parameter helps for dict dbinfo:
+---------------------+-------+-----------------------+
| Key | Type | Comments |
+---------------------+-------+-----------------------+
| username | str | username for database |
| password | int | - |
| retry_interval_time | float | - |
| save_password | bool | - |
| pair | tuple | - |
| multi | dict | Multiple Parameters |
| certificate_info | list | - |
+---------------------+-------+-----------------------+

Parameter helps for dict multi:
+------+-------+------------------+
| Key | Type | Comments |
+------+-------+------------------+
| test | float | test information |
+------+-------+------------------+

Things need attention

Conflict with Argparse

This library cannot read command line arguments at the same time with the argparse library, so please do not useargs = parser.parse_args()to read command line arguments while using this library.

Input value forced conversion

The type of the parameter will be automatically detected as the same type of the initial value set inpreset_config,and the value of the command line parameter will be forced converted to the corresponding type value, such as the default value ofindexin the abovepreset_configdict is1,then the type of the parameter index isintwith the initial value of1.If you specify--index 15.5on the command line, the parameterindexwill be automatically assigned to value15,that is,15.5will be automatically forced converted tointtype.

If the parameter value specified on the command line parameters can not be forcedly converted to specific type, it will report an error, such as if the command line specified--index sdf,as sdf with orignal format ofstringcan not be converted tointtype, so it will report an error.

The list parameter needs to be assigned with a backslash before the string element quotes when passing by commandline

When the command line argument is set to the inputlisttype, if the element in the list is astring,you must use add abackslash \before eachsingle/double quoteto parse it correctly, otherwise the argument value will be treated as anintorfloattype. If there arespacesin the command line they will be merged automatically (but the command line environment can not bezsh,if it is zsh environment then must remove all the spaces inside the list,bashandshdoes not have this problem, that is, in the zsh environment, you cannot add any space(s) between15and\'12\'in--a [15,\'12\']).

If the parameters can be set as follows:

Python test.py --array [1,2.3,\'sdf\']

That can correctly resolve the array parameter whose value is alist,and the content of[1,2.3,'sdf', "qwe" ],that is, a list containing int, float, string type of data simultaneously.

Quotes are required for command-line assignment of tuple parameters, and string elements must be preceded by a backslash

When the command line parameter is set to the inputtupletype, the specified tuple type value must be enclosed inquotes;and if the element in the tuple is astring,abackslashmust be added before each single/double quote\for proper parsing, otherwise the parameter value will be treated as anintorfloattype. Similarly, if there arespacesin the command line they will be merged automatically (but the command line environment cannot bezsh,if it is zsh environment then all internal spaces must be removed, bash and sh do not have this problem).

For example, the parameter can be set to

Python test.py --pair "(1,2,\'msg\')"

The value of the pair parameter is a tuple of type(1,2, "msg" ),i.e. a tuple of typeint,float,string.

Parameter naming convention

If the parameter name contains special characters such as-+.orspaceorother Python reserved characters,you must use themiddle bracket []to read and write the parameter value instead of.E.g., if the parameter name ismulti-information,it can only be accessed byconfig[ "multi-information" ],cannot doconfig.multi-information,because the minussign -is a Python language's reserved symbol.

Unlimited layer of nested objects

Now the tool can support unlimited layers of nesting, other supported parameter types are:int, float, string, bool, tuple and list.

Parameter integrity check, all parameters to be modified must be predefined

The name of the parameter passed on the command linemust be defined inpreset_configin advance, otherwise an error will be reported,e.g.

Pythontest.py--arg11

Since the parameter namearg1is not defined inpreset_configdict, an error is reported indicating that thearg1parameter is not defined. This function is set to perform parameter integrity checking to avoid entering incorrect parameter names through the command line.

Special configurations in zsh environment

Ifzsh: no matches foundoccurs when passing list arguments in the zsh Shell environment, please add a linesetopt no_nomatchat the end of the~/.zshrcfile, after save it then runsource ~/.zshrcon the command line to refresh zsh, then the problem will be solved.

Full conversion example

An example will be given below to demonstrate the convenience of this tool compared to theargparsetool.

The code that needs to be written using theargparsetool:

parser=argparse.ArgumentParser(description='PyTorch local error training')
parser.add_argument('--model',default='vgg8b',
help='model, mlp, vgg13, vgg16, vgg19, vgg8b, vgg11b, resnet18, resnet34, wresnet28-10 and more (default: vgg8b)')
parser.add_argument('--dataset',default='CIFAR10',
help='dataset, MNIST, KuzushijiMNIST, FashionMNIST, CIFAR10, CIFAR100, SVHN, STL10 or ImageNet (default: CIFAR10)')
parser.add_argument('--batch-size',type=int,default=128,
help='input batch size for training (default: 128)')
parser.add_argument('--num-layers',type=int,default=1,
help='number of hidden fully-connected layers for mlp and vgg models (default: 1')
parser.add_argument('--lr',type=float,default=5e-4,
help='initial learning rate (default: 5e-4)')
parser.add_argument('--lr-decay-milestones',nargs='+',type=int,default=[200,300,350,375],
help='decay learning rate at these milestone epochs (default: [200,300,350,375])')
parser.add_argument('--optim',default='adam',
help='optimizer, adam, amsgrad or sgd (default: adam)')
parser.add_argument('--beta',type=float,default=0.99,
help='fraction of similarity matching loss in predsim loss (default: 0.99)')
args=parser.parse_args()

args.cuda=notargs.no_cudaandtorch.cuda.is_available()
ifargs.cuda:
cudnn.enabled=True
cudnn.benchmark=True

Code to be written after conversion with this tool:

'''
:param model: model, mlp, vgg13, vgg16, vgg19, vgg8b, vgg11b, resnet18, resnet34, wresnet28-10 and more (default: vgg8b)
:param dataset: dataset, MNIST, KuzushijiMNIST, FashionMNIST, CIFAR10, CIFAR100, SVHN, STL10 or ImageNet (default: CIFAR10)
:param batch-size: input batch size for training (default: 128)
:param num-layers: number of hidden fully-connected layers for mlp and vgg models (default: 1)
:param lr: initial learning rate (default: 5e-4)
:param lr-decay-milestones: decay learning rate at these milestone epochs (default: [200,300,350,375])
:param optim: optimizer, adam, amsgrad or sgd (default: adam)
:param beta: fraction of similarity matching loss in predsim loss (default: 0.99)
'''
config={
'model':'vgg8b',
'dataset':'CIFAR10',
'batch-size':128,
'num-layers':1,
'lr':5e-4,
'lr-decay-milestones':[200,300,350,375],
'optim':'adam',
'beta':0.99,
}
args=Config(config,name='PyTorch local error training')

args.cuda=notargs.no_cudaandtorch.cuda.is_available()
ifargs.cuda:
cudnn.enabled=True
cudnn.benchmark=True

As we can see, the amount of code has been reduced and is more structured and neat.

Another case with library click:

importclick

@click.command()
@click.option('--count',default=1,help='Number of greetings.')
@click.option('--name',prompt='Your name',
help='The person to greet.')
defhello(count,name):
"""Simple program that greets NAME for a total of COUNT times." ""
forxinrange(count):
click.echo(f "Hello{name}!")

if__name__=='__main__':
hello()

Can be converted to the following code:

fromcommandline_configimportConfig

defhello(o):
"""Simple program that greets NAME for a total of COUNT times." ""
forxinrange(o.count):
print(f "Hello{o.name}!")

if__name__=='__main__':
args={
"count":1,# Number of greetings.
"name":"",# The person to greet.
}
options=Config(args)
hello(options)

Example Running Script

You can runexample.pyin the Github project to test the whole tool, most functions's codes had been provided inside the file:

# Get help for all parameters of example.py
Python example.py -h
# Specify parameter values
Python example.py --dbinfo.multi.test 0.01 --dbinfo.username NUS

Shattered thoughts

The following describes the author's personal reasons for developing and the benefits/conveniences of this package.

For us who often run research experiments, do you often need to set a lot of command line arguments at the beginning of a Python file and call them in the following code asargs.*:

For example, the following example paragraph:

parser=argparse.ArgumentParser(description='index')
parser.add_argument('--index',default=0,type=int,help='party index')
parser.add_argument('--party_num',default=100,type=int)
args=parser.parse_args()

print(args.index)

With one more additional parameter, we need to write one line ofparser.add_argument(...),when handwriting configuration of each parameter, it will be very tedious such as the name needs to add --, and modify the default value, type and description of the time is very troublesome, finally will lead to very long code and inconvenient to maintain.

Even if you use a more advanced libraryclick,you still need to write option constantly, and you need to write the same amount of parameters at the input parameter field of the function to match all the option, writing code is really tedious, such as the following Click code:

importclick

fromcaesar_encryptionimportencrypt

@click.command()
@click.argument('text',nargs=-1)
@click.option('--decrypt/--encrypt','-d/-e')
@click.option('--key','-k',default=1)
defcaesar(text,decrypt,key):
text_string=' '.join(text)
ifdecrypt:
key=-key
cyphertext=encrypt(text_string,key)
click.echo(cyphertext)

if__name__=='__main__':
caesar()

I don't want to specify option over and over again when writing code, and write a lot of corresponding parameters in the function name parameter field, which is very troublesome.

So would it make the code look more structured and clearer if the parameter configuration could be written in the format of Python objects/JSON?

Also, is it possible to directly read and write command line arguments as if they were Python objects, or deep copy them, as if no command line arguments had been configured?

Most importantly, can we make the process of writing code faster, instead of adding a little bit of configuration line by line?

In order to solve the above pain points, this tool has been developed.

My goal is to make it easier for us to write more concise codes that works as the same as verbose command line parsing packages like Argparse or Click, while also making the configuration look structured and easy to maintain. Json was chosen because the Python dictionary type Dict is native in JSON format, so can make the same format as Python code. In this way, the configuration information can be directly embedded in the Python code file (anywhere), without the need to establish a separate configuration file such as TOML. At the same time, you can use the library to write the configuration at any position within the code file to read the command line parameters and modify the value of each field at any time.

Similar tools including:

Name Advantage Disadvantage
Fire Can direct do conversion of function parameters to command line parameters Cannot pass parameters to other functions.
hydra Easy to read and write to yaml Need additional yaml file with specifed path, also cannot perform integrity check and type conversion/check, not friendly for printing
ml_collections Similar functionality to this tool with more configurable items A little tideous to pass commandline parameters, when nesting need to artificially set specifed Class, and also without integrity check, not friendly for printing

TODO

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

  • Advanced options to support more advanced features such as commandline style (instead of use --, can change to - or +), etc.

About

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. Một cái cung người dùng lấy Python Dict hoặc JSON cách thức biên soạn ( nghiên cứu khoa học trung thực nghiệm ) phối trí kho, ở số hiệu có ích điểm. Đọc viết thuộc tính, đồng thời có thể tòng mệnh lệnh hành trung đọc lấy tham số phối trí cũng sửa chữa tham số giá trị.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published