API reference#
Parse: Sled to Python dict#
from_sled
#
Parse the input text into a Python dict.
The input text should be a valid Sled document.
Otherwise, raises an InvalidSledError.
Serialize: Python object to Sled#
to_sled
#
to_sled(obj: object, *, indent: str = DEFAULT_INDENT, use_top_level_braces: bool = DEFAULT_USE_TOP_LEVEL_BRACES, line_separator: LineSeparator = DEFAULT_LINE_SEPARATOR, always_quote: bool = DEFAULT_ALWAYS_QUOTE, break_on_line_separator: bool = DEFAULT_BREAK_ON_LINE_SEPARATOR, ascii_only: bool = DEFAULT_ASCII_ONLY, quote_mark: QuoteMarkType = DEFAULT_QUOTE_MARK, hex_upper_case: bool = DEFAULT_HEX_UPPER_CASE, hex_horizontal_separator: HexHorizontalSeparator = DEFAULT_HEX_HORIZONTAL_SEPARATOR, hex_bytes_per_separator: int = DEFAULT_HEX_BYTES_PER_SEPARATOR, hex_line_length: int = DEFAULT_HEX_LINE_LENGTH, decimal_mark: DecimalMarkType = DEFAULT_DECIMAL_MARK, exponent_prefix: ExponentPrefixType = DEFAULT_EXPONENT_PREFIX, use_thousands_separator: bool = DEFAULT_USE_THOUSANDS_SEPARATOR) -> str
Serializes the input obj as a (top level) Sled document.
The input obj must be a dataclass or a Mapping where:
- every key must be serialized as a Sled string
- every value must either be an Entity instance or have a
to_sled_serializable() method that returns an Entity instance
If the input obj is both a Mapping and a dataclass,
it will be serialized as a Mapping.
This function has the same configuration options and defaults
as SledSerializer.
For details, the SledSerializer documentation below.
Example:
data = {
"name": "John Doe",
"age": 50,
"children": ["Jack", "Jill"],
}
sled_output = to_sled(data)
Sled output:
SledSerializer
#
Converts Python objects to Sled.
Example:
data = {
"name": "John Doe",
"age": 50,
"children": ["Jack", "Jill"],
}
sled_serializer = SledSerializer()
sled_output = sled_serializer.to_sled(data)
Sled output:
__init__
#
__init__(*, indent: str = DEFAULT_INDENT, use_top_level_braces: bool = DEFAULT_USE_TOP_LEVEL_BRACES, line_separator: LineSeparator = DEFAULT_LINE_SEPARATOR, always_quote: bool = DEFAULT_ALWAYS_QUOTE, ascii_only: bool = DEFAULT_ASCII_ONLY, break_on_line_separator: bool = DEFAULT_BREAK_ON_LINE_SEPARATOR, quote_mark: QuoteMarkType = DEFAULT_QUOTE_MARK, hex_upper_case: bool = DEFAULT_HEX_UPPER_CASE, hex_horizontal_separator: HexHorizontalSeparator = DEFAULT_HEX_HORIZONTAL_SEPARATOR, hex_bytes_per_separator: int = DEFAULT_HEX_BYTES_PER_SEPARATOR, hex_line_length: int = DEFAULT_HEX_LINE_LENGTH, decimal_mark: DecimalMarkType = DEFAULT_DECIMAL_MARK, exponent_prefix: ExponentPrefixType = DEFAULT_EXPONENT_PREFIX, use_thousands_separator: bool = DEFAULT_USE_THOUSANDS_SEPARATOR) -> None
| PARAMETER | DESCRIPTION |
|---|---|
|
The incremental indent added for each layer of nesting. Can only contain spaces and tabs.
TYPE:
|
|
If
TYPE:
|
|
The
TYPE:
|
|
If
TYPE:
|
|
If
TYPE:
|
|
If
TYPE:
|
|
Symbol with which to enclose each
TYPE:
|
|
If
TYPE:
|
|
Character (if any) with which to separate adjacent groups of hexadecimal characters.
TYPE:
|
|
Defines the length of each group of hexadecimal characters
by the number of bytes that they represent.
Two hexadecimal characters represent a byte, so the number of
hexadecimal characters per group is twice this number.
Positive values count from the right, negative from the left.
If
TYPE:
|
|
Max number of characters per line in
TYPE:
|
|
Symbol to use for separating the integer part and
fractional part of each
TYPE:
|
|
Character with which to prefix the exponent (if any)
in each
TYPE:
|
|
If
TYPE:
|
to_sled
#
Serializes the input obj as a (top level) Sled document.
The input obj must be a dataclass or a Mapping where:
- every key must be serialized as a Sled string
- every value must either be an Entity instance or have a
to_sled_serializable() method that returns an Entity instance
If the input obj is both a Mapping and a dataclass,
it will be serialized as a Mapping.
Mini serialization#
to_sled_mini
#
to_sled_mini(obj: object, *, use_top_level_braces: bool = DEFAULT_USE_TOP_LEVEL_BRACES, always_quote: bool = DEFAULT_ALWAYS_QUOTE, ascii_only: bool = DEFAULT_ASCII_ONLY, quote_mark: QuoteMarkType = DEFAULT_QUOTE_MARK, hex_upper_case: bool = DEFAULT_HEX_UPPER_CASE, decimal_mark: DecimalMarkType = DEFAULT_DECIMAL_MARK, exponent_prefix: ExponentPrefixType = DEFAULT_EXPONENT_PREFIX) -> str
Serializes the input obj as a minified (top level) Sled document.
Produces a serialization output that is more compact
but less human-friendly.
This function has the same configuration options and defaults
as SledSerializerMini.
For details, refer to the SledSerializerMini documentation below.
The input obj must be a Mapping or dataclass that does NOT have
a to_sled_serializable() method, or an object with such a method
returning such a Mapping or dataclass.
If the input obj is both a Mapping and a dataclass,
it will be serialized as a Mapping.
Example:
data = {
"name": "John Doe",
"age": 50,
"children": ["Jack", "Jill"],
}
sled_output = to_sled_mini(data)
Sled output:
SledSerializerMini
#
Converts Python objects to minified Sled. Produces a serialization output that is more compact but less human-friendly.
Example:
data = {
"name": "John Doe",
"age": 50,
"children": ["Jack", "Jill"],
}
sled_serializer = SledSerializer()
sled_output = sled_serializer.to_sled(data)
Sled output:
__init__
#
__init__(*, use_top_level_braces: bool = DEFAULT_USE_TOP_LEVEL_BRACES, always_quote: bool = DEFAULT_ALWAYS_QUOTE, ascii_only: bool = DEFAULT_ASCII_ONLY, quote_mark: QuoteMarkType = DEFAULT_QUOTE_MARK, hex_upper_case: bool = DEFAULT_HEX_UPPER_CASE, decimal_mark: DecimalMarkType = DEFAULT_DECIMAL_MARK, exponent_prefix: ExponentPrefixType = DEFAULT_EXPONENT_PREFIX) -> None
| PARAMETER | DESCRIPTION |
|---|---|
|
If
TYPE:
|
|
If
TYPE:
|
|
If
TYPE:
|
|
Symbol with which to enclose each
TYPE:
|
|
If
TYPE:
|
|
Symbol to use for separating the integer part and
fractional part of each
TYPE:
|
|
Character with which to prefix the exponent (if any)
in each
TYPE:
|
to_sled
#
Serializes the input obj as a (top level) Sled document.
The input obj must be a dataclass or a Mapping where:
- every key must be serialized as a Sled string
- every value must either be an Entity instance or have a
to_sled_serializable() method that returns an Entity instance
If the input obj is both a Mapping and a dataclass,
it will be serialized as a Mapping.