def test_prefix_in_progress_output(self): prefix = 'test_prefix: ' repeat.repeat( cmd=self.mock_cmd, count=10, verbose=True, progress_stream=self.mock_stdout, prefix=prefix, ) output = self.mock_stdout.getvalue().splitlines(True) # There should be at least *some* output. self.assertGreater(len(output), 0) for line in output: self.assertTrue(line.startswith(prefix)) self.assertTrue(line.endswith('\n'))
def test_abort_on_failure(self): self.mock_subprocess_call.returncodes = iter([0, 7]) returncode = repeat.repeat(self.mock_cmd, count=10, progress_stream=self.mock_stdout) self.assertEqual(returncode, 1) self.assertEqual(self.mock_subprocess_call.call_count, 2)
def main(): args = docopt.docopt(USAGE) if args['--until-failure']: victory_condition = unless_failure_victory elif args['--until-success']: victory_condition = until_success_victory else: victory_condition = repeat_victory sep = args['--separator'] program = [args['<command>']] + args['<args>'] period = int(args['--period']) max_tries = convert_or_none(int, args['--max-tries']) max_time = convert_or_none(int, args['--max-time']) def run_program(): ret_val = call(program) stdout.write(sep) return victory_condition(ret_val) success = repeat(run_program, period=period, max_tries=max_tries, max_time=max_time) if success: exit(0) else: exit(1)
def test_Should_CallAfterAfterYielding_When_AfterSpecified(self): func = mock.Mock(spec=[]) after = mock.Mock(spec=[]) func.side_effect = [1, StopIteration] for _ in repeat(func, after=after): pass self.assertTrue(after.called)
def test_Should_PropagateFunctionException_When_ExceptionRaised(self): func = mock.Mock(spec=[]) func.side_effect = [TestException] called_history = [] with self.assertRaises(TestException): for _ in repeat(func): pass
def test_keep_going(self): self.mock_subprocess_call.returncodes = iter([0, 1, 0]) returncode = repeat.repeat(self.mock_cmd, count=3, keep_going=True, progress_stream=self.mock_stdout) self.assertEqual(returncode, 1) self.assertEqual(self.mock_subprocess_call.call_count, 3)
def guessing_game_module(): num = random.randint(1, 10) guess = int(input('Guess a number between 1 and 10: ')) times = 1 while guess != num: guess = int(input('Guess again: ')) times += 1 if times == 3: break if guess == num: print('You win!') else: print('You lose! The number was', num) repeat.repeat()
def test_Should_CallFunctionUntilStopIterationException_When_Called(self): func = mock.Mock(spec=[]) func.side_effect = [[1], [2], StopIteration] called_history = [] for _ in repeat(func): called_history.append(func.called) func.called = False self.assertEqual([True, True], called_history)
def test_reported_runs_in_progress_output(self): repeat.repeat( cmd=self.mock_cmd, count=3, verbose=True, progress_stream=self.mock_stdout, prefix='testing:', ) full_output = self.mock_stdout.getvalue() expected_run_reporting = textwrap.dedent("""\ testing:Starting run 1 of 3. testing:Run 1 of 3 completed. testing:Starting run 2 of 3. testing:Run 2 of 3 completed. testing:Starting run 3 of 3. testing:Run 3 of 3 completed. """) self.assertIn(expected_run_reporting, full_output)
def test_reported_runs_in_infinite_case(self): self.mock_subprocess_call.returncodes = iter([0, 0, -23]) repeat.repeat( cmd=self.mock_cmd, count=None, verbose=True, progress_stream=self.mock_stdout, prefix='infinite testing> ', ) full_output = self.mock_stdout.getvalue() expected_run_reporting = textwrap.dedent("""\ infinite testing> Starting run 1. infinite testing> Run 1 completed. infinite testing> Starting run 2. infinite testing> Run 2 completed. infinite testing> Starting run 3. infinite testing> Run 3 failed with return code -23. """) self.assertIn(expected_run_reporting, full_output)
def test_repeat_ten_times(self): returncode = repeat.repeat(self.mock_cmd, count=10, progress_stream=self.mock_stdout) self.assertEqual(returncode, 0) self.assertEqual(self.mock_subprocess_call.call_count, 10) for args, kwargs in self.mock_subprocess_call.call_args_list: self.assertEqual(len(args), 1) self.assertEqual(args[0], self.mock_cmd) self.assertFalse(kwargs)
def test_no_progress_output_when_verbose_is_false(self): returncode = repeat.repeat( cmd=self.mock_cmd, count=10, verbose=False, progress_stream=self.mock_stdout, ) self.assertEqual(returncode, 0) self.assertEqual(self.mock_subprocess_call.call_count, 10) output = self.mock_stdout.getvalue() self.assertEqual(output, '')
def test_n_success_failure_in_output(self): self.mock_subprocess_call.returncodes = iter([0, -9, 0]) repeat.repeat( cmd=self.mock_cmd, count=3, verbose=True, keep_going=True, progress_stream=self.mock_stdout, prefix='keep going> ', ) full_output = self.mock_stdout.getvalue() expected_run_reporting = textwrap.dedent("""\ keep going> Starting run 1 of 3. keep going> Run 1 of 3 completed. keep going> # of successes: 1, # of failures: 0 (0.00%). keep going> Starting run 2 of 3. keep going> Run 2 of 3 failed with return code -9. keep going> # of successes: 1, # of failures: 1 (50.00%). keep going> Starting run 3 of 3. keep going> Run 3 of 3 completed. keep going> # of successes: 2, # of failures: 1 (33.33%). """) self.assertIn(expected_run_reporting, full_output)
import hello import repeat hello.main() s = 'shit' exclaim='true' print repeat.repeat(s,exclaim) print '-'*10
import hello import repeat hello.main() s = 'shit' exclaim = 'true' print repeat.repeat(s, exclaim) print '-' * 10
def test_Should_CallBeforeBeforeYielding_When_BeforeSpecified(self): func = mock.Mock(spec=[]) before = mock.Mock(spec=[]) func.side_effect = [1, StopIteration] for _ in repeat(func, before=before): self.assertTrue(before.called)
def main(): print repeat('yay',0) print repeat('woo hoo',1)
import repeat print repeat.repeat("qwerty", 1, 4, 5) try: print repeat.repeat(1, 4, 5, 6) except Exception as ex: print ex try: print repeat.repeat("qwerty", 1, 4, 5, 6) except Exception as ex: print ex
def test_input_1(self): # Failure message: # expected (repeat('*', 3) to equal '***' self.assertEqual(repeat('*', 3), '***')
def test_input_3(self): # Failure message: # expected repeat('abc', 0) to equal '' self.assertEqual(repeat('abc', 0), '')
def test_input_2(self): # Failure message: # expected repeat('abc', 2) to equal 'abcabc' self.assertEqual(repeat('abc', 2), 'abcabc')
[VOLUME, G4], [0, C5], [VOLUME, G4], [VOLUME, C5], [VOLUME, F5], [VOLUME, E5], [VOLUME, C5], ] from fade import fade from gain import gain from repeat import repeat from square import square_wave all_samples = [] quarter_second = 44100 // 4 for volume, frequency in notes: samples = square_wave(int(44100 / frequency // 2)) samples = gain(samples, volume) samples = repeat(samples, quarter_second) samples = fade(samples, quarter_second) all_samples.extend(samples) all_samples = [int(sample) for sample in all_samples] w = wave.open('music.wav', 'wb') w.setnchannels(1) w.setsampwidth(2) w.setframerate(44100) w.writeframes(struct.pack('<' + 'h' * len(all_samples), *all_samples))
def test_repeat(): got = repeat("a", 4) want = "aaaa" assert got == want
def test_repeat_zero_times(self): returncode = repeat.repeat(self.mock_cmd, count=0, progress_stream=self.mock_stdout) self.assertEqual(returncode, 0) self.assertFalse(self.mock_subprocess_call.called)