示例#1
0
 def value(self, a_value):
     iterative_eval_tracker.calced(self)
     self.wip = False
     self._value = a_value
     if not self.close_enough(self._prev_value,
                              tol=iterative_eval_tracker.tolerance):
         iterative_eval_tracker.wip(self)
示例#2
0
    def _value_tree_str(self, address, indent=0):
        """Generator which returns a formatted dependency graph"""
        cell = self.cell_map[address]

        if iterative_eval_tracker.is_calced(address):
            yield f'{" " * indent}{address} <- cycle'
        else:
            iterative_eval_tracker.calced(address)
            yield f'{" " * indent}{address} = {cell.value}'
            for children in sorted(self.dep_graph.predecessors(cell),
                                   key=lambda a: a.address.address):
                yield from self._value_tree_str(children.address.address,
                                                indent + 1)
示例#3
0
def test_iterative_eval_tracker():
    assert isinstance(iterative_eval_tracker.ns.todo, set)

    # init the tracker
    iterative_eval_tracker(iterations=100, tolerance=0.001)
    assert iterative_eval_tracker.ns.iteration_number == 0
    assert iterative_eval_tracker.ns.iterations == 100
    assert iterative_eval_tracker.ns.tolerance == 0.001
    assert iterative_eval_tracker.tolerance == 0.001
    assert iterative_eval_tracker.done

    # test done if no WIP
    iterative_eval_tracker.wip(1)
    assert iterative_eval_tracker.ns.todo == {1}
    assert not iterative_eval_tracker.done
    iterative_eval_tracker.inc_iteration_number()
    assert iterative_eval_tracker.done

    # init the tracker
    iterative_eval_tracker(iterations=2, tolerance=5)
    assert iterative_eval_tracker.ns.iteration_number == 0
    assert iterative_eval_tracker.ns.iterations == 2
    assert iterative_eval_tracker.ns.tolerance == 5

    # test done if max iters exceeded
    iterative_eval_tracker.inc_iteration_number()
    assert iterative_eval_tracker.ns.iteration_number == 1
    iterative_eval_tracker.wip(1)
    assert not iterative_eval_tracker.done

    iterative_eval_tracker.inc_iteration_number()
    assert iterative_eval_tracker.ns.iteration_number == 2
    iterative_eval_tracker.wip(1)
    assert iterative_eval_tracker.done

    # check calced / iscalced
    assert not iterative_eval_tracker.is_calced(1)
    iterative_eval_tracker.calced(1)
    assert iterative_eval_tracker.is_calced(1)
    iterative_eval_tracker.inc_iteration_number()
    assert not iterative_eval_tracker.is_calced(1)
示例#4
0
def test_iterative_eval_tracker():
    assert isinstance(iterative_eval_tracker.ns.todo, set)

    def init_tracker():
        # init the tracker
        iterative_eval_tracker(iterations=100, tolerance=0.001)
        assert iterative_eval_tracker.ns.iteration_number == 0
        assert iterative_eval_tracker.ns.iterations == 100
        assert iterative_eval_tracker.ns.tolerance == 0.001
        assert iterative_eval_tracker.tolerance == 0.001
        assert iterative_eval_tracker.done

    def do_test_tracker():
        # test done if no WIP
        iterative_eval_tracker.wip(1)
        assert iterative_eval_tracker.ns.todo == {1}
        assert not iterative_eval_tracker.done
        iterative_eval_tracker.inc_iteration_number()
        assert iterative_eval_tracker.done

    init_tracker()
    do_test_tracker()

    # init the tracker
    iterative_eval_tracker(iterations=2, tolerance=5)
    assert iterative_eval_tracker.ns.iteration_number == 0
    assert iterative_eval_tracker.ns.iterations == 2
    assert iterative_eval_tracker.ns.tolerance == 5

    # test done if max iters exceeded
    iterative_eval_tracker.inc_iteration_number()
    assert iterative_eval_tracker.ns.iteration_number == 1
    iterative_eval_tracker.wip(1)
    assert not iterative_eval_tracker.done

    iterative_eval_tracker.inc_iteration_number()
    assert iterative_eval_tracker.ns.iteration_number == 2
    iterative_eval_tracker.wip(1)
    assert iterative_eval_tracker.done

    # check calced / iscalced
    assert not iterative_eval_tracker.is_calced(1)
    iterative_eval_tracker.calced(1)
    assert iterative_eval_tracker.is_calced(1)
    iterative_eval_tracker.inc_iteration_number()
    assert not iterative_eval_tracker.is_calced(1)

    class AThread(threading.Thread):
        def run(self):
            try:
                init_tracker()
                import time
                time.sleep(0.1)
                do_test_tracker()
                self.result = True
            except:  # noqa: E722
                self.result = False

    thread = AThread()
    thread.start()
    init_tracker()
    do_test_tracker()
    thread.join()
    assert thread.result