Skip to main content

StructLayoutToolkitCodec

sltcodec encodes and decodes bytearrays according to structured layout definitions. It uses sltcore for bit-level access.

Installation

pip install sltcodec

Quick Example

StructLayout bundles the root structure name with its TypeDict. Pass the same layout to both encode and decode.

from sltcore import InfoSize
from sltcodec import (
    FieldDef,
    FieldInstance,
    StructDef,
    StructInstance,
    StructLayout,
    TypeDict,
    decode,
    encode,
)

fields = [
    FieldDef(name="flag", offset=InfoSize(0, 0), size=InfoSize(0, 1), type="bool"),
    FieldDef(name="value", offset=InfoSize(0, 1), size=InfoSize(1, 0), type="unsigned int"),
]
struct_def = StructDef(name="Packet", fields=fields)
layout = StructLayout(
    struct_def_name="Packet",
    type_dict=TypeDict(struct_dict={"Packet": struct_def}),
)
instance = StructInstance(
    struct_def=struct_def,
    field_instances=[
        FieldInstance(fields[0], True),
        FieldInstance(fields[1], 0xA5),
    ],
)

encoded = encode(layout, instance, bytearray())
decoded = decode(layout, encoded)

Progress Callbacks

encode and decode accept an optional progress_callback argument. The callback is typed as ProgressCallback and receives a float progress value from 0.0 to 1.0.

Progress is calculated for top-level fields as (offset + size) / total_size. Nested structure encode/decode calls pass None internally, so recursive fields do not emit additional callback events.

encode and decode also accept an optional env dictionary for evaluating expression-based field definitions. The default is None, which uses an empty environment. The same environment is passed to recursive nested structure encode and decode calls.

from sltcodec import ProgressCallback, decode

def update_progress(progress: float) -> None:
    progress_bar.value = progress

progress_callback: ProgressCallback = update_progress
decoded = decode(layout, encoded, progress_callback=progress_callback)

decode returns a StructInstance whose size is the actual end of the decoded layout. decode_field stores the actual decoded size in the returned FieldInstance.field_def.size. For nested StructDef fields, that size comes from the nested StructInstance.size; for repeated fields, the next element's offset advances by the actual size of the previous element.

FieldDef.repeat also accepts the special string value "end". When repeat="end", decoding repeats until the input bytearray ends. Decoding stops when the next element offset is out of range or when the next element size would exceed the remaining input.

Field Types

The public PRIMITIVE_TYPES set contains:

{"bool", "signed int", "int", "unsigned int", "float", "bytearray", "bytes"}

FieldDef.type may also be a nested StructDef or an expression string. Expressions can use values from previously processed fields, allowing dynamic types, sizes, and repeat counts. Repeated fields, byte swapping, padding, ranges, and enum metadata are supported.

Enums And Named Structures

Store named structures in TypeDict.struct_dict and enums in TypeDict.enum_dict. A field refers to an enum by its enum_def_name; the codec resolves it through StructLayout.type_dict.

from sltcore import InfoSize
from sltcodec import EnumDef, FieldDef, StructDef, StructLayout, TypeDict, decode

enum_def = EnumDef(name="Status", values={"OK": 0, "NG": 1})
packet = StructDef(
    name="Packet",
    fields=[FieldDef(
        name="status",
        offset=InfoSize(0, 0),
        size=InfoSize(1, 0),
        type="unsigned int",
        enum_def_name="Status",
    )],
)
layout = StructLayout(
    struct_def_name="Packet",
    type_dict=TypeDict(
        struct_dict={"Packet": packet},
        enum_dict={"Status": enum_def},
    ),
)

decoded = decode(layout, bytearray(b"\x01"))
assert decoded.field_instances[0].enum_item == ("NG", 1)

Saving And Loading

Persist a complete StructLayout, including its root structure name, structure definitions, and enum definitions, with save_struct_layout and load_struct_layout.

from pathlib import Path
from sltcodec import load_struct_layout, save_struct_layout

path = Path("struct_layout.json")
save_struct_layout(layout, path)
loaded = load_struct_layout(path)
assert loaded == layout

InfoSize values are stored as typed JSON dictionaries. Expression-based offsets and sizes remain strings and are evaluated when the layout is used.

Public API Reference

The following symbols are exported by sltcodec.__all__ in this order:

