Skip to content

Commit

Permalink
Issue #3 Add the parameter cycles to define maximum number of active/…
Browse files Browse the repository at this point in the history
…iterated cycles
  • Loading branch information
kinow committed Dec 26, 2020
1 parent 12f4c30 commit ad02b1c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 4 additions & 2 deletions decyclify/node_iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class CycleIterator:
"""An iterator that will iterate over all the nodes in a cycle,
before moving to the next cycles."""

def __init__(self, graph: DiGraph):
def __init__(self, graph: DiGraph, cycles=2):
if not isinstance(graph, DiGraph):
raise TypeError('graph must be a non-empty DiGraph')
self.graph = graph
self.cycles = cycles
self.intraiteration_matrix = create_intraiteration_matrix(graph)
self.matrix = np.array(self.intraiteration_matrix, copy=True)
self.current_cycle = 0
Expand Down Expand Up @@ -71,11 +72,12 @@ class TasksIterator:
c.1 is found, it will start the cycle 2 and iterate over a.2,
even if the cycle 1 is still being processed."""

def __init__(self, graph: DiGraph):
def __init__(self, graph: DiGraph, cycles=2):
if not isinstance(graph, DiGraph):
raise TypeError('graph must be a non-empty DiGraph')
graph, cycles_removed = decyclify(graph)
self.graph = graph
self.cycles = cycles
self.cycles_removed = cycles_removed

self.intraiteration_matrix = create_intraiteration_matrix(graph)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_node_iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@ def test_cycle_iterator_type_error():
def test_cycle_iterators(sample_graph):
graph, cycles_removed = decyclify(sample_graph, 'a')

iterator = CycleIterator(graph)
iterator = CycleIterator(graph, cycles=2)

expected = [['a.0'], ['b.0', 'e.0'], ['c.0'], ['d.0'], ['a.1'], ['b.1', 'e.1'], ['c.1'], ['d.1']]
iterated = []

for index, node in enumerate(iterator):
iterated.append(node)
if index == 7:
break
if index > len(expected):
pytest.fail('iterator iterations exceeded length of expected values!')

assert iterated == expected

def test_tasks_iterator(sample_graph):
graph, cycles_removed = decyclify(sample_graph, 'a')

iterator = TasksIterator(graph)
iterator = TasksIterator(graph, cycles=2)

expected = [['a.0'], ['b.0', 'e.0', 'a.1'], ['c.0', 'b.1'], ['d.0', 'c.1'], ['d.1']]
iterated = []

for index, node in enumerate(iterator):
iterated.append(node)
if index == 7:
break
if index > len(expected):
pytest.fail('iterator iterations exceeded length of expected values!')

# TODO: use iterator

Expand Down

0 comments on commit ad02b1c

Please sign in to comment.