Skip to main content

Dataclass to be CLI and logger compatible

Project description

Dataclass to be CLI and logger compatible

This package does only two things:

  1. it takes a dataclass and converts it to a command line str.
  2. it takes a dataclass and converts it to a loggable dict for 3rd party logging like MLFlow. Contrary to asdict it also converts nested dataclasses by separting their attributes by prefix.

By inheriting from the class ReverseCli you can use the to_command_string method to get the command line str and the property loggable_dict to get the loggable dict.

Example

@dataclass(kw_only=True)
class Parameters(ReverseCli):
    a: int
    b: str
    c: bool
    d: list[int]


@dataclass(kw_only=True)
class Nested(ReverseCli):
    a: int
    b: Parameters

parameters = Nested(a=1, b=Parameters(a=1, b="2", c=True, d=[3, 4]))
print(parameters.to_command_string())
# --a 1 --b.a 1 --b.b 2 --b.c --b.d 3 --b.d 4
print(parameters.loggable_dict)
# {'a': 1, 'b.a': 1, 'b.b': '2', 'b.c': True, 'b.d': [3, 4]}

Installation

We use UV for installation and RUFF for formating and linting.

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

dataclasses_reverse_cli-0.0.3.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dataclasses_reverse_cli-0.0.3-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file dataclasses_reverse_cli-0.0.3.tar.gz.

File metadata

File hashes

Hashes for dataclasses_reverse_cli-0.0.3.tar.gz
Algorithm Hash digest
SHA256 52184b28de4e174e43d246b51b6d31885131910aaa150ad663488e9a331fb4ff
MD5 026d52df802b13794c663a10edcff1d7
BLAKE2b-256 0bdd8a367b1fe2a8287a3a71008c5c0d82f292a8615e414f6ca2f5add0951b34

See more details on using hashes here.

File details

Details for the file dataclasses_reverse_cli-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for dataclasses_reverse_cli-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dbdf990f947b377b66d5d12cd73e4d33f705123abfd69b73347a62afa8889e3c
MD5 46f3bc58ec784c1757056ee9d29daea5
BLAKE2b-256 91a7cca561f8e93789a0c1066537e9c2607b0cc9c9a6e8d0b3fa38f62f9d71c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page