def test_Seconds(self):
     t = Timer()
     # Invalid
     for seconds in [1, 1., '1', ['1'], ('1',), {'seconds': 1}]:
         with self.subTest(seconds=seconds):
             with self.assertRaises(AttributeError):
                 t.seconds = seconds
 def test_Minutes(self):
     t = Timer()
     # Invalid
     for minutes in [1, 1., '1', ['1'], ('1',), {'minutes': 1}]:
         with self.subTest(minutes=minutes):
             with self.assertRaises(AttributeError):
                 t.minutes = minutes
Esempio n. 3
0
 def test_RecordTime(self):
     t = Timer()
     t.time()
     self.assertEqual(t.label, '')
     self.assertGreater(t.seconds, 0.)
     self.assertGreater(t.minutes, 0.)
     self.assertEqual(t.clock_name, Timer.DEFAULT_CLOCK_NAME)
Esempio n. 4
0
 def test_InitOtherTimer(self):
     t = Timer('timer1')
     t.time()
     t2 = Timer('timer2', timer=t)
     self.assertNotEqual(t.label, t2.label)
     self.assertEqual(t.seconds, t2.seconds)
     self.assertEqual(t.minutes, t2.minutes)
     self.assertEqual(t.clock_name, t2.clock_name)
 def test_Label(self):
     t = Timer()
     # Invalid
     for label in [1, 1., ['timer1'], ('timer1',), {'label': 'timer1'}]:
         with self.subTest(label=label):
             with self.assertRaises(TypeError):
                 t.label = label
     # Valid
     t.label = 'timer1'
     self.assertEqual(t.label, 'timer1')
Esempio n. 6
0
 def test_UnregisterClock(self):
     TestStack.push(Timer.CLOCKS)
     # Invalid
     for clock_name in [1, 1., 'dummy_clock', ('clock',)]:
         with self.subTest(clock_name=clock_name):
             with self.assertRaises(KeyError):
                 Timer.unregister_clock(clock_name)
     # Valid
     Timer.unregister_clock('clock')
     self.assertFalse({'clock': time.clock}.items() <= Timer.CLOCKS.items())
     Timer.CLOCKS = TestStack.pop()
Esempio n. 7
0
 def test_InitUserArgs(self):
     # Invalid
     for value in ['0.', [0.], (0., ), {0: 0}]:
         with self.subTest(value=value):
             with self.assertRaises(TypeError):
                 t = Timer(seconds=value)
     with self.assertRaises(ValueError):
         t = Timer(seconds=-1.)
     # Valid
     t = Timer('timer1', seconds=10.5, clock_name='clock')
     self.assertEqual(t.label, 'timer1')
     self.assertEqual(t.seconds, 10.5)
     self.assertAlmostEqual(t.minutes, 10.5 / 60.)
     self.assertEqual(t.clock_name, 'clock')
    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'])
Esempio n. 9
0
 def test_Compatibility(self):
     TestStack.push(Timer.CLOCKS)
     Timer.register_clock('constant_time', constant_time)
     t = Timer('timer1', clock_name='clock')
     # Invalid
     for value in [
             0, 0., 'clock', time,
             Timer(clock_name='constant_time'),
             Timer(clock_name='time')
     ]:
         with self.subTest(value=value):
             self.assertFalse(t.is_compatible(value))
     # Valid
     self.assertTrue(t.is_compatible(Timer(clock_name='clock')))
     Timer.CLOCKS = TestStack.pop()
Esempio n. 10
0
 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()
Esempio n. 11
0
 def test_Reset(self):
     t = Timer('timer1', clock_name='process_time')
     t.time()
     t.reset()
     self.assertEqual(t.label, '')
     self.assertEqual(t.seconds, 0.)
     self.assertEqual(t.minutes, 0.)
     self.assertEqual(t.clock_name, Timer.DEFAULT_CLOCK_NAME)
Esempio n. 12
0
 def test_Clear(self):
     # Assumes 'process_time' is a valid clock and not the default one.
     t = Timer('timer1', clock_name='process_time')
     t.time()
     t.clear()
     self.assertEqual(t.label, 'timer1')
     self.assertEqual(t.seconds, 0.)
     self.assertEqual(t.minutes, 0.)
     self.assertNotEqual(t.clock_name, Timer.DEFAULT_CLOCK_NAME)
Esempio n. 13
0
 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()
Esempio n. 14
0
 def test_RegisterClock(self):
     TestStack.push(Timer.CLOCKS)
     # Invalid key, valid value
     for keyval in [[1, 'clock'], [1., 'clock']]:
         with self.subTest(keyval=keyval):
             with self.assertRaises(KeyError):
                 Timer.register_clock(*keyval)
     # Valid key, invalid value
     for keyval in [['clock', 1], ['clock', 1.], ['clock', 'clock']]:
         with self.subTest(keyval=keyval):
             with self.assertRaises(ValueError):
                 Timer.register_clock(*keyval)
     # Valid
     Timer.register_clock('clock2', time.clock)
     self.assertTrue({'clock2': time.clock}.items() <= Timer.CLOCKS.items())
     Timer.CLOCKS = TestStack.pop()
Esempio n. 15
0
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
Esempio n. 16
0
 def test_Str(self):
     t1 = Timer()
     print(t1)
     t2 = Timer('timer2')
     print(t2)
Esempio n. 17
0
 def test_InitDefaultArgs(self):
     t = Timer()
     self.assertEqual(t.label, '')
     self.assertEqual(t.seconds, 0.)
     self.assertEqual(t.minutes, 0.)
     self.assertEqual(t.clock_name, Timer.DEFAULT_CLOCK_NAME)
Esempio n. 18
0
 def test_PrintClocksInfo(self):
     Timer.print_clocks()
Esempio n. 19
0
 def test_PrintInfo(self):
     t = Timer()
     t.print_info()