pygraph-tool is a module to create and manipulate graphs.
Project description
pygraph-tool
pygraph-tool is a module to create and manipulate graphs. Nodes can be all objects who you want and edges are oriented and valued at 1 by default. If you wish one graph not oriented, edges must be declared in one direction and then in the other.
Getting started
Import modules
Graph module:
from pygraph_tool import Graph
Exceptions module:
from pygraph_tool import (
NodeException,
EdgeException,
GraphException
)
Others modules (optional):
from pygraph_tool import Node, Edge
Create new graph
The new graph is empty (No node and no edge).
graph: Graph = Graph()
Create new nodes in graph
Create new nodes n1, n2 and n3, three nodes of graph.
graph.add_node("I'm n1", "n1")
graph.add_node("I'm n2", "n2")
graph.add_node("I'm n3", "n3")
If node already exists(same id), the GraphException
is raise.
try:
graph.add_node("I'm n1 again", "n1")
except GraphException as error:
pass # or do something...
If an argument is None
, the NodeException
is raise.
Create new unidirectional edge in graph
Create new edges e1 such as n1->n2 with weight = 1.5, e2 such as n3->n2 with weight by default = 1 and e3 such as n1->n3 with weight by default = 1
graph.add_unidirectional_edge("n1", "n2", "e1", 1.5)
graph.add_unidirectional_edge("n3", "n2", "e2")
graph.add_unidirectional_edge("n1", "n3", "e3")
If edge already exists (same id), the GraphException
is raise.
try:
graph.add_unidirectional_edge("n2", "n3", "e1")
except GraphException as error:
pass # or do something...
If an argument (except weight
argument) is None
, the EdgeException
is raise.
Create new bidirectional edge in graph
coming soon...
Remove node
If node doesn't exist in graph, GraphException
is raise.
try:
graph.remove_node("n2")
except GraphException as error:
pass # or do something...
Remove edge
If edge doesn't exist in graph, GraphException
is raise.
try:
graph.remove_edge("e3")
except GraphException as error:
pass # or do something...
Visualize the graph (very simple representation)
Create function for display graph
def displayGraph(graph: Graph) -> None:
# display the graph's nodes
for node in graph.nodes:
print(f"{node.node_id}: {node.node_content}")
# display the graph's edges
for edge in graph.edges:
message: str = (
f"{edge.node_start.node_id} "
f"--- {edge.edge_id} = {edge.weight} ---> "
f"{edge.node_end.node_id}"
)
print(message)
# Display graph
displayGraph(graph)
Author
If you have any questions or suggestions, please don't hesitate to contact me : belaich.david@outlook.fr.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pygraph_tool-0.9.1.tar.gz
.
File metadata
- Download URL: pygraph_tool-0.9.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8512fb154b05e370d35d03b5194e85454b29ba44fb50c81ad347bd107b6bd915 |
|
MD5 | abb63e0c73b7665cc2a6334b49e6237c |
|
BLAKE2b-256 | 2986cbaecabdc553efb22c25d1eeb9d41880c2cbe22423c4244db8e3f60857ca |
File details
Details for the file pygraph_tool-0.9.1-py3-none-any.whl
.
File metadata
- Download URL: pygraph_tool-0.9.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76a8497a93c2b994dcca424a4b738d7d235293a700a549d8ad02fa4fd6e632b9 |
|
MD5 | 2343ca1b16a05bc7520517cd45bf5886 |
|
BLAKE2b-256 | 56dd3bc95b89843b47daa7519fbba0bb2dbab59895616e695ae73b35b5b6dad3 |