def test_get_time_of_running(self):
     clock = Clock()
     with clock.running('a'):
         time.sleep(.1)
         delta1 = int(10 * clock.get_time())
         time.sleep(.1)
     delta2 = int(10 * clock.get_time())
     self.assertEqual(delta1, 1)
     self.assertEqual(delta2, 2)
 def test_reset_all(self):
     clock = Clock()
     clock.start('a', 'b')
     time.sleep(.1)
     clock.stop('b')
     self.assertEqual(len(clock.delta), 1)
     clock.reset()
     self.assertEqual(len(clock.get_time()), 0)
 def test_stop_all(self):
     clock = Clock()
     clock.start('a', 'b')
     time.sleep(.1)
     clock.stop()
     self.assertEqual(int(10 * clock.get_time('a')), 1)
     self.assertEqual(int(10 * clock.get_time('b')), 1)
    def test_printing(self):
        clock = Clock()
        with clock.running('a', 'b', 'c'):
            with clock.paused('a'):
                time.sleep(.1)
                with clock.paused('b'):
                    time.sleep(.1)

        with print_catcher() as printer:
            print(repr(clock))

        names = []
        for ind, line in enumerate(printer.txt.split('\n')):
            if line:
                if ind > 0:
                    names.append(line.split()[-1])

        self.assertEqual(names, ['c', 'b', 'a'])
    def test_pausing(self):
        clock = Clock()

        with clock.running('a', 'b', 'c'):
            time.sleep(.1)
            with clock.paused('b', 'c'):
                time.sleep(.1)

        self.assertEqual(int(10 * clock.get_time('a')), 2)
        self.assertEqual(int(10 * clock.get_time('b')), 1)
        self.assertEqual(int(10 * clock.get_time('c')), 1)
        self.assertEqual({int(10 * v)
                          for v in clock.get_time().values()}, {1, 2})
Example #6
0
    def __init__(self, name, **kwargs):
        # assign any user-defined attributes
        for k, v in kwargs.items():
            setattr(self, k, v)
        self.name = name
        self._upstream_nodes = []
        self._downstream_nodes = []

        self._num_top_down_calls = 0

        # node network can be visualized with pydot.  These hold args and kwargs
        # that will be used to add and connect this node in the graph visualization
        self._pydot_node_kwargs = dict(name=self.name, shape='rectangle')
        self._pydot_edge_kwarg_list = []

        self._router = None

        # this will be one of three values: None, 'input', 'output'
        self._logging = None

        # add a clock to allow for timing
        self.clock = Clock()
 def test_bad_start(self):
     clock = Clock()
     with self.assertRaises(ValueError):
         clock.start()
 def test_get_time_delta_only(self):
     clock = Clock()
     clock.start('a')
     clock.stop('a')
     self.assertEqual(clock.get_time('f'), {})
 def test_double_calls(self):
     clock = Clock()
     clock.start('a')
     clock.start('a')
     time.sleep(.1)
     clock.stop('a')
     clock.stop('a')
     self.assertEqual(int(round(10 * clock.get_time())), 1)
     clock.reset('a')
     clock.reset('a')
     clock.reset('b')
     clock.reset('b')
     self.assertEqual(clock.get_time(), {})