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
. bool
s 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
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 typarse-4.2.0.tar.gz
.
File metadata
- Download URL: typarse-4.2.0.tar.gz
- Upload date:
- Size: 19.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c59040c1fce8d82263fa8dfef5ce726fbf7e87a87978d11faf1db97bc2cbbcf7 |
|
MD5 | 1e0d5952c07ddeb817d6a5bc3dfb2350 |
|
BLAKE2b-256 | bfe7d40f6eee8c5a6f8a7302fdd292b6548b3e4f1bf52a922ed1436b7acb7904 |
File details
Details for the file typarse-4.2.0-py3-none-any.whl
.
File metadata
- Download URL: typarse-4.2.0-py3-none-any.whl
- Upload date:
- Size: 16.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e451201c52605b96e50cd9f9a0f8c2056f12d6e0b4c6b72cfd60ba3c38574b7e |
|
MD5 | 804bdcb1f969692b60b46559a26bc69e |
|
BLAKE2b-256 | e3613a4cd06467ecff7152a1d3c6488d9917e98fe31348aeef8f0030571ee85c |