Skip to content

Reduce a complex Python structure to basic types

[Home | sourcehut | PyPI | ReadTheDocs]

Overview

The as-dict-rec Python library mainly provides the as_dict_rec() function that takes an arbitrarily complex Python data structure (a dictionary, a dataclass, a set of lists of complex numbers, etc) and converts it to a form that may be serialized into an e.g. JSON, TOML, or similar representation.

Installation

A program that uses the as-dict-rec library should specify it in its list of requirements, e.g. using PEP508 syntax:

as-dict-rec >= 0.1.10, < 0.2

Basic usage

Convert a structure to a JSON representation:

print(json.dumps(as_dict_rec.as_dict_rec(complex_data), indent=2))

Skip null values in lists, dictionaries, etc, since TOML cannot represent them:

print(tomli_w.dumps(as_dict_rec.as_dict_rec(complex_data, skip_null=True), indent=2))

Contact

The as-dict-rec library was written by Peter Pentchev. It is developed in a sourcehut repository. This documentation is hosted at Ringlet with a copy at ReadTheDocs.