API Reference
Complete reference for all public functions, classes, and constants in the crous Python module.
Core Functions
crous.dumps(obj, default=None)
Serialize a Python object to FLUX binary bytes.
| Parameter | Type | Description |
|---|---|---|
obj | Any | The Python object to serialize |
default | Callable[[Any], Any] | None | Fallback handler for unsupported types |
Returns: bytes — FLUX binary data
Raises: CrousEncodeError — if serialization fails
crous.loads(data, object_hook=None)
Deserialize FLUX/CROUS binary bytes to a Python object.
| Parameter | Type | Description |
|---|---|---|
data | bytes | Binary data to deserialize |
object_hook | Callable[[dict], Any] | None | Transform decoded dictionaries |
Returns: Any — Deserialized Python object
Raises: CrousDecodeError — if deserialization fails
crous.dump(obj, file, default=None)
Serialize and write to a file path or file object.
| Parameter | Type | Description |
|---|---|---|
obj | Any | The Python object to serialize |
file | str | IO[bytes] | File path or writable binary file object |
default | Callable | None | Fallback handler for unsupported types |
Returns: None
Raises: CrousEncodeError, IOError
crous.load(file, object_hook=None)
Read and deserialize from a file path or file object.
| Parameter | Type | Description |
|---|---|---|
file | str | IO[bytes] | File path or readable binary file object |
object_hook | Callable[[dict], Any] | None | Transform decoded dictionaries |
Returns: Any — Deserialized Python object
Raises: CrousDecodeError, FileNotFoundError
Stream Functions
crous.dump_to_stream(obj, stream, default=None)
Serialize and write to a writable stream.
| Parameter | Type | Description |
|---|---|---|
obj | Any | Object to serialize |
stream | IO[bytes] | Any object with a write() method |
default | Callable | None | Fallback handler |
crous.load_from_stream(stream, object_hook=None)
Read and deserialize from a readable stream.
| Parameter | Type | Description |
|---|---|---|
stream | IO[bytes] | Any object with a read() method |
object_hook | Callable | None | Transform decoded dictionaries |
CROUT Functions
crous.to_crout(obj)
Serialize a Python object to CROUT text format.
| Parameter | Type | Description |
|---|---|---|
obj | Any | Object to serialize |
Returns: str — CROUT text representation
crous.from_crout(text)
Deserialize CROUT text to a Python object.
| Parameter | Type | Description |
|---|---|---|
text | str | CROUT text data |
Returns: Any — Deserialized Python object
crous.crout_to_flux(text)
Convert CROUT text directly to FLUX binary (no Python intermediary).
| Parameter | Type | Description |
|---|---|---|
text | str | CROUT text data |
Returns: bytes — FLUX binary data
crous.flux_to_crout(data)
Convert FLUX binary directly to CROUT text (no Python intermediary).
| Parameter | Type | Description |
|---|---|---|
data | bytes | FLUX binary data |
Returns: str — CROUT text representation
Custom Serializer Registry
crous.register_serializer(type, serializer, tag=None)
Register a custom serializer for a Python type.
| Parameter | Type | Description |
|---|---|---|
type | type | The Python class to register |
serializer | Callable[[Any], Any] | Function that converts instance to serializable value |
tag | int | None | Optional explicit tag number (auto-assigned from 100 if omitted) |
crous.unregister_serializer(type)
Remove a previously registered serializer.
| Parameter | Type | Description |
|---|---|---|
type | type | The Python class to unregister |
crous.register_decoder(tag, decoder)
Register a custom decoder for a tagged value.
| Parameter | Type | Description |
|---|---|---|
tag | int | The tag number to decode |
decoder | Callable[[Any], Any] | Function that reconstructs the custom type |
crous.unregister_decoder(tag)
Remove a previously registered decoder.
| Parameter | Type | Description |
|---|---|---|
tag | int | The tag number to unregister |
Classes
crous.CrousEncoder
Reusable encoder with persistent options.
class CrousEncoder:
def __init__(self, default=None, allow_custom=True):
"""
Args:
default: Fallback handler for unsupported types
allow_custom: Whether to use registered custom serializers
"""
def encode(self, obj) -> bytes:
"""Serialize obj to FLUX binary bytes."""crous.CrousDecoder
Reusable decoder with persistent options.
class CrousDecoder:
def __init__(self, object_hook=None):
"""
Args:
object_hook: Transform function for decoded dictionaries
"""
def decode(self, data: bytes) -> Any:
"""Deserialize FLUX binary bytes to Python object."""Exceptions
crous.CrousError
Base exception for all Crous errors. Inherits from Exception.
crous.CrousEncodeError
Raised during serialization. Inherits from CrousError.
crous.CrousDecodeError
Raised during deserialization. Inherits from CrousError.
Module Attributes
| Attribute | Type | Value | Description |
|---|---|---|---|
crous.__version__ | str | "2.0.0" | Library version string |
crous.__version_tuple__ | tuple | (2, 0, 0) | Version as tuple |
crous.version Module
The crous.version submodule provides detailed version and compatibility utilities.
Constants
| Constant | Value | Description |
|---|---|---|
VERSION_MAJOR | 2 | Major version |
VERSION_MINOR | 0 | Minor version |
VERSION_PATCH | 0 | Patch version |
VERSION_STRING | "2.0.0" | Version string |
VERSION_HEX | 0x020000 | Version as hex |
WIRE_VERSION_CURRENT | 2 | Current wire format version |
WIRE_VERSION_MIN_READ | 1 | Minimum readable wire version |
WIRE_VERSION_MAX_READ | 2 | Maximum readable wire version |
Feature Flags (IntFlag)
| Flag | Bit | Description |
|---|---|---|
Feature.NONE | 0 | No features |
Feature.TAGGED | 1 | Tagged values |
Feature.TUPLE | 2 | Native tuple support |
Feature.SET | 4 | Set type |
Feature.FROZENSET | 8 | Frozenset type |
Feature.COMPRESSION | 16 | Compression |
Feature.STREAMING | 32 | Streaming mode |
Feature.SCHEMA | 64 | Schema validation |
Feature.ENCRYPTION | 128 | Encryption |
Feature.DATETIME | 256 | Datetime support |
Feature.DECIMAL | 512 | Decimal support |
Feature.UUID | 1024 | UUID support |
Feature.PATH | 2048 | Path support |
Feature.COMMENTS | 4096 | Comments |
Feature.METADATA | 8192 | Metadata |
Feature.CHECKSUM | 16384 | Checksums |
SemanticVersion
from crous.version import SemanticVersion
# Create from string
v = SemanticVersion.parse("2.0.0")
# Get current library version
v = SemanticVersion.current()
# Comparison operators
v1 = SemanticVersion.parse("1.0.0")
v2 = SemanticVersion.parse("2.0.0")
assert v1 < v2
# Constraint checking (supports >=, >, <=, <, ==, !=, ^, ~)
assert v2.satisfies(">=1.0.0")
assert v2.satisfies("^2.0.0")
assert v2.satisfies("~2.0")
# Bump versions
v3 = v2.bump_minor() # 2.1.0
v4 = v2.bump_major() # 3.0.0Header
from crous.version import Header
header = Header.parse(binary_data)
header.magic # b'FLUX'
header.wire_version # 1
header.flags # 0
header.is_valid # True if magic == b'FLUX'check_compatibility(data)
from crous.version import check_compatibility
result = check_compatibility(binary_data)
result.status # Compatibility enum value
result.is_compatible # True/False
result.is_warning # True if warnings
result.is_error # True if incompatible
result.message # Human-readable descriptionSerializable Types
# Type alias for all natively serializable types
CrousSerializable = Union[
None,
bool,
int,
float,
str,
bytes,
List['CrousSerializable'],
Tuple['CrousSerializable', ...],
Dict[str, 'CrousSerializable'],
Set['CrousSerializable'],
FrozenSet['CrousSerializable'],
]