Symbol Description
PRIMITIVE_TYPES Set of built-in field type names supported by the codec.
ProgressCallback Callable type alias for encode/decode progress updates.
EnumDef Immutable definition of an enumeration.
EnumDict Dictionary-like container for EnumDef objects.
FieldDef Immutable definition of one structured field.
FieldInstance Immutable field definition and decoded/encodable value pair.
StructDef Immutable definition that groups fields into a structure.
StructDict Dictionary-like container for StructDef objects.
StructInstance Decoded/encodable structure instance containing field instances.
StructLayout Bundle of the root structure name and its TypeDict.
TypeDict Bundle of named structure and enum dictionaries.
decode Decode bytes according to a StructLayout.
encode Encode a StructInstance according to a StructLayout.
load_struct_layout Load a StructLayout from JSON.
save_struct_layout Save a StructLayout as JSON.

Types (types.py)

Classes are listed in their definition order. The member tables show the dataclass fields or constructor attributes. Methods beginning with _ are internal implementation details and are omitted from the public method tables.

EnumDef

Member Type Description
name str Enumeration name.
description str | None Optional description.
values dict[str, int] Mapping from enumeration names to integer values.
Method Description
to_dict() Convert the definition to a JSON-compatible dictionary.
to_json() Convert the definition to a JSON string.
from_dict(data) Create an EnumDef from a dictionary.
serialize() Convert the definition to a typed dictionary.
deserialize(data) Create an EnumDef from a typed dictionary.
from_json(data) Create an EnumDef from a JSON string.
__lt__(other) Compare enum definitions using their serialized ordering.

FieldDef

Member Type Description
name str Field name.
offset InfoSize | str Static offset or expression.
size InfoSize | str Static size or expression.
type str | StructDef Primitive, named, or nested field type.
scale float Numeric scale applied to the field.
repeat int | str | None Number of repeated field values, an expression that evaluates to the count, or "end" to decode repeatedly until input end.
description str | None Optional field description.
range_expression str | None Optional value-range expression.
enum_def_name str | None Name of the associated enum definition.
byte_swap bool | str Whether to reverse bytes, or an expression controlling it.
Method Description
to_dict() Convert the definition to a JSON-compatible dictionary.
to_json() Convert the definition to a JSON string.
from_dict(data) Create a FieldDef from a dictionary.
from_json(data) Create a FieldDef from a JSON string.
__lt__(other) Compare field definitions using their serialized ordering.

FieldInstance

Member Type Description
field_def FieldDef Definition of the field.
value Any Decoded or encodable field value.
enum_item tuple[str, int] | None Matching enum name and value, when available.
is_padding bool Whether this instance represents padding.
Method Description
range_check(env=None) Evaluate the field's range expression.
from_value(field_def, value, type_dict=None, is_padding=False) Create an instance and resolve matching enum metadata.
with_value(value, type_dict=None) Return a new instance with the given value; keeps field_def and is_padding and recomputes enum_item.
__lt__(other) Compare field instances using field-definition order.

StructDef

Member Type Description
name str Structure name.
description str Structure description.
fields list[FieldDef] Ordered field definitions.
Method Description
get_field(struct_def_name) Return the first field whose type references the given structure name.
get_fields(struct_def_name) Return all fields whose type references the given structure name.
to_dict() Convert the definition to a JSON-compatible dictionary.
to_json() Convert the definition to a JSON string.
from_dict(data) Create a StructDef from a dictionary or legacy field list.
from_json(data) Create a StructDef from a JSON string.
__lt__(other) Compare structure definitions using their serialized ordering.

StructInstance

Member Type Description
struct_def StructDef Structure definition for the instance.
field_instances list[FieldInstance] Stored field instances, kept in field order.
size InfoSize Total structure size.
Method Description
append_field_instance(field_instance) Append one field instance.
extend_field_instances(field_instances) Append multiple field instances.
get_field(field_def_name) Return the first field instance whose field_def.name matches.
get_fields(field_def_name) Return all field instances whose field_def.name matches.
__iter__() Iterate over field instances.
__len__() Return the number of field instances.
__getitem__(index) Get a field instance by index.
__post_init__() Normalize field order, size, and generated padding after construction.
__lt__(other) Compare structure instances using their serialized ordering.

