Skip to content

Commit

Permalink
Merge pull request #14 from PyMoDAQ/feature/serializer
Browse files Browse the repository at this point in the history
Feature/serializer
  • Loading branch information
seb5g authored Nov 25, 2024
2 parents 1864d53 + d86c5f6 commit 6a82c49
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ venv.bak/
*yacctab.py
*lextab.py

.mu_repo
9 changes: 8 additions & 1 deletion docs/src/user_folder/tcpip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ They both implements specific methods applicable to a given object but also a ge

.. code-block::
>>> from pymodaq.utils.tcp_ip.serializer import Serializer, DeSerializer
>>> from pymodaq.utils.tcp_ip.serializer import DeSerializer
from pymodaq_data.serialize.serializer_legacy import Serializer
>>> string = 'Hello'
>>> ser = Serializer(string)
>>> print(ser.string_serialization(string))
b'\x00\x00\x00\x05Hello'
In this example, the serializer first send 4 bytes encoding the length of the
>>> string = 'Hello'
>>> ser = Serializer(string)
>>> print(ser.string_serialization(string))
Expand Down
46 changes: 40 additions & 6 deletions src/pymodaq_gui/parameter/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from __future__ import annotations
from typing import TYPE_CHECKING, List
from typing import TYPE_CHECKING, List, Tuple, Any
from dataclasses import Field, fields
import numpy as np
from collections import OrderedDict
from dataclasses import dataclass
from pymodaq_utils.utils import find_keys_from_val
from pymodaq_data.serialize.factory import SerializableFactory, SerializableBase
from pymodaq_gui.parameter import ioxml

if TYPE_CHECKING:
from pymodaq_gui.parameter import Parameter
from pymodaq_gui.parameter import Parameter

ser_factory = SerializableFactory()

class ParameterWithPath:

@SerializableFactory.register_decorator()
class ParameterWithPath(SerializableBase):
""" holds together a Parameter object and its full path
To be used when communicating between TCPIP to reconstruct properly the Parameter
Expand All @@ -22,6 +26,7 @@ class ParameterWithPath:
path: full path of the parameter, if None it is constructed from the parameter parents
"""
def __init__(self, parameter: Parameter, path: List[str] = None):
super().__init__()
self._parameter = parameter
if path is None:
path = get_param_path(parameter)
Expand All @@ -35,6 +40,33 @@ def parameter(self) -> Parameter:
def path(self) -> List[str]:
return self._path

@staticmethod
def serialize(param: 'ParameterWithPath') -> bytes:
"""
"""
bytes_string = b''
path = param.path
param_as_xml = ioxml.parameter_to_xml_string(param.parameter)
bytes_string += ser_factory.get_apply_serializer(path)
bytes_string += ser_factory.get_apply_serializer(param_as_xml)
return bytes_string

@classmethod
def deserialize(cls, bytes_str: bytes) -> Tuple[Any, bytes]:
"""Convert bytes into a ParameterWithPath object
Returns
-------
ParameterWithPath: the decoded object
bytes: the remaining bytes string if any
"""
path, remaining_bytes = ser_factory.get_apply_deserializer(bytes_str)
param_as_xml, remaining_bytes = ser_factory.get_apply_deserializer(remaining_bytes)
param_dict = ioxml.XML_string_to_parameter(param_as_xml)
param_obj = Parameter(**param_dict[0])
return ParameterWithPath(param_obj, path), remaining_bytes


def get_widget_from_tree(parameter_tree, widget_instance):
widgets = []
Expand Down Expand Up @@ -104,8 +136,10 @@ def getValues(param:Parameter,) -> OrderedDict:
"""
return param.getValues()

def compareParameters(param1:Parameter,param2:Parameter,opts:list=[])-> bool:
"""Compare the structure and the opts of two parameters with their children, return True if structure and all opts are identical

def compareParameters(param1:Parameter, param2:Parameter, opts: list = [])-> bool:
"""Compare the structure and the opts of two parameters with their children,
return True if structure and all opts are identical
Parameters
----------
param1: Parameter
Expand Down
15 changes: 11 additions & 4 deletions tests/parameter_test/param_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ def test_compareValuesParameter():
putils.compareValuesParameter(param1=P1,param2=P3) == False,
putils.compareValuesParameter(param1=P1,param2=P4) == False]






class TestScroll:
def test_scroll_log(self):
Expand Down Expand Up @@ -235,3 +231,14 @@ def test_set_param_from_param(qtbot):
assert settings_old.child('main_settings', 'axis').value() == 2
assert dict_widget.currentText() == 'DAQ2D'


def test_ParameterWithPath_serialize():

p1_with_path = putils.ParameterWithPath(P1.child('numbers', 'afloat', 'aint'))
assert isinstance(putils.ser_factory.get_apply_serializer(p1_with_path), bytes)

param_back: putils.ParameterWithPath = putils.ser_factory.get_apply_deserializer(
putils.ser_factory.get_apply_serializer(p1_with_path))[0]
assert param_back.path == p1_with_path.path
assert putils.compareParameters(param_back.parameter, p1_with_path.parameter)

0 comments on commit 6a82c49

Please sign in to comment.