def start_defining_segment_if_close_to_socket(self, mouse_pos):
        """Trigger segment definition if a socket from given node is nearby."""

        ### reference nodes near the mouse in a dedicated list

        nodes_near_mouse = self.nodes_near_mouse

        nodes_near_mouse.extend(
            node
            for node in self.nodes_on_screen
            if node.rect.inflate(120, 120).collidepoint(mouse_pos)
        )

        ### clear the collection of nodes on screen
        self.nodes_on_screen.clear()

        ### if no collision was detected, we can leave the method right away,
        ### since there's no point searching for a nearby socket if there
        ### isn't even a nearby node
        if not nodes_near_mouse: return

        ### otherwise, it means the periphery of one or more nodes collided
        ### (we know that cause this method only executes when the mouse moves
        ### while clicked outside objects in the graph);
        ###
        ### we assume the user may want to start defining a segment from the
        ### nearest socket, but only if there's one close enough to the mouse,
        ### which is what we'll check next

        ## reference magnet point locally and set it to the mouse position

        mp = self.magnet_point
        mp.update(mouse_pos)

        ## get iterator of all connectable sockets

        sockets = chain.from_iterable(

            get_all_connectable_sockets(node)
            for node in nodes_near_mouse

        )

        ## try grabing the closest socket

        key_func = lambda item: mp.distance_to(item.rect.center)

        try:
            closest_socket = min(sockets, key=key_func)

        ### if none is found, indicate it by assigning None to the
        ### corresponding variable

        except ValueError:
            closest_socket = None

        ### before we continue, let's make sure the list of nodes
        ### near the mouse is cleared

        finally:
            nodes_near_mouse.clear()

        ### now, if there is indeed a socket near the mouse, we can
        ### trigger segment definition from that socket

        if closest_socket and mp.distance_to(closest_socket.rect.center) < 60:
            self.trigger_defining_segment(closest_socket, mouse_pos)
Trigger segment definition if a socket from given node is nearby.
nodezator/graphman/socketparenthood/action.py