EnumDict

Member Type Description
_items dict[str, EnumDef] Stored enum definitions. Use mapping operations or items_dict().
Method Description
__getitem__(key) Get an enum definition by name.
__setitem__(key, value) Store an enum definition.
__delitem__(key) Delete an enum definition.
__iter__() Iterate over enum names.
__len__() Return the number of enum definitions.
items_dict() Return the underlying dictionary.

StructDict

Member Type Description
_items dict[str, StructDef] Stored structure definitions. Use mapping operations or items_dict().
Method Description
__getitem__(key) Get a structure definition by name.
__setitem__(key, value) Store a structure definition.
__delitem__(key) Delete a structure definition.
__iter__() Iterate over structure names.
__len__() Return the number of structure definitions.
items_dict() Return the underlying dictionary.

TypeDict

Member Type Description
enum_dict EnumDict Named enum definitions.
struct_dict StructDict Named structure definitions.

TypeDict(enum_dict=None, struct_dict=None) constructs both containers from optional dictionaries.

StructLayout

Member Type Description
struct_def_name str Key of the root structure in type_dict.struct_dict.
type_dict TypeDict Structure and enum definitions used for resolution.

StructLayout(struct_def_name, type_dict) constructs a layout bundle.

Codec (codec.py)

The following public definitions are listed in their definition order. Names beginning with _ are internal helpers and are not part of the public API.

Definition Signature Description
PRIMITIVE_TYPES set[str] Built-in field type names.
ProgressCallback Callable[[float], None] Progress callback type for top-level encode/decode updates.
save_struct_layout (struct_layout, path) -> None Save a layout to a JSON file.
load_struct_layout (path) -> StructLayout Load a layout from a JSON file.
save_struct_def_dict (path, struct_def_dict) -> None Save a structure-definition dictionary through the layout format.
load_struct_def_dict (path) -> dict[str, StructDef] Load a structure-definition dictionary.
save_enum_def_dict (path, enum_def_dict) -> None Save an enum-definition dictionary through the layout format.
load_enum_def_dict (path) -> dict[str, EnumDef] Load an enum-definition dictionary.
encode (struct_layout, struct_instance, buf, padding_alignment_bits=32, progress_callback=None, env=None) -> bytearray Encode a complete structure. env provides values for expression evaluation and is inherited by nested encodes.
decode_field (field_def, data, env=None, type_dict=None, padding_alignment_bits=32) -> FieldInstance | None Decode one field.
decode (struct_layout, data, padding_alignment_bits=32, progress_callback=None, env=None) -> StructInstance Decode a complete structure. env provides values for expression evaluation and is inherited by nested decodes.

save_struct_def_dict, load_struct_def_dict, save_enum_def_dict, and load_enum_def_dict are available from sltcodec.codec for dictionary-level compatibility. The package root exports the complete StructLayout API: save_struct_layout and load_struct_layout.

Metadata

Release files for sltcodec 2.7.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sltcodec 2.7.4
File Size Uploaded
sltcodec-2.7.4.tar.gz 33.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sltcodec 2.7.4
File Interpreter ABI Platform
sltcodec-2.7.4-py3-none-any.whl Python 3 none any Details

Total release size: 52.3 kB

Release files / sltcodec-2.7.4.tar.gz

Download URL sltcodec-2.7.4.tar.gz
Size 33.9 kB
Tags Source
SHA-256 checksum
How to use checksums
49e37ce05302a2d7cb01909977f7b5c03b0504ca80b0893738b1bcf5bd233bf6
BLAKE2b-256 checksum
How to use checksums
175781f149679d49504af29e321d9b332ef3744441783052d49513ada1c80285
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / sltcodec-2.7.4-py3-none-any.whl

Download URL sltcodec-2.7.4-py3-none-any.whl
Size 18.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1dbe39b34078a663b7326e79cd996fea4427c62e1fb29f5c27df90c4d4f17261
BLAKE2b-256 checksum
How to use checksums
3c1a9e49a187668811f584578667f6f56c1e2d9e6b605f394773c862a0d35887
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

2.7.4 This release

2 release files

2.7.3

2 release files

2.7.2

2 release files

2.7.1

2 release files

2.7.0

2 release files

2.6.1

2 release files

2.6.0

2 release files

2.5.2

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.1

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page