text
stringlengths
0
828
if node_name is not None:
node = self._node_name_map.get(node_name)
if node is not None:
return node
db_node = self._graph.database.Node.find_by_name(node_name)
if db_node is None:
self._internal_add_node(node_name=node_name,
external_id=external_id,
are_neighbors_cached=False,
add_to_cache=True)
else:
self._internal_add_node(node_name=db_node.name,
external_id=db_node.external_id,
are_neighbors_cached=db_node.are_neighbors_cached,
add_to_cache=False)
else:
return None
else:
self._internal_add_node(node_name=db_node.name,
external_id=db_node.external_id,
are_neighbors_cached=db_node.are_neighbors_cached,
add_to_cache=False)
node = self._node_name_map.get(node_name)
# Trying to load the cached neighbors of the created node from the database could
# cause a very-very-very deep recursion, so don't even think about doing it here.
return node"
4435,"def _internal_add_node(self,
node_name: str,
external_id: Optional[str] = None,
are_neighbors_cached: bool = False,
add_to_cache: bool = False) -> None:
""""""
Adds a node with the given name to the graph without checking whether it already exists or not.
Arguments:
node_name (str): The name of the node to add.
external_id (Optional[str]): The external ID of the node.
are_neighbors_cached (bool): Whether the neighbors of the node have already been cached.
add_to_cache (bool): Whether the node should also be created in the local cache.
""""""
index: int = len(self)
node: Node = self._create_node(index, node_name, external_id)
node.are_neighbors_cached = are_neighbors_cached
self._nodes[index] = node
self._node_name_map[node_name] = node
if add_to_cache:
db: GraphDatabaseInterface = self._graph.database
db_node: DBNode = db.Node.find_by_name(node.name)
if db_node is None:
db_node = db.Node(node.name, node.external_id)
db_node.are_neighbors_cached = False
db.session.add(db_node)
db.session.commit()"
4436,"def edge_list(self) -> List[Edge]:
""""""
The ordered list of edges in the container.
""""""
return [edge for edge in sorted(self._edges.values(), key=attrgetter(""key""))]"
4437,"def add_edge(self,
source: Node,
target: Node,
weight: float = 1,
save_to_cache: bool = True) -> None:
""""""
Adds an edge to the edge list that will connect the specified nodes.
Arguments:
source (Node): The source node of the edge.
target (Node): The target node of the edge.
weight (float): The weight of the created edge.
save_to_cache (bool): Whether the edge should be saved to the local database.
""""""
if not isinstance(source, Node):
raise TypeError(""Invalid source: expected Node instance, got {}."".format(source))
if not isinstance(target, Node):
raise TypeError(""Invalid target: expected Node instance, got {}."".format(target))
if source.index == target.index or\
self.get_edge_by_index(source.index, target.index) is not None:
return
self._edges[(source.index, target.index)] = Edge(source, target, weight)
if save_to_cache:
should_commit: bool = False
database: GraphDatabaseInterface = self._graph.database
db_edge: DBEdge = database.Edge.find_by_name(source.name, target.name)
if db_edge is None:
database.session.add(database.Edge(source.name, target.name, weight))
should_commit = True
elif db_edge.weight != weight:
db_edge.weight = weight
should_commit = True
if should_commit: