dataclass_wizard.wizard_cli package¶
Submodules¶
dataclass_wizard.wizard_cli.cli module¶
Entry point for the Wizard CLI tool.
- class dataclass_wizard.wizard_cli.cli.FileTypeWithExt(mode='r', ext=None, bufsize=-1, encoding=None, errors='ignore')[source]¶
Bases:
FileType
Extends
argparse.FileType
to add a default file extension if the provided file name is missing one.
- dataclass_wizard.wizard_cli.cli.gen_py_schema(args)[source]¶
Entry point for the wiz gen-schema (gs) command.
- dataclass_wizard.wizard_cli.cli.get_div(out_file: TextIO, char='_', line_width=50)[source]¶
Returns a formatted line divider to print.
- dataclass_wizard.wizard_cli.cli.main(args=None)[source]¶
A companion CLI tool for the Dataclass Wizard, which simplifies interaction with the Python dataclasses module.
- dataclass_wizard.wizard_cli.cli.parser: ArgumentParser¶
dataclass_wizard.wizard_cli.schema module¶
Generates a Python (dataclass) schema, given a JSON input. The entry point for this module is the gen-schema subcommand.
This JSON to Dataclass conversion tool was inspired by the following projects:
The parser supports the full JSON spec, so both list and dict as the root type are properly handled as expected.
A few important notes on the behavior of JSON parsing:
Lists with multiple dictionaries will have all the keys and type definitions merged into a single model dataclass, as the dictionary objects are considered homogenous in this case.
Nested lists within the above structure (e.g. list -> dict -> list) should similarly merge all list elements with the list for that same key in each sibling dict object. For example, assuming the below input:
... [{"d1": [1, {"k": "v"}]}, {"d1": [{"k": 2}, {"k2": "v2"}, True]}]
- This should result in a single, merged type definition for “d1”::
… List[Union[int, dataclass(k: Union[str, int], k2: str), bool]]
Any nested dictionaries within lists will have their Model class name generated with the singular form of the key containing the model definition – for example, {“Items”:[{“key”:”value”}]} will result in a model class named Item. In the case a dictionary is nested within a list, it will have the class name auto-incremented with a common prefix – for example, Data1, Data2, etc.
The implementation below uses regex code in the rules.english module from the library Python-Inflector (https://github.com/bermi/Python-Inflector).
This library is available under the BSD license, which can be obtained from https://opensource.org/licenses.
The library Python-Inflector contains the following attribution notices:
Copyright (c) 2006 Bermi Ferrer Martinez bermi a-t bermilabs - com
See the end of this file for the original BSD-style license from this library.
- class dataclass_wizard.wizard_cli.schema.PyCodeGenerator(file_name: dataclasses.InitVar[str] = None, file_contents: dataclasses.InitVar[str] = None, force_strings: dataclasses.InitVar[bool] = None, experimental: dataclasses.InitVar[bool] = None)[source]¶
Bases:
object
This is the main class responsible for generating Python code that leverages dataclasses, given a JSON object as an input data.
- data: List[Any] | Dict[str, Any]¶
- experimental: dataclasses.InitVar[bool] = None¶
- file_contents: dataclasses.InitVar[str] = None¶
- file_name: dataclasses.InitVar[str] = None¶
- force_strings: dataclasses.InitVar[bool] = None¶
- parser: JSONRootParser¶
- property py_code: str¶