rogiervandergeer / pydargs Goto Github PK
View Code? Open in Web Editor NEWPydargs allows configuring a (Pydantic) dataclass through command line arguments.
License: BSD 3-Clause "New" or "Revised" License
Pydargs allows configuring a (Pydantic) dataclass through command line arguments.
License: BSD 3-Clause "New" or "Revised" License
Support actions, and action specific arguments, for example:
For example:
class Action1:
a: int
class Action2:
b: str
class Config:
action: Union[Action1, Action2]
Which would allow either:
cmd Action1 --a 1
cmd Action2 --b abc
Allow lists (or sequences). Would be great if we can support two options for:
@dataclass
class Config:
a: list[int]
cmd --a 1 --a 2 --a 3
cmd --a [1, 2, 3]
Where option 2 is probably best implemented as JSON.
We most likely need an option for the user to choose between one of the two instead of allowing both
Support dates
Either as a flag to the metadata, or as an argument to for example skip all fields that end with a certain postfix.
As bytes("a string")
also requires an encoding, we would need an encoding metadata field
Support enums AND literals
Allow the user to pass arguments to
ArgumentParser(...)
to add help for the whole command,Consider the following script:
from dataclasses import dataclass, field
from pydargs import parse
@dataclass
class Name:
first_name: str
last_name: str
full_name: str = field(init=False)
def __post_init__(self):
self.full_name = f"{self.first_name} {self.last_name}"
parse(Name)
When called as python example.py --first-name John --last-name Doe
, it errors: example.py: error: the following arguments are required: --full-name
even though I can instantiate Name
without supplying a value for full_name
.
It's easily resolved in practice by giving full_name
any default (e.g. an empty string) which is then immediately overwritten in __post_init__
, so it's not a big issue. Still, it would be less surprising if pydargs and the dataclass itself agreed on which fields are mandatory.
Support a short option, such that
@dataclass
class Config:
a_long_field: str
can also be configured to allow -a
Support Unions.
MVP: support unions such that Optional[int]
can have a None
as default (i.e. all arguments are parsed as int)
Better: allow Union[int, str]
that would parse "42"
as int and "abc"
as str.
Some of our desires features will need to accept configuration, for example the input method of lists / sequences.
We need to decide how we expect the user to provide this information. I see two options:
Use the metadata
of the dataclasses.field
see here
@dataclass
class Config:
a_field: str = field(metadata=dict(of="whatever", fields="we", will="need")
@dataclass
class Config:
a_field: Annotated[str, dict(of="whatever", fields="we", will="need")]
But this may be difficult to do well in python3.9.
Support dataclasses as an argument.
For example:
@dataclass
class Sub:
a: int
b: str
@dataclass
class Config:
sub: Sub
It would be great if this could be supported with:
--sub-a 42 --sub-b abc
, as well as--sub '{"a": 42, "b": "abc"}
Implement booleans, such that
@dataclass
class Config:
b: bool
Creates two arguments: --b
which sets b to True
, and --no-b
which sets b to False
.
Write documentation, in the README
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.