How to use the contextlib2.redirect_stdout function in contextlib2

To help you get started, we’ve selected a few contextlib2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github IuryAlves / TryPython / docker / eval.py View on Github external
def _get_console_out(to_eval, namespace):
    fake_out, fake_err = StringIO(), StringIO()
    console = code.InteractiveConsole(locals=namespace)
    with redirect_stdout(fake_out), redirect_stderr(fake_err):
        for function in namespace.get("functions", []):
            for statement in function.split("\\n"):
                console.push(statement)
        for statement in to_eval.split("\n"):
            if statement:
                console.push(statement)
            else:
                console.push('\n')
    return fake_out.getvalue(), fake_err.getvalue()
github python-cmd2 / cmd2 / cmd2.py View on Github external
def do_help(self, arglist):
        """List available commands with "help" or detailed help with "help cmd"."""
        if arglist:
            # Getting help for a specific command
            funcname = self._func_named(arglist[0])
            if funcname:
                # Check to see if this function was decorated with an argparse ArgumentParser
                func = getattr(self, funcname)
                if func.__dict__.get('has_parser', False):
                    # Function has an argparser, so get help based on all the arguments in case there are sub-commands
                    new_arglist = arglist[1:]
                    new_arglist.append('-h')

                    # Temporarily redirect all argparse output to both sys.stdout and sys.stderr to self.stdout
                    with redirect_stdout(self.stdout):
                        with redirect_stderr(self.stdout):
                            func(new_arglist)
                else:
                    # No special behavior needed, delegate to cmd base class do_help()
                    cmd.Cmd.do_help(self, funcname[3:])
        else:
            # Show a menu of what commands help can be gotten for
            self._help_menu()

contextlib2

Backports and enhancements for the contextlib module

Python-2.0
Latest version published 2 years ago

Package Health Score

76 / 100
Full package analysis

Similar packages