Skip to content

Instantly share code, notes, and snippets.

@zakandrewking
Created April 24, 2017 20:35
Show Gist options
  • Save zakandrewking/a460fa8b7a70d58420d3f7096c792b19 to your computer and use it in GitHub Desktop.
Save zakandrewking/a460fa8b7a70d58420d3f7096c792b19 to your computer and use it in GitHub Desktop.
import contextlib
import os
import sys
# Suppress stdout http://stackoverflow.com/a/17753573
class DummyFile(object):
def write(self, x): pass
@contextlib.contextmanager
def no_stdout(std_channel, dest_filename):
old_std_channel = os.dup(std_channel.fileno())
dest_file = open(dest_filename, 'a')
os.dup2(dest_file.fileno(), std_channel.fileno())
yield
os.dup2(old_std_channel, std_channel.fileno())
dest_file.close()
with no_stdout(sys.stdout, 'file.txt'):
me_nlp = ME_NLP1(me, growth_key='mu')
muopt, basis, _, _ = me_nlp.bisectmu(precision=1e-6)
with no_stdout(sys.stdout, os.devnull):
me_nlp = ME_NLP1(me, growth_key='mu')
muopt, basis, _, _ = me_nlp.bisectmu(precision=1e-6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment