def test_ClockName(self): t = Timer() # Invalid for clock_name in [1, 1., ['clock'], ('clock',), {'clock_name': 'clock'}]: with self.subTest(clock_name=clock_name): with self.assertRaises(TypeError): t.clock_name = clock_name for clock_name in ['myclock', 'acounter']: with self.subTest(clock_name=clock_name): with self.assertRaises(KeyError): t.clock_name = clock_name # Valid t.clock_name = 'clock' self.assertEqual(t.clock_name, 'clock') self.assertIs(t._clock, Timer.CLOCKS['clock'])
def test_RegisterClock(self): TestStack.push(Timer.CLOCKS) Timer.register_clock('constant_time', constant_time) t = Timer() t.clock_name = 'constant_time' t.time() self.assertEqual(t.clock_name, 'constant_time') self.assertAlmostEqual(t.seconds, 1.) self.assertAlmostEqual(t.minutes, 1. / 60.) Timer.CLOCKS = TestStack.pop()
def test_GetInfo(self): TestStack.push(Timer.CLOCKS) # Timer with clock supported by time.get_clock_info() t = Timer() info = t.get_info() self.assertIsInstance(info, types.SimpleNamespace) for value in info.__dict__.values(): with self.subTest(value=value): self.assertIsNotNone(value) # Timer with custom function, does not supports time.get_clock_info() Timer.register_clock('constant_time', constant_time) t.clock_name = 'constant_time' info = t.get_info() self.assertIsInstance(info, types.SimpleNamespace) self.assertIsNotNone(info.implementation) self.assertIsNone(info.adjustable) self.assertIsNone(info.monotonic) self.assertIsNone(info.resolution) Timer.CLOCKS = TestStack.pop()
from smarttimers import Timer # Find the current time function of a Timer t1 = Timer('Timer1') print(Timer.CLOCKS[t1.clock_name]) # or Timer.print_clocks() print(t1.clock_name) # Change current time function t1.clock_name = 'process_time' # Record a time measurement t1.time() print(t1) # Create another Timer compatible with 'Timer1' t2 = Timer('Timer2', clock_name='process_time') t2.print_info() t2.time() print(t2) # Sum Timers t3 = Timer.sum(t1, t2) # or t3 = t1 + t2 print(t3) # Find difference between Timers t4 = Timer.diff(t1, t2) # or