Feat: add pure graph

This commit is contained in:
Bertrand Benjamin 2024-10-07 06:17:01 +02:00
parent c90f407cfc
commit 5c69bb5503
2 changed files with 49 additions and 0 deletions

33
plesna/graph.py Normal file
View File

@ -0,0 +1,33 @@
from typing import Callable
from pydantic import BaseModel
class Node(BaseModel):
name: str
infos: dict = {}
def __hash__(self):
return hash(self.name)
class Edge(BaseModel):
arrow_name: str
source: Node
target: Node
edge_kwrds: dict = {}
class Graph:
def __init__(self):
self._edges = []
self._nodes = set()
def append(self, edge: Edge):
self._edges.append(edge)
self._nodes.add(edge.source)
self._nodes.add(edge.target)
@property
def nodes(self):
return self._nodes

View File

@ -0,0 +1,16 @@
from plesna.graph import Edge, Graph, Node
def test_init():
nodeA = Node(name="A")
nodeB = Node(name="B")
nodeC = Node(name="C")
edge1 = Edge(arrow_name="arrow", source=nodeA, target=nodeC)
edge2 = Edge(arrow_name="arrow", source=nodeB, target=nodeC)
graph = Graph()
graph.append(edge1)
graph.append(edge2)
assert graph.nodes == {nodeA, nodeB, nodeC}