API Reference
The main function
as_dict_rec.as_dict_rec(obj, *, skip_null=False, overrides=None)
Reduce a complex Python structure, e.g. a dataclass, to basic types.
Useful for e.g. serializing to JSON, TOML, etc.
Source code in src/as_dict_rec/impl.py
as_dict_rec.Overrides
dataclass
Override some of the default handlers.
Source code in src/as_dict_rec/impl.py
as_dict_handlers = dataclasses.field(default_factory=(lambda: AS_DICT_HANDLERS))
class-attribute
instance-attribute
The types that we handle in special ways before anything else.
basic_types = dataclasses.field(default_factory=(lambda: BASIC_TYPES))
class-attribute
instance-attribute
The types that can usually be serialized directly.
list_types = dataclasses.field(default_factory=(lambda: LIST_TYPES))
class-attribute
instance-attribute
The types that we represent as a list.
sorted_list_types = dataclasses.field(default_factory=(lambda: SORTED_LIST_TYPES))
class-attribute
instance-attribute
The types that we represent as a list, but we sort it.
str_types = dataclasses.field(default_factory=(lambda: STR_TYPES))
class-attribute
instance-attribute
The types that we stringify.
The various lists of type conversions
as_dict_rec.AS_DICT_HANDLERS = {logging.Logger: lambda log: f'Logger("{log.name}", {logging.getLevelName(log.level)})', re.Pattern: lambda pattern: pattern.pattern, tempfile._TemporaryFileWrapper: lambda tempf: tempf.path}
module-attribute
The types that we handle in special ways before anything else.
as_dict_rec.BASIC_TYPES = (float, int, str, types.NoneType)
module-attribute
The types that can usually be serialized directly.
as_dict_rec.LIST_TYPES = (list, tuple)
module-attribute
The types that we represent as a list.
as_dict_rec.SORTED_LIST_TYPES = (set,)
module-attribute
The types that we represent as a list, but we sort it.
as_dict_rec.STR_TYPES = (enum.Enum, ipaddress.IPv4Address, ipaddress.IPv6Address, numbers.Number, pathlib.Path, uuid.UUID)
module-attribute
The types that we stringify.