NodeView
__init__(graph)
Examples:
To obtain a NodeView
instance, please use Graph.nodes
. Refer to Graph.nodes:
>>> G = Graph.from_db("Social")
>>> node_view = G.nodes
>>> len(G.nodes)
2
__getitem__(key)
Retrieve data associated with a node.
The interpretation of key
depends on the graph's node type configuration:
- Single Node Type: Pass the node’s identifier as
node_id
. In this scenario, since the graph contains only one node type, the type is automatically assigned based on the providednode_id
. You don’t need to specify the node type explicitly. - Multiple Node Types: Pass a tuple in the form
(node_type, node_id)
.
Examples:
Retrieve data for a node using its identifier.
Single Node Type Example:
>>> G = Graph.from_db("Social")
>>> G.nodes["Alice"]
{'name': 'Alice', 'age': 25}
Multiple Node Types Example:
>>> G = Graph.from_db("Social")
>>> G.nodes[("Person", "Alice")]
{'name': 'Alice', 'age': 25}
__contains__(key)
Check if a node exists.
The interpretation of key
depends on the graph's node type configuration:
- Single Node Type: Pass the node’s identifier as
node_id
. In this scenario, since the graph contains only one node type, the type is automatically assigned based on the providednode_id
. You don’t need to specify the node type explicitly. - Multiple Node Types: Pass a tuple in the form
(node_type, node_id)
.
Examples:
Check whether a node exists in the view.
Single Node Type Example:
>>> G = Graph.from_db("Social")
>>> "Alice" in G.nodes
True
Multiple Node Types Example:
>>> G = Graph.from_db("Social")
>>> ("Person", "Alice") in G.nodes
True
__iter__()
Iterate over all nodes.
The return value depends on the graph’s node type configuration:
- Single Node Type: Each iteration returns a
node_id
. - Multiple Node Types: Each iteration returns a tuple
(node_type, node_id)
.
Warning
Iterating over all nodes will retrieve all data from the database.
This method is intended for small datasets only. For large datasets, using this method may lead to significant performance issues or excessive memory usage.
Examples:
Iterate over all nodes in the view.
Single Node Type Example:
>>> G = Graph.from_db("Social")
>>> for node_id in G.nodes:
>>> print(node_id)
Michael
Alice
Multiple Node Types Example:
>>> G = Graph.from_db("Social")
>>> for node_type, node_id in G.nodes:
>>> print(f"{node_type}: {node_id}")
Person: Alice
Person: Michael
Community: Photography Enthusiasts
Community: Fitness and Wellness Group
__len__()
Return the number of nodes.
Examples:
Get the number of nodes in the view:
>>> len(G.nodes)
2