def test_polyline2d_to_from_dict(): """Test the to/from dict of Polyline2D objects.""" pts = (Point2D(0, 0), Point2D(2, 0), Point2D(2, 2), Point2D(0, 2)) pline = Polyline2D(pts) pline_dict = pline.to_dict() new_pline = Polyline2D.from_dict(pline_dict) assert isinstance(new_pline, Polyline2D) assert new_pline.to_dict() == pline_dict
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
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