def test_connect_remove_nodes_edges(root, normalizer): graph = Graph(root=root, normalizer=normalizer) start = Node() end = Node() other = Node() another = Node() edge = graph.connect(start=start, verb="NEIGHBORS", end=end) assert graph.info() == dict(nodes=2, edges=1) graph.reindex() assert 2 == len(list(graph.iterate_edges(verbs="NEIGHBORS"))) assert 1 == len(list(graph.iterate_edges("NEIGHBORS", nodes=start))) edge2 = graph.connect(start=end, verb="NEIGHBORS", end=start) assert isinstance(edge2, Edge) assert graph.info() == dict(nodes=2, edges=2) graph.reindex() assert 4 == len(list(graph.edges.iterate("NEIGHBORS"))) assert 2 == len(list(graph.edges.iterate("NEIGHBORS", nodes=start))) graph.connect(start=start, verb="NEIGHBORS", end=other) assert graph.info() == dict(nodes=3, edges=3) graph.reindex() assert 6 == len(list(graph.edges.iterate("NEIGHBORS"))) assert 1 == len(list(graph.edges.iterate("NEIGHBORS", nodes=other))) graph.connect(start=start, verb="NEIGHBORS", end=another) assert graph.info() == dict(nodes=4, edges=4) graph.remove_edge(edge) assert graph.info() == dict(nodes=4, edges=3) graph.remove_node(other) assert graph.info() == dict(nodes=3, edges=3) graph.remove_node(end) assert graph.info() == dict(nodes=2, edges=3) graph.remove_node(another) assert graph.info() == dict(nodes=1, edges=3) graph.reindex() assert graph.info() == dict(nodes=1, edges=3) graph.remove_edges(other) assert graph.info() == dict(nodes=1, edges=2) graph.clean_edges() assert graph.info() == dict(nodes=1, edges=0)
def remove_edges(self, node: NodeKey) -> int: key = Node.to_key(node) count = 0 for _, edge in self.edges.iterate(nodes=key): self.edges.remove(edge) count += 1 return count
def get_neighbors( self, node_key: NodeKey, verb: str = None, direction: Optional[Direction] = None, label: str = None, offset: int = 0, limit: int = 10, ) -> NeighborResponse: node_key = Node.to_key(node_key) neighbors, total_count = self.graph.get_neighbors( node_key=node_key, verb=verb, direction=direction, label=label, offset=offset, limit=limit, ) for neighbor in neighbors: neighbor.node = self.get_node(neighbor.key) return NeighborResponse( neighbors=neighbors, offset=offset, limit=limit, total=total_count, )
def __iter__(self) -> Iterator[Trail]: seen = set() for start in self.starts: start = Node.to_key(start) if start not in seen: seen.add(start) yield Trail(start=start)
def iterate(self, verbs=None, directions=None, nodes=None): verbs = (None, ) if not verbs else verbs nodes = (None, ) if nodes is None else nodes directions = Direction.as_tuple(directions, all_if_none=True) for verb in ensure_iterable(verbs): for direction in ensure_iterable(directions): for node in ensure_iterable(nodes): node_key = Node.to_key(node) it = self._iterate_ts_line(direction, node_key, verb) for ts, line in it: edge = Edge.from_line(line, ts=ts) yield edge.get_other(direction), edge
def test_clear_info(root, normalizer): graph = Graph(root, normalizer) graph.save_node(node=Node()) graph.reindex() assert graph.info() == { "nodes": 1, "edges": 0, } graph.clear() graph.reindex() assert graph.info() == { "nodes": 0, "edges": 0, }
def get_edges( self, node_key: NodeKey, verb: str = None, direction: Optional[Direction] = None, limit: int = 100, ) -> List[Edge]: node_key = Node.to_key(node_key) edges = [] for _, edge in self.graph.iterate_edges(nodes=node_key, verbs=verb, directions=direction): edges.append(edge) if len(edges) >= limit: break return edges
def save_node(self, node: Union[Node, dict]) -> Node: node = Node.create(node) self.graph.save_node(node) return node
def remove_node(self, key: str) -> Node: node = self.proxy.remove_node(key) node = Node.create(node) if node else None return node
def save_node(self, node: Node) -> Node: node = self.proxy.save_node(node) node = Node.create(node) if node else None return node
def get_node(self, key: str) -> Optional[Node]: node = self.proxy.get_node(key) node = Node.create(node) if node else None return node
def test_edge_request(): node = Node() # noinspection PyTypeChecker request = NeighborRequest(node_key=node) assert request.node_key == node.key
def remove(self, node: Node) -> Node: key = Node.to_key(node) removed = self.cache.pop(key, None) return removed
def __contains__(self, node) -> bool: key = Node.to_key(node) return self.cache.__contains__(key)