def is_connected(self, whom, direction="<STR_LIT:to>", failed=None):
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>)<EOL><DEDENT>if isinstance(whom, list):<EOL><INDENT>is_list = True<EOL><DEDENT>else:<EOL><INDENT>whom = [whom]<EOL>is_list = False<EOL><DEDENT>whom_ids = [n.id for n in whom]<EOL>for node in whom:<EOL><INDENT>if not isinstance(node, Node):<EOL><INDENT>raise TypeError(<EOL>"<STR_LIT>".format(type(node))<EOL>)<EOL><DEDENT><DEDENT>if direction not in ["<STR_LIT:to>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>".format(direction)<EOL>)<EOL><DEDENT>connected = []<EOL>if direction == "<STR_LIT:to>":<EOL><INDENT>vectors = (<EOL>Vector.query.with_entities(Vector.destination_id)<EOL>.filter_by(origin_id=self.id, failed=False)<EOL>.all()<EOL>)<EOL>destinations = set([v.destination_id for v in vectors])<EOL>for w in whom_ids:<EOL><INDENT>connected.append(w in destinations)<EOL><DEDENT><DEDENT>elif direction == "<STR_LIT>":<EOL><INDENT>vectors = (<EOL>Vector.query.with_entities(Vector.origin_id)<EOL>.filter_by(destination_id=self.id, failed=False)<EOL>.all()<EOL>)<EOL>origins = set([v.origin_id for v in vectors])<EOL>for w in whom_ids:<EOL><INDENT>connected.append(w in origins)<EOL><DEDENT><DEDENT>elif direction in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>vectors = (<EOL>Vector.query.with_entities(Vector.origin_id, Vector.destination_id)<EOL>.filter(<EOL>and_(<EOL>Vector.failed == false(),<EOL>or_(<EOL>Vector.destination_id == self.id,<EOL>Vector.origin_id == self.id,<EOL>),<EOL>)<EOL>)<EOL>.all()<EOL>)<EOL>destinations = set([v.destination_id for v in vectors])<EOL>origins = set([v.origin_id for v in vectors])<EOL>if direction == "<STR_LIT>":<EOL><INDENT>origins_destinations = destinations.union(origins)<EOL><DEDENT>elif direction == "<STR_LIT>":<EOL><INDENT>origins_destinations = destinations.intersection(origins)<EOL><DEDENT>for w in whom_ids:<EOL><INDENT>connected.append(w in origins_destinations)<EOL><DEDENT><DEDENT>if is_list:<EOL><INDENT>return connected<EOL><DEDENT>else:<EOL><INDENT>return connected[<NUM_LIT:0>]<EOL><DEDENT>
Check whether this node is connected [to/from] whom.

        whom can be a list of nodes or a single node.
        direction can be "to" (default), "from", "both" or "either".

        If whom is a single node this method returns a boolean,
        otherwise it returns a list of booleans