def neighbors(self, type=None, direction="<STR_LIT:to>", failed=None):
<EOL>if type is None:<EOL><INDENT>type = Node<EOL><DEDENT>if not issubclass(type, Node):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(type)<EOL>)<EOL><DEDENT>if direction not in ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT:to>"]:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(direction)<EOL>)<EOL><DEDENT>if failed is not None:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>neighbors = []<EOL>if direction == "<STR_LIT:to>":<EOL><INDENT>outgoing_vectors = (<EOL>Vector.query.with_entities(Vector.destination_id)<EOL>.filter_by(origin_id=self.id, failed=False)<EOL>.all()<EOL>)<EOL>neighbor_ids = [v.destination_id for v in outgoing_vectors]<EOL>if neighbor_ids:<EOL><INDENT>neighbors = Node.query.filter(Node.id.in_(neighbor_ids)).all()<EOL>neighbors = [n for n in neighbors if isinstance(n, type)]<EOL><DEDENT><DEDENT>if direction == "<STR_LIT>":<EOL><INDENT>incoming_vectors = (<EOL>Vector.query.with_entities(Vector.origin_id)<EOL>.filter_by(destination_id=self.id, failed=False)<EOL>.all()<EOL>)<EOL>neighbor_ids = [v.origin_id for v in incoming_vectors]<EOL>if neighbor_ids:<EOL><INDENT>neighbors = Node.query.filter(Node.id.in_(neighbor_ids)).all()<EOL>neighbors = [n for n in neighbors if isinstance(n, type)]<EOL><DEDENT><DEDENT>if direction == "<STR_LIT>":<EOL><INDENT>neighbors = list(<EOL>set(<EOL>self.neighbors(type=type, direction="<STR_LIT:to>")<EOL>+ self.neighbors(type=type, direction="<STR_LIT>")<EOL>)<EOL>)<EOL><DEDENT>if direction == "<STR_LIT>":<EOL><INDENT>neighbors = list(<EOL>set(self.neighbors(type=type, direction="<STR_LIT:to>"))<EOL>& set(self.neighbors(type=type, direction="<STR_LIT>"))<EOL>)<EOL><DEDENT>return neighbors<EOL>
Get a node's neighbors - nodes that are directly connected to it.

        Type specifies the class of neighbour and must be a subclass of
        Node (default is Node).
        Connection is the direction of the connections and can be "to"
        (default), "from", "either", or "both".