Giter Club home page Giter Club logo

cargparse's Introduction


cargparse

Parse and validate configuration files with argparse.

Python version PyPI version code style: black

Installation

pip install cargparse

Basic usage

Given config.yaml:

text: hello world
number: 42

Load the file as a string and use argparse as you normally would for command line arguments!

import argparse
import cargparse
import ruamel.yaml # alternatively: pyyaml, strictyaml, etc.

with open("config.yaml") as f:
    yaml = ruamel.yaml.safe_load(f)

parser = argparse.ArgumentParser()
parser.add_argument("--text", type=str, required=True)
parser.add_argument("--number", type=int, required=True)
parser.add_argument("--decimal", type=float)
config = cargparse.Cargparse(parser).parse_dict(yaml)
>> config
{"text": "hello world", "number": 42)
>> config.text
"hello world"
>> type(config.number)
<class "int">

⚠️ Read the documentation for more information about type validation.

Advanced usage

You are not restricted to a flat hierarchy.

model:
  lstm:
    input_size: 100
    hidden_size:
      - 128
      - 64
  summary: True

Define a helper function to parse each nested section args, which is interpreted as a dictionary str.

from __future__ import annotations

def parse_config(filename: Path | str) -> cargparse.Namespace:

    def model_namespace(args: str) -> cargparse.Namespace:
        parser = argparse.ArgumentParser()
        parser.add_argument("--cnn", type=cnn_namespace)
        parser.add_argument("--lstm", type=lstm_namespace)
        parser.add_argument("--summary", type=cargparse.boolean)
        return cargparse.Cargparse(parser).parse_dict(args)

    def cnn_namespace(args: str) -> cargparse.Namespace:
        parser = argparse.ArgumentParser()
        parser.add_argument("--in_channels", type=int, required=True)
        parser.add_argument("--out_channels", type=int, required=True)
        parser.add_argument("--kernel_width", type=int, required=True)
        return cargparse.Cargparse(parser).parse_dict(args)

    def lstm_namespace(args: str) -> cargparse.Namespace:
        parser = argparse.ArgumentParser()
        parser.add_argument("--input_size", type=int, required=True)
        parser.add_argument("--hidden_size", type=cargparse.list_int, required=True)
        return cargparse.Cargparse(parser).parse_dict(args)

    with open(filename) as f:
        yaml = ruamel.yaml.safe_load(f)

    parser = argparse.ArgumentParser()
    parser.add_argument("--model", type=model_namespace, required=True)
    return cargparse.Cargparse(parser).parse_dict(yaml)
>> config.model.cnn
>> config.model.lstm.hidden_units
*** AttributeError: hidden_units not in namespace: ["hidden_size", "input_size"]
>> config.model.lstm.hidden_size
[128, 64]

⚠️ Read the documentation for more information about type validation.

cargparse's People

Contributors

edemattos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.