Skip to content

Commit

Permalink
make exceptions for assertive add edge
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Sep 18, 2024
1 parent 2934053 commit 10c824f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion jord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

__project__ = "Jord"
__author__ = "Christian Heider Lindbjerg"
__version__ = "0.7.7"
__version__ = "0.7.8"
__doc__ = r"""
.. module:: jord
:platform: Unix, Windows
Expand Down
55 changes: 51 additions & 4 deletions jord/networkx_utilities/construction.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
from typing import Mapping, Any
from typing import Any, Mapping

import shapely
from networkx import MultiDiGraph

__all__ = ["assertive_add_edge", "add_shapely_node"]
__all__ = [
"assertive_add_edge",
"add_shapely_node",
"IllegalLoopException",
"IllegalDuplicateEdgeException",
]


class IllegalLoopException(Exception): ...


class IllegalDuplicateEdgeException(Exception): ...


def assertive_add_edge(
Expand All @@ -16,8 +27,28 @@ def assertive_add_edge(
allow_loops: bool = True,
allow_duplicates: bool = False,
) -> None:
"""
:param graph: The Graph
:type graph: MultiDiGraph
:param u: from node id
:type u: int
:param v: to node id
:type v: int
:param uniqueid: id of edge
:type uniqueid: int
:param attributes: attributes of edge
:type attributes: Mapping[str, Any]
:param allow_loops: Allow loops
:type allow_loops: bool
:param allow_duplicates: Allow duplicate edges
:type allow_duplicates: bool
:return: None
"""
if not allow_loops:
assert u != v, f"{u} == {v}"
if u == v:
raise IllegalLoopException(f"{u} == {v}")
# assert u != v, f"{u} == {v}"

assert isinstance(u, int)
assert isinstance(v, int)
Expand All @@ -26,14 +57,30 @@ def assertive_add_edge(
assert graph.has_node(v)

if not allow_duplicates and graph.has_edge(u, v, uniqueid):
assert not graph.has_edge(u, v, uniqueid)
if graph.has_edge(u, v, uniqueid):
raise IllegalDuplicateEdgeException(
f"Graph already contains the edge ({u} -> {v}) with {uniqueid=}"
)
# assert not graph.has_edge(u, v, uniqueid)

graph.add_edge(u, v, key=uniqueid, uniqueid=uniqueid, **attributes)


def add_shapely_node(
graph: MultiDiGraph, u: int, point: shapely.Point, **kwargs
) -> None:
"""
Add a shapely point based node to the graph.
:param graph: The Graph
:type graph: MultiDiGraph
:param u: Node id
:type u: int
:param point:
:type point: shapely.Point
:param kwargs: Attributes of node
:return: None
"""
assert isinstance(u, int)

graph.add_node(
Expand Down

0 comments on commit 10c824f

Please sign in to comment.