Skip to main content

Pydargs allows configuring a dataclass through command line arguments.

Project description

pydargs

Pydargs allows configuring a dataclass through command line arguments.

Installation

Pydargs can be installed with your favourite package manager. For example:

pip install pydargs

Usage

A minimal usage example would be:

from dataclasses import dataclass
from pydargs import parse


@dataclass
class Config:
    number: int
    some_string: str = "abc"

if __name__ == "__main__":
    config = parse(Config)

After which this entrypoint can be called with

entrypoint --number 42

or

entrypoint --number 42 --some-string abcd

Supported Types

The base types are supported: int, float, str, bool, as well as:

  • Enums or literals comprised of those types.
  • Bytes, with an optional encoding metadata field: a_value: bytes = field(metadata=dict(encoding="ascii")), which defaults to utf-8.
  • Date and datetime, with an optional date_format metadata field: your_date: date = field(metadata=dict(date_format="%m-%d-%Y")). When not provided dates in ISO 8601 format are accepted.
  • Lists of those types, either denoted as e.g. list[int] or Sequence[int]. Multiple arguments to a numbers: list[int] field can be provided as --numbers 1 2 3. A list-field without a default will require at least a single value to be provided. If a default is provided, it will be completely replaced by any arguments, if provided.
  • Optional types, denoted as e.g. typing.Optional[int] or int | None (for Python 3.10 and above). Any argument passed is assumed to be of the provided type and can never be None.
  • Unions of types, denoted as e.g. typing.Union[int, str] or int | str. Each argument will be parsed into the first type that returns a valid result. Note that this means that str | int will always result in a value of type str.
  • Any other type that can be instantiated from a string, such as Path.

Metadata

Additional options can be provided to the dataclass field metadata.

The following metadata fields are supported:

positional

Set positional=True to create a positional argument instead of an option.

from dataclasses import dataclass, field

@dataclass
class Config:
  argument: str = field(metadata=dict(positional=True))

as_flags

Set as_flags=True for a boolean field:

from dataclasses import dataclass, field

@dataclass
class Config:
  verbose: bool = field(default=False, metadata=dict(as_flags=True))

which would create the arguments --verbose and --no-verbose to set the value of verbose to True or False respectively, instead of a single option that requires a value like --verbose True.

parser

Provide a custom type converter that parses the argument into the desired type. For example:

from dataclasses import dataclass, field
from json import loads

@dataclass
class Config:
  list_of_numbers: list[int] = field(metadata=dict(parser=loads))

This would parse --list-of-numbers [1, 2, 3] into the list [1, 2, 3]. Note that the error message returned when providing invalid input is lacking any details. Also, no validation is performed to verify that the returned type matches the field type. In the above example, --list-of-numbers '{"a": "b"}' would result in list_of_numbers being the dictionary {"a": "b"} without any kind of warning.

Ignoring fields

Fields can be ignored by adding the ignore_arg metadata field:

@dataclass
class Config:
    number: int
    ignored: str = field(metadata=dict(ignore_arg=True))

When indicated, this field is not added to the parser and cannot be overridden with an argument.

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

pydargs-0.5.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

pydargs-0.5.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pydargs-0.5.2.tar.gz.

File metadata

  • Download URL: pydargs-0.5.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pydargs-0.5.2.tar.gz
Algorithm Hash digest
SHA256 99934f969212369c424746ffb4283d7131edb4cdce1c78ac9f1433c5d2295446
MD5 335dcd070adcc32a78d9250359aca930
BLAKE2b-256 2611918841f67e33f883586cdcfefa49723897df9a26fa2d62507b829a24b858

See more details on using hashes here.

Provenance

File details

Details for the file pydargs-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: pydargs-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pydargs-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4be8bb6d7ae42fd39079179a4b60cb8744169e97a6c16518d15b73e464fdf248
MD5 ecd14a8b13523ba631bcd269ff6d1876
BLAKE2b-256 68039451a2d1355973de3dc126a2b4aa5ab3a019a766a40a85f82f68e7274ea8

See more details on using hashes here.

Provenance

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