-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide simple GraphLike and NodeLike implementations
- Loading branch information
1 parent
c74e8d6
commit 9fb06e1
Showing
5 changed files
with
122 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Dict, Iterable, Tuple | ||
|
||
from typing_extensions import Self | ||
|
||
from .protocols import GraphLike, NodeLikeT_co, Position | ||
|
||
|
||
@dataclass | ||
class SimpleNode: | ||
"""SimpleNode provides a base class and a simple implementation of | ||
the :py:class:`NodeLike` protocol.""" | ||
|
||
id: int | ||
position: Position | ||
|
||
|
||
@dataclass | ||
class SimpleExternalNode: | ||
"""SimpleExternalNode provides a base class and a simple implementation of | ||
the :py:class:`ExternalNodeLike` protocol.""" | ||
|
||
id: int | ||
position: Position | ||
external_id: int | ||
|
||
@classmethod | ||
def with_same_external_id(cls, id: int, position: Position) -> Self: | ||
"""with_same_external_id instantiates a SimpleExternalNode with | ||
:py:attr:`external_id` set to the same value as :py:attr:`id`. | ||
""" | ||
return cls(id=id, position=position, external_id=id) | ||
|
||
|
||
@dataclass | ||
class SimpleGraph(GraphLike[NodeLikeT_co]): | ||
"""SimpleGraph provides a base class and a simple implementation of | ||
the :py:class:`GraphLike` protocol over two dictionaries: one holding nodes, | ||
and another holding edge costs.""" | ||
|
||
nodes: Dict[int, NodeLikeT_co] = field(default_factory=dict) | ||
edges: Dict[int, Dict[int, float]] = field(default_factory=dict) | ||
|
||
def get_node(self, id: int) -> NodeLikeT_co: | ||
return self.nodes[id] | ||
|
||
def get_edges(self, id: int) -> Iterable[Tuple[int, float]]: | ||
return self.edges[id].items() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters