itacart.serialization.tree_blob.encode_tree

itacart.serialization.tree_blob.encode_tree(index)[source]

Encode a compositional index as a TreeBlob.

Canonicalisation is the encoder’s job, not the caller’s. The index is recomposed into canonical prefix form here, so equivalent spellings produce identical bytes and calling recompose_to_prefix_form() beforehand is redundant rather than required. It stays public because two of the format’s properties are stated in terms of it and because a reader verifying a hash needs to name the form the bytes are in. A cell written whole absorbs any descendant also written under it, so an index naming a cell and one of its descendants encodes the cell – the region it names.

Note that itacart.normalize() is not that form: it collapses a complete sibling set into its parent, which preserves the region while changing the leaf set. Passing a normalised index encodes a different, smaller cell set, on purpose.

Parameters:

index (str) – Compositional index string.

Return type:

bytes

Returns:

The binary blob.

Raises:

InvalidIndexError – If the index is malformed, or if it names row 1000, which holds the polar caps: this version of the binary codecs does not serialize them.

Example

>>> encode_tree("NE(0625/0451(2,1))") == encode_tree("NE(0625/0451(1,2))")
True