Nested Automated Argument Parsing Configuration (NAAPC).
Project description
Nested Automated Argument Parsing Configuration (NAAPC)
NAAPC contains two classes: NConfig and NDict. NDict provides method to easily manipulate nested dictionaries. NConfig is a subclass of NDict and can automatically modify configurations according to CLI arguments.
Installation
pip install naapc
Or from source code:
pip install .
Typical Usage.
Assume a configuration file test.yaml:
task:
task: classification
train:
pretrain: false
loss_args:
lr: 0.1
The typical usage is as follows:
from naapc import nconfig
from argparse import parser
parser.add_argument("-c", type=str, dest="config")
args, extra_args = parser.parse_known_args(["-c", "test.yaml", "--task;task", "regression", "--train;loss_args;lr", "0.2", "--train;pretrain", "1", "--others", "other"])
with open(args.config, "r") as f:
raw = yaml.safe_load(f)
config = nconfig(raw)
extra_args = config.parse_update(parser, extra_args)
The resulting configurations:
task:
task: regression
train:
pretrain: true
loss_args:
lr: 0.2
The data type is determined by the type in the configuration file. The boolean data is treated as integer number 1 and 0 during parsing.
You may custom the arguments:
task:
task: regression
train:
pretrain: true
loss_args:
lr: 0.2
_ARGUMENT_SPECIFICATION:
task;task:
flag: --task
choices: ["regression", "classification"]
train;lr:
flag: lr
ndict Usages
for a sample configuration test.yaml file:
task:
task: classification
train:
loss_args:
lr: 0.1
and a sample list configuration test_list.yaml file:
l:
- d:
task:
task: classification
- d:
train:
loss_args:
lr: 0.1
from naapc import ndict
with open("test.yaml", "r") as f:
raw = yaml.safe_load(f)
nd = ndict(raw["d"], delimiter=";")
nd1 = ndict.from_flatten_dict(nd.flatten_dict) # nd1 == nd
nd2 = ndict.from_list_of_dict(raw["l"]) # nd2 == nd1 == nd
"task;path" in nd # "task" in raw and "path" in raw["task"]
del nd["task;path"] # del raw["task]["path]
nd["task;path"] = "cwd" # raw["task"]["path"] = Path(".").absolute()
nd.flatten_dict # {"task;task": "classification", "train;loss_args;lr": 0.1}
nd.flatten_dict_split # raw["l"]
nd.paths # ["task", "task;task", "train", "train;loss_args", "train;loss_args;lr"]
nd.get("task;seed", 1) # raw["task"].get("seed", 1)
nd.raw_dict # raw
nd.size # len(nd.flatten_dict)
nd.update({"task;here": "there"}) # raw["task]["here] = "there
nd.items() # raw.items()
nd.keys() # raw.keys()
nd.values() # raw.values()
len(nd) # len(raw)
bool(nd) # len(nd) > 0
nd1 == nd # nd1.flatten_dict == nd.flatten_dict
nd1["task;path"] = "xcwd"
nd1["task;extra"] = "ecwd"
nd["train;epochs"] = 100
nd.compare_dict(nd1) # {"task;path": ("cwd", "xcwd"), "task;extra": (None, ecwd), "train;epochs": (100, None)}
nd.is_matched(
{
"task;path": "ecwd",
"train;epochs": "!QUERY d[path] == d['train;minimum_epochs']"
}
) # Test if this dictionary is what you want.
nconfig Usage
nconfig only supports int, str, float, bool, and list of these types. The nconfig automatically checks data type when modifications are applied. Note that argument specification ("_ARGUMENT_SPECIFICATION") does not count as part of the configurations but will be saved when use save() method. The path specified as "_IGNORE_IN_CLI" will not be added to the parser.
config.save("path.yaml") # Save configurations as a yaml file
config.add_to_argparse(parser) # Generate cli arguments for every configuration.
config.parse_update(parser, args) # Parse cli arguments and update corresponding configuration. Extra arguments will be returned.
Typical specifications are as follows:
_ARGUMENT_SPECIFICATION:
task;task:
flag: --task
task;seed:
flag: --seed
task;device:
flag: -d
data;dataset:
choices: ["cifar", "imagenet", "asap"]
log;comet_ml_key:
_IGNORE_IN_CLI
Other functionalities are the same to NDict.
Typing
Add a type
NestedOrDict = Union[ndict, dict]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file naapc-1.6.0.tar.gz
.
File metadata
- Download URL: naapc-1.6.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 276af3dd055b46978935f761abdab592c7d2591c91b66a584af593358bd721dc |
|
MD5 | b21269c19741beb4726b7b75e7054ea9 |
|
BLAKE2b-256 | 5f7ed4b0acf6e6ff3e5cbcbf69bd92746a0f657d3b62e880e7a2d5f22d793526 |
File details
Details for the file naapc-1.6.0-py3-none-any.whl
.
File metadata
- Download URL: naapc-1.6.0-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3411ff3fe0eea9b7b0c2883402e0a9e01d1201b5ebeee5739d88e4e94fbc5f4d |
|
MD5 | 6056c4dbcfe85aeb92fc35e8306fd291 |
|
BLAKE2b-256 | faa906d72e81ca146d2c4728ba7349ef2c20ce4fccc6418b22cf9910d31f4dfb |