def test_dataclass_serialization_class( val_a, exp_val_a_tree, exp_val_a_dc, to_yaml_tree_mod, from_yaml_tree_mod, sort_string_lists, ): """Test the `dataclass_serialization_class` function. The test defines a dataclass and its corresponding serialization class using `dataclass_serialization_class`. It first calls ``to_yaml_tree`` to get tree from the generated serialization class. Afterwards the tree is used with the ``from_yaml_tree`` method to construct a new dataclass instance. The results of the function calls are checked against the expected values. Parameters ---------- val_a : Initial value of the dataclasses' variable a exp_val_a_tree : Expected value of the variable a in the tree after `to_yaml_tree` was run exp_val_a_dc : Expected value of the variable a of the reconstructed dataclass to_yaml_tree_mod : The value passed as corresponding function parameter from_yaml_tree_mod : The value passed as corresponding function parameter sort_string_lists The value passed as corresponding function parameter """ @dataclass class _DataClass: a: list[str] b: int = 1 dc = _DataClass(a=val_a, b=2) dataclass_asdf = dataclass_serialization_class( class_type=_DataClass, class_name="Test", version="0.1.0", sort_string_lists=sort_string_lists, to_yaml_tree_mod=to_yaml_tree_mod, from_yaml_tree_mod=from_yaml_tree_mod, ) tree = dataclass_asdf().to_yaml_tree(dc, None, None) assert tree["b"] == 2 assert tree["a"] == exp_val_a_tree dc_restored = dataclass_asdf().from_yaml_tree(tree, None, None) assert dc_restored.b == 2 assert dc_restored.a == exp_val_a_dc
from dataclasses import dataclass from weldx.asdf.util import dataclass_serialization_class __all__ = ["Workpiece", "WorkpieceConverter"] @dataclass class Workpiece: """<CLASS DOCSTRING>""" geometry: str WorkpieceConverter = dataclass_serialization_class( class_type=Workpiece, class_name="aws/design/workpiece", version="0.1.0")
from weldx.asdf.util import dataclass_serialization_class from weldx.measurement import Error __all__ = ["Error", "ErrorConverter"] ErrorConverter = dataclass_serialization_class( class_type=Error, class_name="measurement/error", version="0.1.0" )
from __future__ import annotations from dataclasses import dataclass from weldx.asdf.util import dataclass_serialization_class from .connection import Connection from .workpiece import Workpiece __all__ = ["SubAssembly", "SubAssemblyConverter"] @dataclass class SubAssembly: """<CLASS DOCSTRING>""" workpiece: list[Workpiece] connection: Connection SubAssemblyConverter = dataclass_serialization_class( class_type=SubAssembly, class_name="aws/design/sub_assembly", version="0.1.0", )
from weldx.asdf.util import dataclass_serialization_class from weldx.measurement import Signal __all__ = ["Signal", "SignalConverter"] def _from_yaml_tree_mod(tree: dict): if "data" not in tree: tree["data"] = None return tree SignalConverter = dataclass_serialization_class( class_type=Signal, class_name="measurement/signal", version="0.1.0", from_yaml_tree_mod=_from_yaml_tree_mod, )
from weldx.asdf.util import dataclass_serialization_class from weldx.measurement import MeasurementEquipment __all__ = ["MeasurementEquipment", "MeasurementEquipmentConverter"] def _from_yaml_tree_mod(tree): if "sources" not in tree: tree["sources"] = None return tree MeasurementEquipmentConverter = dataclass_serialization_class( class_type=MeasurementEquipment, class_name="equipment/measurement_equipment", version="0.1.0", from_yaml_tree_mod=_from_yaml_tree_mod, )
from dataclasses import dataclass from weldx.asdf.util import dataclass_serialization_class from .joint_penetration import JointPenetration from .weld_details import WeldDetails __all__ = ["Connection", "ConnectionConverter"] @dataclass class Connection: """<CLASS DOCSTRING>""" joint_type: str weld_type: str joint_penetration: JointPenetration weld_details: WeldDetails ConnectionConverter = dataclass_serialization_class( class_type=Connection, class_name="aws/design/connection", version="0.1.0", )
from dataclasses import dataclass import pint from weldx.asdf.util import dataclass_serialization_class __all__ = ["JointPenetration", "JointPenetrationConverter"] @dataclass class JointPenetration: """<CLASS DOCSTRING>""" complete_or_partial: str root_penetration: pint.Quantity groove_weld_size: float = None incomplete_joint_penetration: float = None weld_size: float = None weld_size_E1: float = None weld_size_E2: float = None depth_of_fusion: float = None JointPenetrationConverter = dataclass_serialization_class( class_type=JointPenetration, class_name="aws/design/joint_penetration", version="0.1.0", )
from dataclasses import dataclass, field import numpy as np import pint from weldx.asdf.util import dataclass_serialization_class from weldx.constants import Q_ __all__ = ["UnitValidatorTestClass", "UnitValidatorTestClassConverter"] @dataclass class UnitValidatorTestClass: """Testclass for validating pint.Quantities with wx_unit.""" length_prop: pint.Quantity = Q_(1, "m") velocity_prop: pint.Quantity = Q_(2, "km / s") current_prop: pint.Quantity = Q_(np.eye(2, 2), "mA") nested_prop: dict = field(default_factory=lambda: dict( q1=Q_(np.eye(3, 3), "m"), q2=Q_(2, "m^3"))) simple_prop: dict = field( default_factory=lambda: dict(value=float(3), units="m")) delta_prop: dict = Q_(100, "Δ°C") UnitValidatorTestClassConverter = dataclass_serialization_class( class_type=UnitValidatorTestClass, class_name="debug/test_unit_validator", version="0.1.0", )
from weldx.constants import Q_ # Dimension ---------------------------------------------------------------------------- @dataclass class Dimension: """ Stores data of a dimension. """ name: str length: int DimensionConverter = dataclass_serialization_class(class_type=Dimension, class_name="core/dimension", version="0.1.0") # Variable ----------------------------------------------------------------------------- @dataclass class Variable: """Represents an n-dimensional piece of data.""" name: str dimensions: list data: np.ndarray attrs: Mapping[Hashable, Any] = dataclasses.field(default_factory=dict) class VariableConverter(WeldxConverter):
from dataclasses import dataclass import pint from weldx.asdf.util import dataclass_serialization_class __all__ = ["GasComponent", "GasComponentConverter"] @dataclass class GasComponent: """<CLASS DOCSTRING>""" gas_chemical_name: str gas_percentage: pint.Quantity GasComponentConverter = dataclass_serialization_class( class_type=GasComponent, class_name="aws/process/gas_component", version="0.1.0")
from dataclasses import dataclass import pandas as pd import pint from weldx.asdf.util import dataclass_serialization_class from weldx.constants import Q_ __all__ = ["PropertyTagTestClass", "PropertyTagTestClassConverter"] @dataclass class PropertyTagTestClass: """Helper class to test the shape validator""" prop1: pd.Timestamp = pd.Timestamp("2020-01-01") prop2: pd.Timestamp = pd.Timestamp("2020-01-02") prop3: pint.Quantity = Q_("3m") PropertyTagTestClassConverter = dataclass_serialization_class( class_type=PropertyTagTestClass, class_name="debug/test_property_tag", version="0.1.0", )
from weldx.asdf.types import WeldxConverter from weldx.asdf.util import dataclass_serialization_class from weldx.core import GenericSeries from weldx.util.xarray import _get_coordinate_quantities @dataclass class GenericSeriesFreeDimension: units: pint.Quantity symbol: "str" = None GenericSeriesFreeDimensionConverter = dataclass_serialization_class( class_type=GenericSeriesFreeDimension, class_name="core/generic_series_free_dimension", version="0.1.0", ) class GenericSeriesConverter(WeldxConverter): """Serialization class for weldx.core.GenericSeries""" name = "core/generic_series" version = "0.1.0" types = [GenericSeries] def to_yaml_tree(self, obj: GenericSeries, tag: str, ctx) -> dict: """Convert to python dict.""" if obj.is_expression: dims = {}
from weldx.asdf.util import dataclass_serialization_class from weldx.measurement import Measurement __all__ = ["Measurement", "MeasurementConverter"] MeasurementConverter = dataclass_serialization_class( class_type=Measurement, class_name="measurement/measurement", version="0.1.0")
from __future__ import annotations from dataclasses import dataclass from weldx.asdf.util import dataclass_serialization_class from .sub_assembly import SubAssembly __all__ = ["Weldment", "WeldmentConverter"] @dataclass class Weldment: """<CLASS DOCSTRING>""" sub_assembly: list[SubAssembly] WeldmentConverter = dataclass_serialization_class( class_type=Weldment, class_name="aws/design/weldment", version="0.1.0")
from weldx.asdf.util import dataclass_serialization_class from weldx.measurement import SignalSource __all__ = ["SignalSource", "SignalSourceConverter"] SignalSourceConverter = dataclass_serialization_class( class_type=SignalSource, class_name="measurement/source", version="0.1.0")
from weldx.asdf.util import dataclass_serialization_class from weldx.measurement import SignalTransformation __all__ = ["SignalTransformation", "SignalTransformationConverter"] SignalTransformationConverter = dataclass_serialization_class( class_type=SignalTransformation, class_name="measurement/signal_transformation", version="0.1.0", )
from dataclasses import dataclass import pint from weldx.asdf.util import dataclass_serialization_class __all__ = ["WeldDetails", "WeldDetailsConverter"] @dataclass class WeldDetails: """<CLASS DOCSTRING>""" joint_design: str weld_sizes: pint.Quantity number_of_passes: int WeldDetailsConverter = dataclass_serialization_class( class_type=WeldDetails, class_name="aws/design/weld_details", version="0.1.0" )
from __future__ import annotations from dataclasses import dataclass from weldx.asdf.util import dataclass_serialization_class from .gas_component import GasComponent __all__ = ["ShieldingGasType", "ShieldingGasTypeConverter"] @dataclass class ShieldingGasType: """<CLASS DOCSTRING>""" gas_component: list[GasComponent] common_name: str designation: str = None ShieldingGasTypeConverter = dataclass_serialization_class( class_type=ShieldingGasType, class_name="aws/process/shielding_gas_type", version="0.1.0", )
import pint from weldx.asdf.util import dataclass_serialization_class from .shielding_gas_type import ShieldingGasType __all__ = ["ShieldingGasForProcedure", "ShieldingGasForProcedureConverter"] @dataclass class ShieldingGasForProcedure: """<CLASS DOCSTRING>""" use_torch_shielding_gas: bool torch_shielding_gas: ShieldingGasType torch_shielding_gas_flowrate: pint.Quantity use_backing_gas: bool = None backing_gas: ShieldingGasType = None backing_gas_flowrate: pint.Quantity = None use_trailing_gas: bool = None trailing_shielding_gas: ShieldingGasType = None trailing_shielding_gas_flowrate: pint.Quantity = None ShieldingGasForProcedureConverter = dataclass_serialization_class( class_type=ShieldingGasForProcedure, class_name="aws/process/shielding_gas_for_procedure", version="0.1.0", )