Skip to main content

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.

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
from naapc import NDict

with open("test.yaml", "r") as f:
  raw = yaml.safe_load(f)
nd = NDict(raw)

nd1 = NDict.from_flatten_dict(nd.flatten_dict) # 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.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.copy()                        # nd1 = deepcopy(nd)
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)}

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.

config.save("path.yaml")               # Save configurations as a yaml file

Other functionalities are the same to NDict.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

naapc-1.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

naapc-1.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file naapc-1.1.0.tar.gz.

File metadata

  • Download URL: naapc-1.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for naapc-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a2b72263a754223347f9fd7dd23e736cd9ae2a52bf1a5d5dfe1c4545beb43031
MD5 4cbfb36d01dd9266f45ec41f8c94c88f
BLAKE2b-256 8d8552d69184db7dd76dffc5b82a683f432dce012fe7a6d7c2eb0027a5a3071d

See more details on using hashes here.

File details

Details for the file naapc-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: naapc-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for naapc-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31f19d186097534a5104c1bd5cd7dab625155fc520eeac43f0a7461d3fc58268
MD5 aa1d1c61c0cfbbb8b8302e6e056e7398
BLAKE2b-256 015a0475cf4b60dc5efe5ce229d94a41b9e8a54e993a0b3b452db982ff520113

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page