yggdrasil.serialize package¶
Submodules¶
yggdrasil.serialize.AsciiMapSerialize module¶
- class yggdrasil.serialize.AsciiMapSerialize.AsciiMapSerialize(*args, **kwargs)[source]¶
Bases:
SerializeBase
Class for serializing/deserializing name/value mapping.
- Parameters:
delimiter (str, optional) – Delimiter that should be used to separate name/value pairs in the map. Defaults to t.
- classmethod concatenate(objects, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- concats_as_str = False¶
- default_datatype = {'type': 'object'}¶
yggdrasil.serialize.AsciiTableSerialize module¶
- class yggdrasil.serialize.AsciiTableSerialize.AsciiTableSerialize(*args, **kwargs)[source]¶
Bases:
DefaultSerialize
Class for serialize table output into bytes messages comprising a formatted ASCII table.
- Parameters:
format_str (str, optional) – If provided, this string will be used to format messages from a list of arguments and parse messages to get a list of arguments in C printf/scanf style. Defaults to None and messages are assumed to already be bytes.
field_names (list, optional) – The names of fields in the format string. If not provided, names are set based on the order of the fields in the format string.
field_units (list, optional) – The units of fields in the format string. If not provided, all fields are assumed to be dimensionless.
as_array (bool, optional) – If True, each of the arguments being serialized/deserialized will be arrays that are converted to/from bytes in column major (‘F’) order. Otherwise, each argument should be a scalar. Defaults to False.
delimiter (str, optional) – Character(s) that should be used to separate columns. Defaults to ‘t’.
use_astropy (bool, optional) – If True, the astropy package will be used to serialize/deserialize table. Defaults to False.
**kwargs – Additional keyword args are processed as part of the type definition.
- format_str¶
Format string used to format/parse bytes messages from/to a list of arguments in C printf/scanf style.
- Type:
- as_array¶
True or False depending if serialized/deserialized python objects will be arrays or scalars.
- Type:
- default_datatype = {'type': 'array'}¶
- default_read_meth = 'readline'¶
- deserialize_file_header(fd)[source]¶
Deserialize the header information from the file and update the serializer.
- Parameters:
fd (file) – File containing header.
- file_extensions = ['.txt']¶
- func_deserialize(msg)[source]¶
Deserialize a message.
- Parameters:
msg – Message to be deserialized.
- Returns:
Deserialized message.
- Return type:
obj
- classmethod get_testing_options(**kwargs)[source]¶
Method to return a dictionary of testing options for this class.
- Parameters:
**kwargs – Keyword arguments are passed to the parent class’s method.
- Returns:
Dictionary of variables to use for testing.
- Return type:
- has_header = True¶
- serialize_file_header()[source]¶
Return the serialized header information that should be prepended to files serialized using this class.
- Returns:
Header string that should be written to the file.
- Return type:
- update_serializer(*args, **kwargs)[source]¶
Update serializer with provided information.
- Parameters:
skip_type (bool, optional) – If True, everything is updated except the data type. Defaults to False.
**kwargs – Additional keyword arguments are processed as part of they type definition and are parsed for old-style keywords.
- Raises:
RuntimeError – If there are keywords that are not valid datatype keywords (currect or old-style).
yggdrasil.serialize.CABOSerialize module¶
- class yggdrasil.serialize.CABOSerialize.CABOSerialize(*args, **kwargs)[source]¶
Bases:
AsciiMapSerialize
Class for serializing/deserializing CABO parameter files.
- classmethod concatenate(objects, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- concats_as_str = True¶
- default_datatype = {'type': 'object'}¶
- file_extensions = ['.cab']¶
yggdrasil.serialize.DefaultSerialize module¶
- class yggdrasil.serialize.DefaultSerialize.DefaultSerialize(*args, **kwargs)[source]¶
Bases:
SerializeBase
Default class for serializing/deserializing a python object into/from a bytes message.
- Parameters:
**kwargs – Additional keyword args are passed to the parent class.
- classmethod concatenate(objects, as_array=False, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- Parameters:
- Returns:
Set of objects that results from concatenating those provided.
- Return type:
- classmethod dict2object(obj, as_array=False, field_names=None, **kwargs)[source]¶
Conver a dictionary to a message object.
- Parameters:
obj (dict) – Dictionary to convert to serializable object.
as_array (bool, optional) – If True, the objects in the list are complete columns in a table and as_format is set to True. Defaults to False.
field_names (list, optional) – The field names associated with a table-like data type. Defaults to None. This keyword must be provided if as_array is True.
**kwargs – Additional keyword arguments are ignored.
- Returns:
Serializable object.
- Return type:
- file_extensions = ['.ygg']¶
- func_deserialize(msg)[source]¶
Deserialize a message.
- Parameters:
msg – Message to be deserialized.
- Returns:
Deserialized message.
- Return type:
obj
- classmethod get_testing_options(**kwargs)[source]¶
Method to return a dictionary of testing options for this class.
- classmethod object2array(obj, as_array=False, field_names=None, **kwargs)[source]¶
Convert a message object into an array.
- Parameters:
obj (object) – Object that would be serialized by this class and should be returned in an array form.
as_array (bool, optional) – If True, the objects in the list are complete columns in a table and as_format is set to True. Defaults to False.
field_names (list, optional) – The field names associated with a table-like data type. Defaults to None. This keyword must be provided if as_array is True.
**kwargs – Additional keyword arguments are ignored.
- Returns:
Array version of the provided object.
- Return type:
np.array
- classmethod object2dict(obj, as_array=False, field_names=None, **kwargs)[source]¶
Convert a message object into a dictionary.
- Parameters:
obj (object) – Object that would be serialized by this class and should be returned in a dictionary form.
as_array (bool, optional) – If True, the objects in the list are complete columns in a table and as_format is set to True. Defaults to False.
field_names (list, optional) – The field names associated with a table-like data type. Defaults to None. This keyword must be provided if as_array is True.
**kwargs – Additional keyword arguments are ignored.
- Returns:
Dictionary version of the provided object.
- Return type:
yggdrasil.serialize.DirectSerialize module¶
- class yggdrasil.serialize.DirectSerialize.DirectSerialize(*args, **kwargs)[source]¶
Bases:
SerializeBase
Class for directly serializing bytes.
- classmethod concatenate(objects, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- default_datatype = {'type': 'bytes'}¶
- func_deserialize(msg)[source]¶
Deserialize a message.
- Parameters:
msg – Message to be deserialized.
- Returns:
Deserialized message.
- Return type:
obj
- classmethod get_testing_options(**kwargs)[source]¶
Method to return a dictionary of testing options for this class.
- Returns:
- Dictionary of variables to use for testing. Key/value pairs:
- kwargs (dict): Keyword arguments for comms tested with the
provided content.
- empty (object): Object produced from deserializing an empty
message.
objects (list): List of objects to be serialized/deserialized. extra_kwargs (dict): Extra keyword arguments not used to
construct type definition.
- typedef (dict): Type definition resulting from the supplied
kwargs.
- dtype (np.dtype): Numpy data types that is consistent with the
determined type definition.
- Return type:
yggdrasil.serialize.FunctionalSerialize module¶
- class yggdrasil.serialize.FunctionalSerialize.FunctionalSerialize(*args, **kwargs)[source]¶
Bases:
SerializeBase
Class for serializing/deserializing a Python object into/from a bytes message using defined functions.
- Parameters:
encoded_datatype (schema, optional) – JSON schema describing the type that serialized objects should conform to. Defaults to the class attribute default_encoded_datatype. If either func_serialize or func_deserialize are not provided, this needs to be specified in order to serialize non-bytes objects.
func_serialize (func, optional) – Callable object that takes Python objects as input and returns a representation that conforms to encoded_datatype. Defaults to None and the default serialization for encoded_datatype will be used.
func_deserialize (func, optional) – Callable object that takes objects of a type that conforms to encoded_datatype and returns a deserialized Python object. Defaults to None and the default deserialization for encoded_datatype will be used.
**kwargs – Additional keyword args are passed to the parent class’s constructor.
- func_deserialize = None¶
- func_serialize = None¶
yggdrasil.serialize.JSONSerialize module¶
- class yggdrasil.serialize.JSONSerialize.JSONSerialize(*args, **kwargs)[source]¶
Bases:
SerializeBase
Class for serializing a python object into a bytes message using JSON.
- Parameters:
- classmethod concatenate(objects, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- concats_as_str = False¶
- default_datatype = {'type': 'object'}¶
- file_extensions = ['.json']¶
- yggdrasil.serialize.JSONSerialize.decode_json(msg, **kwargs)[source]¶
Decode a Python object from a JSON serialization.
- yggdrasil.serialize.JSONSerialize.encode_json(obj, fd=None, indent=None, sort_keys=True, **kwargs)[source]¶
Encode a Python object in JSON format.
- Parameters:
obj (object) – Python object to encode.
fd (file, optional) – File descriptor for file that encoded object should be written to. Defaults to None and string is returned.
indent (int, str, optional) – Indentation for new lines in encoded string. Defaults to None.
sort_keys (bool, optional) – If True, the keys will be output in sorted order. Defaults to True.
**kwargs – Additional keyword arguments are passed to rapidjson.dumps.
- Returns:
Encoded object.
- Return type:
yggdrasil.serialize.MatSerialize module¶
- class yggdrasil.serialize.MatSerialize.MatSerialize(*args, **kwargs)[source]¶
Bases:
SerializeBase
Class for serializing a python object into a bytes message using the Matlab .mat format.
- classmethod concatenate(objects, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- concats_as_str = False¶
- default_datatype = {'type': 'object'}¶
- file_extensions = ['.mat']¶
yggdrasil.serialize.ObjSerialize module¶
- class yggdrasil.serialize.ObjSerialize.ObjDict[source]¶
Bases:
GeometryBase
,ObjWavefront
Enhanced dictionary class for storing Obj information.
- apply_scalar_map(*args, **kwargs)[source]¶
Set the color of faces in a 3D object based on a scalar map. This creates a copy unless no_copy is True.
- Parameters:
scalar_arr (arr) – Scalar values that should be mapped to colors for each face.
color_map (str, optional) – The name of the color map that should be used. Defaults to ‘plasma’.
vmin (float, optional) – Value that should map to the minimum of the colormap. Defaults to min(scalar_arr).
vmax (float, optional) – Value that should map to the maximum of the colormap. Defaults to max(scalar_arr).
scaling (str, optional) – Scaling that should be used to map the scalar array onto the colormap. Defaults to ‘linear’.
scale_by_area (bool, optional) – If True, the elements of the scalar array will be multiplied by the area of the corresponding face. If True, vmin and vmax should be in terms of the scaled array. Defaults to False.
no_copy (bool, optional) – If True, the returned object will not be a copy. Defaults to False.
- Returns:
Obj with updated vertex colors.
- Return type:
- classmethod from_shape(shape, d, conversion=1.0)[source]¶
Create a ply dictionary from a PlantGL shape and descritizer.
- Parameters:
scene (openalea.plantgl.scene) – Scene that should be descritized.
d (openalea.plantgl.descritizer) – Descritizer.
conversion (float, optional) – Conversion factor that should be applied to the vertex positions. Defaults to 1.0.
- class yggdrasil.serialize.ObjSerialize.ObjSerialize(*args, **kwargs)[source]¶
Bases:
PlySerialize
Class for serializing/deserializing .obj file formats. Reader adapted from https://www.pygame.org/wiki/OBJFileLoader.
- default_datatype = {'type': 'obj'}¶
- file_extensions = ['.obj']¶
- func_deserialize(msg)[source]¶
Deserialize a message.
- Parameters:
msg (bytes) – Message to be deserialized.
- Returns:
Deserialized message.
- Return type:
obj
- func_serialize(args)[source]¶
Serialize a message.
- Parameters:
args – List of arguments to be formatted or numpy array to be serialized.
- Returns:
Serialized message.
- Return type:
yggdrasil.serialize.PandasSerialize module¶
- class yggdrasil.serialize.PandasSerialize.PandasSerialize(*args, **kwargs)[source]¶
Bases:
AsciiTableSerialize
Class for serializing/deserializing Pandas data frames.
- Parameters:
- classmethod apply_field_names(frame, field_names=None)[source]¶
Apply field names as columns to a frame, first checking for a mapping. If there is a direct mapping, the columns are reordered to match the order of the field names. If there is not an overlap in the field names and columns, a one-to-one mapping is assumed, but a warning is issued. If there is a partial overlap, an error is raised.
- Parameters:
frame (pandas.DataFrame) – Frame to apply field names to as columns.
field_names (list, optional) – New field names that should be applied. If not provided, the original frame will be returned unaltered.
- Returns:
Frame with updated field names.
- Return type:
pandas.DataFrame
- Raises:
RuntimeError – If there is a partial overlap between the field names and columns.
- as_array = True¶
- cformat2nptype(*args, **kwargs)[source]¶
Method to convert c format string to numpy data type.
- Parameters:
*args – Arguments are passed to serialize.cformat2nptype.
**kwargs – Keyword arguments are passed to serialize.cformat2nptype.
- Returns:
Corresponding numpy data type.
- Return type:
np.dtype
- classmethod concatenate(objects, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- concats_as_str = False¶
- consolidate_array(out)[source]¶
Consolidate message into a structure numpy array if possible.
- Parameters:
out (list, tuple, np.ndarray) – Object to consolidate into a structured numpy array.
- Returns:
Structured numpy array containing consolidated message.
- Return type:
np.ndarray
- Raises:
ValueError – If the array cannot be consolidated.
- default_read_meth = 'read'¶
- deserialize_file_header(fd)[source]¶
Deserialize the header information from the file and update the serializer.
- Parameters:
fd (file) – File containing header.
- classmethod dict2object(obj, field_names=None, **kwargs)[source]¶
Conver a dictionary to a message object.
- disable_file_header()[source]¶
Set serializer attributes to disable file headers from being included in the serializations.
- property empty_msg¶
Object indicating empty message.
- Type:
obj
- enable_file_header()[source]¶
Set serializer attributes to enable file headers to be included in the serializations.
- get_field_names(*args, **kwargs)[source]¶
Get the field names for an array of fields.
- Parameters:
*args – Arguments are passed to the parent class’s method.
**kwargs – Keyword arguments are passed to the parent class’s method.
- Returns:
Names for each field in the data type.
- Return type:
- classmethod get_testing_options(not_as_frames=False, no_names=False, no_header=False, **kwargs)[source]¶
Method to return a dictionary of testing options for this class.
- Parameters:
not_as_frames (bool, optional) – If True, the returned example includes data that is not in a pandas data frame. Defaults to False.
no_names (bool, optional) – If True, an example is returned where the names are not provided to the deserializer. Defaults to False.
no_header (bool, optional) – If True, an example is returned
False. (where a header is not included. Defaults to) –
- Returns:
Dictionary of variables to use for testing.
- Return type:
- initialize_from_message(msg, **kwargs)[source]¶
Initialize the serializer based on recieved message.
- Parameters:
msg (object) – Message that serializer should be initialized from.
**kwargs – Additional keyword arguments are treated as metadata that may contain additional information for initializing the serializer.
- classmethod normalize_bytes2unicode(args)[source]¶
Convert columns that are bytes to unicode.
- Parameters:
args (pd.DataFrame) – Pandas dataframe to convert columns for.
- Returns:
Version of args without bytes in columns.
- Return type:
pd.DataFrame
- classmethod normalize_unicode2bytes(args)[source]¶
Convert columns that are unicode to bytes.
- Parameters:
args (pd.DataFrame) – Pandas dataframe to convert columns for.
- Returns:
Version of args without unicode in columns.
- Return type:
pd.DataFrame
- classmethod object2array(obj, **kwargs)[source]¶
Convert a message object into an array.
- Parameters:
obj (object) – Object that would be serialized by this class and should be returned in an array form.
**kwargs – Additional keyword arguments are ignored.
- Returns:
Array version of the provided object.
- Return type:
np.array
- property send_converter¶
yggdrasil.serialize.PickleSerialize module¶
- class yggdrasil.serialize.PickleSerialize.PickleSerialize(*args, **kwargs)[source]¶
Bases:
DefaultSerialize
Class for serializing a python object into a bytes message by pickling.
- classmethod concatenate(objects, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- file_extensions = ['.pkl']¶
- classmethod get_first_frame(msg)[source]¶
Extract one frame from the provided message that may contain one or more frames.
- classmethod get_testing_options(**kwargs)[source]¶
Method to return a dictionary of testing options for this class.
- Returns:
Dictionary of variables to use for testing.
- Return type:
- is_framed = True¶
yggdrasil.serialize.PlySerialize module¶
- class yggdrasil.serialize.PlySerialize.GeometryBase[source]¶
Bases:
object
Base class for extening rapidjson geometry classes.
- apply_scalar_map(scalar_arr, color_map=None, vmin=None, vmax=None, scaling='linear', scale_by_area=False, no_copy=False, _as_obj=False)[source]¶
Set the color of faces in a 3D object based on a scalar map. This creates a copy unless no_copy is True.
- Parameters:
scalar_arr (arr) – Scalar values that should be mapped to colors for each face.
color_map (str, optional) – The name of the color map that should be used. Defaults to ‘plasma’.
vmin (float, optional) – Value that should map to the minimum of the colormap. Defaults to min(scalar_arr).
vmax (float, optional) – Value that should map to the maximum of the colormap. Defaults to max(scalar_arr).
scaling (str, optional) – Scaling that should be used to map the scalar array onto the colormap. Defaults to ‘linear’.
scale_by_area (bool, optional) – If True, the elements of the scalar array will be multiplied by the area of the corresponding face. If True, vmin and vmax should be in terms of the scaled array. Defaults to False.
no_copy (bool, optional) – If True, the returned object will not be a copy. Defaults to False.
- Returns:
Geometry with updated vertex colors.
- Return type:
- classmethod from_scene(scene, d=None, conversion=1.0)[source]¶
Create a geometry dictionary from a PlantGL scene and descritizer.
- Parameters:
scene (openalea.plantgl.scene) – Scene that should be descritized.
d (openalea.plantgl.descritizer, optional) – Descritizer. Defaults to openalea.plantgl.all.Tesselator.
conversion (float, optional) – Conversion factor that should be applied to the vertex positions. Defaults to 1.0.
- classmethod from_shape(shape, d, conversion=1.0, _as_obj=False)[source]¶
Create a geometry dictionary from a PlantGL shape and descritizer.
- Parameters:
scene (openalea.plantgl.scene) – Scene that should be descritized.
d (openalea.plantgl.descritizer) – Descritizer.
conversion (float, optional) – Conversion factor that should be applied to the vertex positions. Defaults to 1.0.
- to_geom_args(conversion=1.0, name=None, _as_obj=False)[source]¶
Get arguments for creating a PlantGL geometry.
- Parameters:
- Returns:
Class, arguments and keyword arguments for PlantGL geometry.
- Return type:
- class yggdrasil.serialize.PlySerialize.PlyDict[source]¶
Bases:
GeometryBase
,Ply
Enhanced dictionary class for storing Ply information.
- class yggdrasil.serialize.PlySerialize.PlySerialize(*args, **kwargs)[source]¶
Bases:
SerializeBase
Class for serializing/deserializing .ply file formats.
- Parameters:
write_header (bool, optional) – If True, headers will be added to serialized output. Defaults to True.
newline (str, optional) – String that should be used for new lines. Defaults to ‘n’.
prune_duplicates (bool, optional) – If True, serialized meshes in array format will be pruned of duplicates when being normalized into a Ply object. If False, duplicates will not be pruned. Defaults to True.
- classmethod concatenate(objects, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- concats_as_str = False¶
- default_datatype = {'type': 'ply'}¶
- file_extensions = ['.ply']¶
- func_deserialize(msg)[source]¶
Deserialize a message.
- Parameters:
msg (bytes) – Message to be deserialized.
- Returns:
Deserialized message.
- Return type:
obj
- func_serialize(args)[source]¶
Serialize a message.
- Parameters:
args – List of arguments to be formatted or numpy array to be serialized.
- Returns:
Serialized message.
- Return type:
- classmethod get_testing_options(**kwargs)[source]¶
Method to return a dictionary of testing options for this class.
- Returns:
Dictionary of variables to use for testing.
- Return type:
yggdrasil.serialize.SerializeBase module¶
- class yggdrasil.serialize.SerializeBase.SerializeBase(*args, **kwargs)[source]¶
Bases:
YggClass
Base class for serializing/deserializing a Python object into/from a bytes message.
- Parameters:
newline (str, optional) – One or more characters indicating a newline. Defaults to ‘n’.
comment (str, optional) – One or more characters indicating a comment. Defaults to ‘# ‘.
datatype (schema, optional) – JSON schema defining the type of object that the serializer will be used to serialize/deserialize. Defaults to default_datatype.
**kwargs – Additional keyword args are processed as part of the type definition.
- initialized¶
True if the serializer has been initialized either by input arguments specifying the type or by infering the type from a processed message.
- Type:
- Class Attributes:
- has_header (bool): True if the serialization has a header when written
to a file.
- default_read_meth (str): Default method that data should be read from
a file for deserialization.
- is_framed (bool): True if the serialization has a frame allowing
multiple serialized objects to be recovered from a single message.
- concats_as_str (bool): True if serialized objects can be concatenated
directly as strings.
- encoded_datatype (schema): JSON schema defining the type of object
produced by the class’s func_serialize method. For most classes this will be {‘type’: ‘scalar’, ‘subtype’: ‘string’}, indicating that the method will produce bytes suitable for serialization.
- static before_registration(cls)[source]¶
Operations that should be performed to modify class attributes prior to registration.
- cformat2nptype(*args, **kwargs)[source]¶
Method to convert c format string to numpy data type.
- Parameters:
*args – Arguments are passed to serialize.cformat2nptype.
**kwargs – Keyword arguments are passed to serialize.cformat2nptype.
- Returns:
Corresponding numpy data type.
- Return type:
np.dtype
- classmethod concatenate(objects, **kwargs)[source]¶
Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.
- concats_as_str = True¶
- consolidate_array(out)[source]¶
Consolidate message into a structure numpy array if possible.
- Parameters:
out (list, tuple, np.ndarray) – Object to consolidate into a structured numpy array.
- Returns:
Structured numpy array containing consolidated message.
- Return type:
np.ndarray
- Raises:
ValueError – If the array cannot be consolidated.
- decode(msg, no_data=False, metadata=None)[source]¶
Decode message parts into header and body.
- Parameters:
- Returns:
Deserialized message and header information.
- Return type:
- Raises:
ValueError – If msg contains a header, but metadata is also provided as an argument.
TypeError – If msg is not bytes.
- default_datatype = {'subtype': 'string', 'type': 'scalar'}¶
- default_read_meth = 'read'¶
- deserialize_file_header(fd)[source]¶
Deserialize the header information from the file and update the serializer.
- Parameters:
fd (file) – File containing header.
- disable_file_header()[source]¶
Set serializer attributes to disable file headers from being included in the serializations.
- dump(fd, obj, **kwargs)[source]¶
Serialize to a file.
- Parameters:
fd (str, file) – Filename or file-like object to load from.
**kwargs – Additional keyword arguments are passed to the created FileComm used for reading.
- property empty_msg¶
Object indicating empty message.
- Type:
obj
- enable_file_header()[source]¶
Set serializer attributes to enable file headers to be included in the serializations.
- encode(data, metadata, no_metadata=False, max_header_size=0)[source]¶
Encode the message with metadata in a header.
- Parameters:
data (bytes) – Message data serialized into bytes.
metadata (dict) – Metadata that should be included in the message header.
no_metadata (bool, optional) – If True, no metadata will be added to the serialized message. Defaults to False.
max_header_size (int, optional) – Maximum size that header should occupy in order to be sent in a single message. A value of 0 indicates that any size header is valid. Defaults to 0.
- Returns:
Encoded message with header.
- Return type:
- file_extensions = ['.txt']¶
- func_deserialize(msg)[source]¶
Deserialize a message.
- Parameters:
msg – Message to be deserialized.
- Returns:
Deserialized message.
- Return type:
obj
- get_status_message(nindent=0, extra_lines_before=None, extra_lines_after=None)[source]¶
Return lines composing a status message.
- Parameters:
nindent (int, optional) – Number of tabs that should be used to indent each line. Defaults to 0.
extra_lines_before (list, optional) – Additional lines that should be added to the beginning of the default print message. Defaults to empty list if not provided.
extra_lines_after (list, optional) – Additional lines that should be added to the end of the default print message. Defaults to empty list if not provided.
- Returns:
- Lines composing the status message and the
prefix string used for the last message.
- Return type:
- classmethod get_testing_options(table_example=False, array_columns=False, include_oldkws=False, table_string_type='bytes', no_names=False, no_units=False, **kwargs)[source]¶
Method to return a dictionary of testing options for this class.
- Parameters:
table_example (bool, optional) – If True, the returned options will be for an array of elements representing a table-like structure. Defaults to False.
array_columns (bool, optional) – If True, table_example is set to True and the returned options will be for an array data type where each element is an array representing a column Defaults to False.
include_oldkws (bool, optional) – If True, old-style keywords will be added to the returned options. This will only have an effect if table_example is True. Defaults to False.
table_string_type (str, optional) – Type that should be used for the string column in the table. Defaults to ‘bytes’.
no_names (bool, optional) – If True, an example is returned where the names are not provided to the deserializer. Defaults to False.
no_units (bool, optional) – If True, units will not be added to the returned array if table_example is True.
- Returns:
Dictionary of variables to use for testing. Key/value pairs:
kwargs (dict): Keyword arguments for comms tested with the provided content.
empty (object): Object produced from deserializing an empty message.
objects (list): List of objects to be serialized/deserialized. extra_kwargs (dict): Extra keyword arguments not used to construct type definition.
typedef (dict): Type definition resulting from the supplied kwargs.
dtype (np.dtype): Numpy data types that is consistent with the determined type definition.
contents (bytes): Concatenated serialization that will result from deserializing the serialized objects.
contents_recv (list): List of objects that would be deserialized from contents.
- Return type:
- has_header = False¶
- initialize_from_message(msg, serializer=None, **metadata)[source]¶
Initialize the serializer based on recieved message.
- Parameters:
msg (object) – Message that serializer should be initialized from.
**kwargs – Additional keyword arguments are treated as metadata that may contain additional information for initializing the serializer.
- initialize_from_metadata(metadata)[source]¶
Initialize a serializer based on received metadata. This method will exit early if the serializer has already been intialized.
- Parameters:
metadata (dict) – Header information including type info that should be used to initialize the serializer class.
- is_framed = False¶
- property numpy_dtype¶
Corresponding structured data type. Will be None unless the type is an array of 1darrays.
- Type:
np.dtype
- classmethod object2array(obj, **kwargs)[source]¶
Convert a message object into an array.
- Parameters:
obj (object) – Object that would be serialized by this class and should be returned in an array form.
**kwargs – Additional keyword arguments are ignored.
- Returns:
- Array version of the provided object or None if one cannot
be created.
- Return type:
np.array
- serialize(args, metadata=None, add_serializer_info=False, no_metadata=False, max_header_size=0)[source]¶
Serialize a message.
- Parameters:
args (obj) – List of arguments to be formatted or a ready made message.
metadata (dict, optional) – Keyword arguments that should be added to the header. Defaults to None and no header is added.
add_serializer_info (bool, optional) – If True, serializer information will be added to the metadata. Defaults to False.
no_metadata (bool, optional) – If True, no metadata will be added to the serialized message. Defaults to False.
max_header_size (int, optional) – Maximum size that header should occupy in order to be sent in a single message. A value of 0 indicates that any size header is valid. Defaults to 0.
- Returns:
Serialized message.
- Return type:
- Raises:
TypeError – If returned msg is not bytes type (str on Python 2).
- serialize_file_header()[source]¶
Return the serialized header information that should be prepended to files serialized using this class.
- Returns:
Header string that should be written to the file.
- Return type:
- update_serializer(skip_type=False, seritype=None, datatype=None, from_message=False, **kwargs)[source]¶
Update serializer with provided information.
- Parameters:
skip_type (bool, optional) – If True, everything is updated except the data type. Defaults to False.
**kwargs – Additional keyword arguments are processed as part of they type definition and are parsed for old-style keywords.
- Raises:
RuntimeError – If there are keywords that are not valid datatype keywords (currect or old-style).
yggdrasil.serialize.YAMLSerialize module¶
- class yggdrasil.serialize.YAMLSerialize.YAMLSerialize(*args, **kwargs)[source]¶
Bases:
JSONSerialize
Class for serializing a python object into a bytes message using YAML.
- Parameters:
indent (str, int, optional) – String or number of spaces that should be used to indent each level within the seiralized structure. Defaults to ‘t’.
encoding (str, optional) – Encoding that should be used to serialize the object. Defaults to ‘utf-8’.
default_flow_style (bool, optional) – If True, nested collections will be serialized in the block style. If False, they will always be serialized in the flow style. See PyYAML Documentation.
- file_extensions = ['.yaml', '.yml']¶
- yggdrasil.serialize.YAMLSerialize.decode_yaml(msg, sorted_dict_type=None, **kwargs)[source]¶
Decode a Python object from a YAML serialization.
- Parameters:
- Returns:
Deserialized Python object.
- Return type:
- yggdrasil.serialize.YAMLSerialize.encode_yaml(obj, fd=None, indent=None, sorted_dict_type=None, sort_keys=True, **kwargs)[source]¶
Encode a Python object in YAML format.
- Parameters:
obj (object) – Python object to encode.
fd (file, optional) – File descriptor for file that encoded object should be written to. Defaults to None and string is returned.
indent (int, str, optional) – Indentation for new lines in encoded string. Defaults to None.
sort_keys (bool, optional) – If True, dictionaries will be sorted alphabetically by key. Defaults to True.
**kwargs – Additional keyword arguments are passed to yaml.dump.
- Returns:
Encoded object.
- Return type: