Python contextlib.redirect_stdout() Examples
The following are 30
code examples of contextlib.redirect_stdout().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
contextlib
, or try the search function
.
Example #1
Source File: utils.py From arm_now with MIT License | 7 votes |
def pcolor(color, *args, **kwargs): """ proxy print arguments """ output = sys.stdout if "file" not in kwargs else kwargs["file"] with contextlib.redirect_stdout(output): print(color, end="") print(*args, end="", **kwargs) print("\x1B[0m")
Example #2
Source File: test_descriptors_cpython.py From Clean-Code-in-Python with MIT License | 6 votes |
def test_working_example(self): instance = MyClass2() capture = io.StringIO() with redirect_stdout(capture): NewMethod("External call")(instance, "first", "second") external = capture.getvalue() self.assertIsNotNone(self.pattern.match(external), repr(external)) capture = io.StringIO() with redirect_stdout(capture): instance.method("first", "second") internal = capture.getvalue() self.assertIsNotNone(self.pattern.match(internal), repr(internal))
Example #3
Source File: unittest_checker_similar.py From python-netsurv with MIT License | 6 votes |
def test_multiline_imports(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([MULTILINE, MULTILINE]) assert ex.value.code == 0 assert ( output.getvalue().strip() == ( """ 8 similar lines in 2 files ==%s:0 ==%s:0 from foo import ( bar, baz, quux, quuux, quuuux, quuuuux, ) TOTAL lines=16 duplicates=8 percent=50.00 """ % (MULTILINE, MULTILINE) ).strip() )
Example #4
Source File: test_binaries.py From fairseq with MIT License | 6 votes |
def test_max_positions(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_max_positions') as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) with self.assertRaises(Exception) as context: train_translation_model( data_dir, 'fconv_iwslt_de_en', ['--max-target-positions', '5'], ) self.assertTrue( 'skip this example with --skip-invalid-size-inputs-valid-test' in str(context.exception) ) train_translation_model( data_dir, 'fconv_iwslt_de_en', ['--max-target-positions', '5', '--skip-invalid-size-inputs-valid-test'], ) with self.assertRaises(Exception) as context: generate_main(data_dir) generate_main(data_dir, ['--skip-invalid-size-inputs-valid-test'])
Example #5
Source File: unittest_checker_similar.py From python-netsurv with MIT License | 6 votes |
def test_ignore_nothing(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([SIMILAR1, SIMILAR2]) assert ex.value.code == 0 assert ( output.getvalue().strip() == ( """ 5 similar lines in 2 files ==%s:0 ==%s:0 import one from two import two three four five TOTAL lines=44 duplicates=5 percent=11.36 """ % (SIMILAR1, SIMILAR2) ).strip() )
Example #6
Source File: test_binaries.py From crosentgec with GNU General Public License v3.0 | 6 votes |
def test_fconv_self_att_wp(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_fconv_self_att_wp') as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) config = [ '--encoder-layers', '[(512, 3)] * 2', '--decoder-layers', '[(512, 3)] * 2', '--decoder-attention', 'True', '--encoder-attention', 'False', '--gated-attention', 'True', '--self-attention', 'True', '--project-input', 'True', ] train_translation_model(data_dir, 'fconv_self_att_wp', config) generate_main(data_dir) # fusion model os.rename(os.path.join(data_dir, 'checkpoint_last.pt'), os.path.join(data_dir, 'pretrained.pt')) config.extend([ '--pretrained', 'True', '--pretrained-checkpoint', os.path.join(data_dir, 'pretrained.pt'), '--save-dir', os.path.join(data_dir, 'fusion_model'), ]) train_translation_model(data_dir, 'fconv_self_att_wp', config)
Example #7
Source File: unittest_checker_similar.py From python-netsurv with MIT License | 6 votes |
def test_multiline_imports(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([MULTILINE, MULTILINE]) assert ex.value.code == 0 assert ( output.getvalue().strip() == ( """ 8 similar lines in 2 files ==%s:0 ==%s:0 from foo import ( bar, baz, quux, quuux, quuuux, quuuuux, ) TOTAL lines=16 duplicates=8 percent=50.00 """ % (MULTILINE, MULTILINE) ).strip() )
Example #8
Source File: test_binaries.py From fairseq with MIT License | 6 votes |
def test_cmlm_transformer(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_cmlm_transformer') as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir, ['--joined-dictionary']) train_translation_model(data_dir, 'cmlm_transformer', [ '--apply-bert-init', '--criterion', 'nat_loss', '--noise', 'full_mask', '--pred-length-offset', '--length-loss-factor', '0.1' ], task='translation_lev') generate_main(data_dir, [ '--task', 'translation_lev', '--iter-decode-max-iter', '9', '--iter-decode-eos-penalty', '0', '--print-step', ])
Example #9
Source File: test_binaries.py From fairseq with MIT License | 6 votes |
def test_iterative_nonautoregressive_transformer(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_iterative_nonautoregressive_transformer') as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir, ['--joined-dictionary']) train_translation_model(data_dir, 'iterative_nonautoregressive_transformer', [ '--apply-bert-init', '--src-embedding-copy', '--criterion', 'nat_loss', '--noise', 'full_mask', '--stochastic-approx', '--dae-ratio', '0.5', '--train-step', '3' ], task='translation_lev') generate_main(data_dir, [ '--task', 'translation_lev', '--iter-decode-max-iter', '9', '--iter-decode-eos-penalty', '0', '--print-step', ])
Example #10
Source File: test_binaries.py From fairseq with MIT License | 6 votes |
def test_mixture_of_experts(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_moe') as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) train_translation_model(data_dir, 'transformer_iwslt_de_en', [ '--task', 'translation_moe', '--user-dir', 'examples/translation_moe/src', '--method', 'hMoElp', '--mean-pool-gating-network', '--num-experts', '3', '--encoder-layers', '2', '--decoder-layers', '2', '--encoder-embed-dim', '8', '--decoder-embed-dim', '8', ]) generate_main(data_dir, [ '--task', 'translation_moe', '--user-dir', 'examples/translation_moe/src', '--method', 'hMoElp', '--mean-pool-gating-network', '--num-experts', '3', '--gen-expert', '0' ])
Example #11
Source File: unittest_checker_similar.py From python-netsurv with MIT License | 6 votes |
def test_ignore_nothing(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([SIMILAR1, SIMILAR2]) assert ex.value.code == 0 assert ( output.getvalue().strip() == ( """ 5 similar lines in 2 files ==%s:0 ==%s:0 import one from two import two three four five TOTAL lines=44 duplicates=5 percent=11.36 """ % (SIMILAR1, SIMILAR2) ).strip() )
Example #12
Source File: stdlib.py From tox with MIT License | 6 votes |
def suppress_output(): """suppress both stdout and stderr outputs""" if sys.version_info >= (3, 5): from contextlib import redirect_stdout, redirect_stderr else: class _RedirectStream(object): _stream = None def __init__(self, new_target): self._new_target = new_target self._old_targets = [] def __enter__(self): self._old_targets.append(getattr(sys, self._stream)) setattr(sys, self._stream, self._new_target) return self._new_target def __exit__(self, exctype, excinst, exctb): setattr(sys, self._stream, self._old_targets.pop()) class redirect_stdout(_RedirectStream): _stream = "stdout" class redirect_stderr(_RedirectStream): _stream = "stderr" with TemporaryFile("wt") as file: with redirect_stdout(file): with redirect_stderr(file): yield
Example #13
Source File: test_binaries.py From fairseq with MIT License | 6 votes |
def test_alignment(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_alignment') as data_dir: create_dummy_data(data_dir, alignment=True) preprocess_translation_data(data_dir, ['--align-suffix', 'align']) train_translation_model( data_dir, 'transformer_align', [ '--encoder-layers', '2', '--decoder-layers', '2', '--encoder-embed-dim', '8', '--decoder-embed-dim', '8', '--load-alignments', '--alignment-layer', '1', '--criterion', 'label_smoothed_cross_entropy_with_alignment' ], run_validation=True, ) generate_main(data_dir)
Example #14
Source File: test_arguments.py From zdict with GNU General Public License v3.0 | 6 votes |
def test_multiprocessing(self): testargs = ['', '-j', '2', '-d', '-dt', 'yahoo', 'test'] with patch.object(sys, 'argv', new=testargs): f1 = StringIO() with redirect_stdout(f1): main() testargs = ['', '-j', '-d', '-dt', 'yahoo', 'test'] with patch.object(sys, 'argv', new=testargs): f2 = StringIO() with redirect_stdout(f2): main() testargs = ['', '-d', '-dt', 'yahoo', 'test'] with patch.object(sys, 'argv', new=testargs): f3 = StringIO() with redirect_stdout(f3): main() result1 = f1.getvalue().strip() result2 = f2.getvalue().strip() result3 = f3.getvalue().strip() assert result1 == result2 == result3
Example #15
Source File: test_descriptors_cpython.py From Clean-code-in-Python with MIT License | 6 votes |
def test_working_example(self): instance = MyClass2() capture = io.StringIO() with redirect_stdout(capture): NewMethod("External call")(instance, "first", "second") external = capture.getvalue() self.assertIsNotNone(self.pattern.match(external), repr(external)) capture = io.StringIO() with redirect_stdout(capture): instance.method("first", "second") internal = capture.getvalue() self.assertIsNotNone(self.pattern.match(internal), repr(internal))
Example #16
Source File: test_binaries_gpu.py From fairseq with MIT License | 5 votes |
def test_levenshtein_transformer(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory( "test_levenshtein_transformer" ) as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir, ["--joined-dictionary"]) train_translation_model( data_dir, "levenshtein_transformer", [ "--apply-bert-init", "--early-exit", "6,6,6", "--criterion", "nat_loss", ], task="translation_lev", ) generate_main( data_dir, [ "--task", "translation_lev", "--iter-decode-max-iter", "9", "--iter-decode-eos-penalty", "0", "--print-step", ], )
Example #17
Source File: test_binaries.py From fairseq with MIT License | 5 votes |
def test_lstm_lm(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_lstm_lm') as data_dir: create_dummy_data(data_dir) preprocess_lm_data(data_dir) train_language_model( data_dir, 'lstm_lm', ['--add-bos-token'], run_validation=True, ) eval_lm_main(data_dir) generate_main(data_dir, [ '--task', 'language_modeling', '--sample-break-mode', 'eos', '--tokens-per-sample', '500', ])
Example #18
Source File: test_binaries.py From fairseq with MIT License | 5 votes |
def test_transformer_lm(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_transformer_lm') as data_dir: create_dummy_data(data_dir) preprocess_lm_data(data_dir) train_language_model( data_dir, 'transformer_lm', ['--add-bos-token'], run_validation=True, ) eval_lm_main(data_dir) generate_main(data_dir, [ '--task', 'language_modeling', '--sample-break-mode', 'eos', '--tokens-per-sample', '500', ])
Example #19
Source File: test_binaries.py From fairseq with MIT License | 5 votes |
def test_lightconv_lm(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_lightconv_lm') as data_dir: create_dummy_data(data_dir) preprocess_lm_data(data_dir) train_language_model( data_dir, 'lightconv_lm', ['--add-bos-token'], run_validation=True, ) eval_lm_main(data_dir) generate_main(data_dir, [ '--task', 'language_modeling', '--sample-break-mode', 'eos', '--tokens-per-sample', '500', ])
Example #20
Source File: test_binaries.py From fairseq with MIT License | 5 votes |
def test_fconv_self_att_wp(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_fconv_self_att_wp') as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) config = [ '--encoder-layers', '[(128, 3)] * 2', '--decoder-layers', '[(128, 3)] * 2', '--decoder-attention', 'True', '--encoder-attention', 'False', '--gated-attention', 'True', '--self-attention', 'True', '--project-input', 'True', '--encoder-embed-dim', '8', '--decoder-embed-dim', '8', '--decoder-out-embed-dim', '8', '--multihead-self-attention-nheads', '2' ] train_translation_model(data_dir, 'fconv_self_att_wp', config) generate_main(data_dir) # fusion model os.rename(os.path.join(data_dir, 'checkpoint_last.pt'), os.path.join(data_dir, 'pretrained.pt')) config.extend([ '--pretrained', 'True', '--pretrained-checkpoint', os.path.join(data_dir, 'pretrained.pt'), '--save-dir', os.path.join(data_dir, 'fusion_model'), ]) train_translation_model(data_dir, 'fconv_self_att_wp', config)
Example #21
Source File: test_binaries.py From fairseq with MIT License | 5 votes |
def test_eval_bleu(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory('test_eval_bleu') as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) train_translation_model(data_dir, 'fconv_iwslt_de_en', [ '--eval-bleu', '--eval-bleu-print-samples', '--eval-bleu-remove-bpe', '--eval-bleu-detok', 'space', '--eval-bleu-args', '{"beam": 4, "min_len": 10}', ])
Example #22
Source File: test_descriptors_cpython.py From Clean-code-in-Python with MIT License | 5 votes |
def test_method_unbound_fails(self): instance = MyClass1() capture = io.StringIO() with redirect_stdout(capture): Method("External call")(instance, "first", "second") result = capture.getvalue() self.assertIsNotNone(self.pattern.match(result), repr(result)) with self.assertRaises(TypeError): instance.method("first", "second")
Example #23
Source File: test_binaries_gpu.py From fairseq with MIT License | 5 votes |
def test_quantization(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory("test_quantization") as data_dir: create_dummy_data(data_dir) preprocess_lm_data(data_dir) # tests both scalar and iterative PQ quantization _quantize_language_model(data_dir, "transformer_lm")
Example #24
Source File: test_binaries_gpu.py From fairseq with MIT License | 5 votes |
def test_memory_efficient_fp16(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory("test_memory_efficient_fp16") as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) train_translation_model( data_dir, "fconv_iwslt_de_en", ["--memory-efficient-fp16"] ) generate_main(data_dir)
Example #25
Source File: test_binaries_gpu.py From fairseq with MIT License | 5 votes |
def test_fp16(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory("test_fp16") as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) train_translation_model(data_dir, "fconv_iwslt_de_en", ["--fp16"]) generate_main(data_dir)
Example #26
Source File: test_train.py From fairseq with MIT License | 5 votes |
def test_load_no_checkpoint(self): with contextlib.redirect_stdout(StringIO()): trainer, epoch_itr = get_trainer_and_epoch_itr(1, 150, 0, 0) trainer.get_train_iterator = MagicMock(return_value=epoch_itr) self.patches['os.path.isfile'].return_value = False _, epoch_itr = checkpoint_utils.load_checkpoint(self.args_mock, trainer) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertEqual(epoch_itr.epoch, 1) self.assertEqual(epoch_itr.iterations_in_epoch, 0) self.assertEqual(next(itr)['net_input']['src_tokens'][0].item(), 0)
Example #27
Source File: test_train.py From fairseq with MIT License | 5 votes |
def test_load_full_checkpoint(self): with contextlib.redirect_stdout(StringIO()): trainer, epoch_itr = get_trainer_and_epoch_itr(2, 150, 300, 150) trainer.get_train_iterator = MagicMock(return_value=epoch_itr) _, epoch_itr = checkpoint_utils.load_checkpoint(self.args_mock, trainer) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertEqual(epoch_itr.epoch, 3) self.assertEqual(epoch_itr.iterations_in_epoch, 0) self.assertEqual(next(itr)['net_input']['src_tokens'][0].item(), 0)
Example #28
Source File: compat.py From conllu with MIT License | 5 votes |
def redirect_stdout(target): original = sys.stdout sys.stdout = target yield sys.stdout = original
Example #29
Source File: test_train.py From fairseq with MIT License | 5 votes |
def test_load_partial_checkpoint(self): with contextlib.redirect_stdout(StringIO()): trainer, epoch_itr = get_trainer_and_epoch_itr(2, 150, 200, 50) trainer.get_train_iterator = MagicMock(return_value=epoch_itr) _, epoch_itr = checkpoint_utils.load_checkpoint(self.args_mock, trainer) self.assertEqual(epoch_itr.epoch, 2) self.assertEqual(epoch_itr.iterations_in_epoch, 50) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertEqual(epoch_itr.epoch, 2) self.assertEqual(epoch_itr.iterations_in_epoch, 50) self.assertEqual(next(itr)['net_input']['src_tokens'][0].item(), 50) self.assertEqual(epoch_itr.iterations_in_epoch, 51) for _ in range(150 - 52): next(itr) self.assertEqual(epoch_itr.iterations_in_epoch, 149) self.assertTrue(itr.has_next()) next(itr) self.assertFalse(itr.has_next()) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertTrue(itr.has_next()) self.assertEqual(epoch_itr.epoch, 3) self.assertEqual(epoch_itr.iterations_in_epoch, 0)
Example #30
Source File: compat.py From conllu with MIT License | 5 votes |
def capture_print(func, args=None): f = StringIO() with redirect_stdout(f): if args: func(args) else: func() return f.getvalue()