Package xtelligent_serial

This module uses decorators to accomplish serialization and deserialization of types. There are three decorators associated with the API. One assigns a function to serialize a specific type, another does the same for deserialization, and a final decorator allows a type (class) to serialize itself.

In the example links, please expand the code in the example documentation. It is best to expand the code at the top of the page to see all the decorators in use.

Expand source code
'''
This module uses decorators to accomplish serialization and deserialization
of types. There are
three decorators associated with the API. One assigns a function to serialize
a specific type, another does the same for deserialization, and a final
decorator allows a type (class) to serialize itself.

In the example links, please expand the code in the example documentation. It is best to
expand the code at the top of the page to see all the decorators in use.
'''
from .data_class import descriptor as DataClassDescriptor
from .registry import (register_deserializer, register_serializer,
                       serialize, deserialize)
from .decorator import deserializer, serialization, serializer
from .sequence_of import SequenceOf
from .signatures import JSONSerializable
try:
    from .version import MAJOR_VERSION, MINOR_VERSION, BUILDNUMBER
except: # pylint: disable=bare-except
    MAJOR_VERSION = 0
    MINOR_VERSION = 0
    BUILDNUMBER = 0
version = f'{MAJOR_VERSION}.{MINOR_VERSION}.{BUILDNUMBER}'

__all__ = [
    'deserializer',
    'serializer',
    'serialization',
    'JSONSerializable',
    'version',
    'serialize',
    'deserialize',
    'SequenceOf',
]

Sub-modules

xtelligent_serial.data_class
xtelligent_serial.data_structs
xtelligent_serial.decorator
xtelligent_serial.json
xtelligent_serial.registry
xtelligent_serial.sequence_of
xtelligent_serial.signatures
xtelligent_serial.version

Functions

def deserialize(t: Type, raw_data: Union[int, float, bool, str, collections.abc.Sequence, collections.abc.Iterable, collections.abc.Mapping]) ‑> Any

Creates an instance of t from raw data (dict, list, str, int, etc.). Uses a modified single dispatch pattern to find deserializers.

Expand source code
def deserialize(t: Type, raw_data: JSONSerializable) -> Any:
    '''Creates an instance of `t` from raw data (dict, list, str, int, etc.).
    Uses a modified single dispatch pattern to find deserializers.
    '''
    if not t:
        raise ValueError('Expected t to be a type')
    func = fjd.dispatch(type(raw_data) if t == object else t)
    if not func:
        raise ValueError(f'No deserialization handler for type {t}')
    if isinstance(raw_data, Mapping): # Assume dict:object, never list!
        return func({**raw_data, TYPEKEY: t}, type=t)
    return func(raw_data, type=t)
def deserializer(type: Type)

Decorator that registers a function as a deserializer for a type. The decorated function receives a deserialize() method that takes a single argument - the data to deserialize to type.

See:examples.simple.

Expand source code
def deserializer(type: Type):
    '''Decorator that registers a function as a deserializer for a type.
    The decorated function receives a `deserialize` method that takes a single
    argument - the data to deserialize to `type`.

    See:`examples.simple`.
    '''
    def wrapper(func: Deserializer):
        update_wrapper(wrapper, func)
        register_deserializer(type, func)
        def ds(data_dict) -> type:
            return deserialize(type, data_dict)
        setattr(func, 'deserialize', ds)
        return func
    return wrapper
def serialization(Cls)

Marks a class for serialization.

See:examples.simple

Expand source code
def serialization(Cls):
    '''Marks a class for serialization.

    See:`examples.simple`
    '''
    # pylint: disable=unnecessary-lambda
    register_serializer(Cls, lambda raw_data: Cls.__serializer__(raw_data))
    register_deserializer(Cls, lambda raw_data: Cls.__deserializer__(raw_data))
    return Cls
def serialize(target) ‑> Union[int, float, bool, str, collections.abc.Sequence, collections.abc.Iterable, collections.abc.Mapping]

Serializes an instance of t into raw data (dict, list, str, int, etc.). Uses the single dispatch pattern to find implementations of the function for return None if target is None else serialize(props(target)) specific types.

Expand source code
@singledispatch
def serialize(target) -> JSONSerializable:
    '''Serializes an instance of `t` into raw data (dict, list, str, int, etc.).
    Uses the single dispatch pattern to find implementations of the function for
    return None if target is None else serialize(props(target))
    specific types.'''
    return None if target is None else serialize(props(target))
def serializer(type: Type)

Decorator that registers a function as a serializer for a type.

See:examples.simple.

Expand source code
def serializer(type: Type):
    '''Decorator that registers a function as a serializer for a type.

    See:`examples.simple`.
    '''
    def wrapper(func: Serializer):
        update_wrapper(wrapper, func)
        register_serializer(type, func)
        return func
    return wrapper