Skip to main content

Simple type-based argument parsing

Project description

Typarse

This is a small project born out of my frustration with simple argument parsing in Python.

Not only do I have to instantiate some object whose name I can never remember, then I get way too many function parameters to get them right... It's a mess. And I don't even need half the features.

So this is an attempt at streamlining this process while simultaneously promoting some better type safety, by using the magic of Python type hints!

Really all the magic here is happening in the BaseParser class. You just need to subclass it, add a few typed parameters, perhaps with some extra information in dictionaries... and you're done! For examples, just look at, well, examples.

Supported types

First of all, all the basic types supported by argparse are also supported here. Things like: str, int, float. bools are automatically interpreted as flags, False by default. Each type can be wrapped in a List to support reading them like --list 1 2 3 4. Each type can also be made Optional which makes it, well, optinal.

Simple illustrative example

from typarse import BaseParser
from typing import List


class Parser(BaseParser):
    nums: List[int]
    square: bool
    default: int = 0

    _abbrev = {
        "nums": "n",
        "square": "s",
        "default": "d"
    }

    _help = {
        "nums": "List of numbers to sum",
        "square": "Whether the result should be squared",
        "default": "Initial value, added to the sum"
    }



args = Parser()

print(args.default + sum(args.nums) ** (1+args.square))

Functionality 2: Config

In the spirit of the library, I added a config management component. Similarly to parsing, you can define a config using type annotations on a class -- also in a nested manner.

Example:

from typarse import BaseConfig
from typing import List 

class Config(BaseConfig):
    rate: float = 0.1
    amount: float = 0.01
    limit: float = 1.
    class PolicyConfig(BaseConfig):
        layers: List[int] = [32, 32, 32]
        activation: str = "relu"

config = {
    "rate": 0.5,
    "PolicyConfig": {
        "layers": [64, 64]
    }
}

Installation

It's on PyPi so install it with pip install typarse, poetry add typarse or whatever you want. If you want to install it from the source, it's pure Python so I'm sure you can figure it out.

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

typarse-4.2.0.tar.gz (19.4 kB view hashes)

Uploaded Source

Built Distribution

typarse-4.2.0-py3-none-any.whl (16.7 kB view hashes)

Uploaded Python 3

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