Skip to content

Commit dd394f8

Browse files
committed
✨ add edge.set_weight() and edge.weight()
1 parent cabc111 commit dd394f8

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

pynode_next/edge.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self, source, target, weight=None, directed=False):
1212
self._weight = weight
1313
if weight == None:
1414
self._weight = ""
15-
15+
1616
self._color = Color.LIGHT_GREY
1717

1818
self._internal_id = uuid.uuid4()
@@ -70,6 +70,18 @@ def color(self):
7070
"""Gets the edge's color."""
7171
return self._color
7272

73+
def set_weight(self, weight):
74+
"""Sets the edge's weight. (Weight must be serialisable)"""
75+
self._weight = weight
76+
core.ax(
77+
lambda x: self._dispatch_wrapper(x, {"labels": {1: {"text": str(weight)}}})
78+
)
79+
return self
80+
81+
def weight(self):
82+
"""Returns the edge's weight. Returns an empty string if weight wasn't defined at init and hasn't been changed since."""
83+
return self._weight
84+
7385
def traverse(self, initial_node=None, color=Color.RED, keep_path=True):
7486
if initial_node == None:
7587
source = self._source
@@ -114,11 +126,7 @@ def _data(self):
114126
"source": str(self._source),
115127
"target": str(self._target),
116128
"directed": self._directed,
117-
"labels": {
118-
1: {
119-
"text": str(self._weight)
120-
}
121-
}
129+
"labels": {1: {"text": str(self._weight)}},
122130
}
123131
}
124132
}

0 commit comments

Comments
 (0)