def test_linesegment2_to_from_dict():
    """Test the to/from dict of LineSegment2D objects."""
    pt = Point2D(2, 0)
    vec = Vector2D(0, 2)
    seg = LineSegment2D(pt, vec)
    seg_dict = seg.to_dict()
    new_seg = LineSegment2D.from_dict(seg_dict)
    assert isinstance(new_seg, LineSegment2D)
    assert new_seg.to_dict() == seg_dict
Esempio n. 2
0
    def from_dict(cls, data):
        """Initialize an ElectricalConnector from a dictionary.

        Args:
            data: A dictionary representation of an ElectricalConnector object.
        """
        # check the type of dictionary
        assert data['type'] == 'ElectricalConnector', 'Expected ElectricalConnector ' \
            'dictionary. Got {}.'.format(data['type'])
        wires = [Wire.from_dict(wire) for wire in data['wires']]
        geo = LineSegment2D.from_dict(data['geometry']) \
            if data['geometry']['type'] == 'LineSegment2D' \
            else Polyline2D.from_dict(data['geometry'])
        con = cls(data['identifier'], geo, wires)
        if 'display_name' in data and data['display_name'] is not None:
            con.display_name = data['display_name']
        return con
Esempio n. 3
0
    def from_dict_abridged(cls, data, wires):
        """Initialize an ElectricalConnector from an abridged dictionary.

        Args:
            data: A ElectricalConnectorAbridged dictionary.
            wires: A dictionary with identifiers of Wires as keys and Python
                Wire objects as values.
        """
        assert data['type'] == 'ElectricalConnectorAbridged', \
            'Expected ElectricalConnectorAbridged. Got {}.'.format(data['type'])
        try:
            wires = [wires[wire_id] for wire_id in data['wires']]
        except KeyError as e:
            raise ValueError('Failed to find "{}" in wires.'.format(e))
        geo = LineSegment2D.from_dict(data['geometry']) \
            if data['geometry']['type'] == 'LineSegment2D' \
            else Polyline2D.from_dict(data['geometry'])
        con = cls(data['identifier'], geo, wires)
        if 'display_name' in data and data['display_name'] is not None:
            con.display_name = data['display_name']
        return con