[core] Adds a .copy() method to graph structure.
This commit is contained in:
@ -1,3 +1,5 @@
|
|||||||
|
from copy import copy
|
||||||
|
|
||||||
from bonobo.constants import BEGIN
|
from bonobo.constants import BEGIN
|
||||||
|
|
||||||
|
|
||||||
@ -62,6 +64,15 @@ class Graph:
|
|||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def copy(self):
|
||||||
|
g = Graph()
|
||||||
|
|
||||||
|
g.edges = copy(self.edges)
|
||||||
|
g.named = copy(self.named)
|
||||||
|
g.nodes = copy(self.nodes)
|
||||||
|
|
||||||
|
return g
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def topologically_sorted_indexes(self):
|
def topologically_sorted_indexes(self):
|
||||||
"""Iterate in topological order, based on networkx's topological_sort() function.
|
"""Iterate in topological order, based on networkx's topological_sort() function.
|
||||||
|
|||||||
@ -71,3 +71,23 @@ def test_graph_topological_sort():
|
|||||||
assert g.topologically_sorted_indexes.index(3) < g.topologically_sorted_indexes.index(4)
|
assert g.topologically_sorted_indexes.index(3) < g.topologically_sorted_indexes.index(4)
|
||||||
assert g[3] == sentinel.b1
|
assert g[3] == sentinel.b1
|
||||||
assert g[4] == sentinel.b2
|
assert g[4] == sentinel.b2
|
||||||
|
|
||||||
|
|
||||||
|
def test_copy():
|
||||||
|
g1 = Graph()
|
||||||
|
g2 = g1.copy()
|
||||||
|
|
||||||
|
assert g1 is not g2
|
||||||
|
|
||||||
|
assert len(g1) == 0
|
||||||
|
assert len(g2) == 0
|
||||||
|
|
||||||
|
g1.add_chain([])
|
||||||
|
|
||||||
|
assert len(g1) == 1
|
||||||
|
assert len(g2) == 0
|
||||||
|
|
||||||
|
g2.add_chain([], identity)
|
||||||
|
|
||||||
|
assert len(g1) == 1
|
||||||
|
assert len(g2) == 2
|
||||||
|
|||||||
Reference in New Issue
Block a user