clattr
Easily make a command line interface from any interface in your code. How easily? This easy:
import clattr
def my_program(a: int, b: str = "not provided"):
print(f"Running some code with: a={a}, b={b}")
if __name__ == "__main__":
clattr.call(my_program)
That's all it takes. Clattr will inspect your function and collect the values it needs it from command line arguments, environment variables, or config files, and then call it.
python examples/function --a 1
Running some code with: a=1, b=not provided
If you want to think in a more data oriented design, you can have clattr construct a data object for you and use it as you please.
import attr
import clattr
@attr.s(auto_attribs=True, frozen=True)
class Basic:
a: int
b: str = "not provided"
def my_program(data: Basic):
# Your actual program will go here. For this example we just print the input.
print(data)
if __name__ == "__main__":
data = clattr.call(Basic)
my_program(data)
This could be invoked as
python examples/basic.py --a 1 --b hi
clattr will construct this object
Basic(a=1, b='hi')
Which you can then pass into the rest of your code as you please. The example simply prints it and then exits.
Or if you have environment variables defined
export A=1
export B=hi
python example.py
again yields
Basic(a=1, b='hi')
clattr also supports nested objects (or functions taking complex objects as inputs)
from typing import Optional
import datetime as dt
import attr
import clattr
@attr.s(auto_attribs=True, frozen=True)
class Foo:
a: dt.datetime
b: Optional[str] = None
@attr.s(auto_attribs=True, frozen=True)
class Bar:
f: Foo
c: int
def my_program(data: Bar):
print(data)
if __name__ == "__main__":
bar = clattr.call(Bar)
my_program(bar)
You specify values for the fields in the nested class by referring to them with a their field name in the outer class
python examples/advanced.py --c 1 --f.a 2020-01-01 --f.b hi
Bar(f=Foo(a=1, b='hi'), c=1)
You can also supply one or more json formatted config files. Provide the name(s) of these files as positional arguments. `clattr`` will search them, last file first, for any keys fields that are not provided at the command line before searching the environment.
python examples/advanced.py --c 1 examples/foo.json
Bar(f=Foo(a=1, b='str'), c=1)
clattr is inspired by clout, but I wanted to try being a bit more opinionated to make both the library and code using it simpler.
Metadata
Release files for clattr 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| clattr-0.0.1.tar.gz | 6.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| clattr-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.3 kB
Release files / clattr-0.0.1.tar.gz
| Download URL | clattr-0.0.1.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3c2e60aa312d22783c144c1de83177be6aee1444cc8fa378dd311ab753f5de4a
|
|
BLAKE2b-256 checksum How to use checksums |
a11b34edc2a85576d123452d817b6ad7a05ec6a82ec71c4595826098fe4d3267
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.9
|
Release files / clattr-0.0.1-py3-none-any.whl
| Download URL | clattr-0.0.1-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5f3942e2e108304fbd627d80a786c1f0d5b395b9164291bffa50056b0c5a8499
|
|
BLAKE2b-256 checksum How to use checksums |
dbbf0159f454549075cec4cd1cd059b52a23f1a71c1832cdb4b4004a0e04e8e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.9
|