Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the pythonic way to wrap several functions in the same with statements

I am using the Python library, Fabric, to do some remote server maintenance. Fabric automatically outputs all of the responses to remote and local commands unless you wrap the command in a couple with statements. Like so, on a local machine,

with settings(warn_only='true'):
    with hide('running', 'stdout', 'stderr', 'warnings'):
        output = local("uname -a", True)

or like this on a remote machine:

with settings(warn_only='true'):
    with hide('running', 'stdout', 'stderr', 'warnings'):
        output = run("uname -a")

I am writing a long and complex task and find myself repeating those two with statements over and over again. I want to write a function called _mute() to prevent that repetition. It would let me do something like this:

def _mute(fabric_cmd, args):
    with settings(warn_only='true'):
        with hide('running', 'stdout', 'stderr', 'warnings'):
            output = fabric_cmd(args)
    return output

def some_remote_task():
    # Run a remote task silently
    _mute(remote, 'uname -a')

def some_local_task():
    # Run a local task silently
    _mute(local, 'uname -a', True)

I've looked into some solutions and know that "eval" could do this for me. But every page I read about eval suggests that it's almost always a bad idea because of security issues. I looked into partials, but I couldn't figure out how to make an argument in my _mute function callable. I'm guessing there's a higher level Python concept I'm missing here. What's the pythonic way to go about doing this? Thanks for any direction you might be able to provide.

like image 794
bryan kennedy Avatar asked Sep 03 '12 16:09

bryan kennedy


People also ask

What does it mean to wrap a function?

A wrapper function is a subroutine (another word for a function) in a software library or a computer program whose main purpose is to call a second subroutine or a system call with little or no additional computation.

What is a wrapper in programming?

In the context of software engineering, a wrapper is defined as an entity that encapsulates and hides the underlying complexity of another entity by means of well-defined interfaces.

Which feature in Python allow us to wrap another function in order to extend the behavior of the wrapped function without permanently modifying it?

Decorators allow us to wrap another function in order to extend the behaviour of the wrapped function, without permanently modifying it.

How do you wrap code in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.


1 Answers

The better solution would be for you to build your own context manager; by far the easiest way would be to use the contextlib.contextmanager decorator:

from contextlib import contextmanager

@contextmanager
def _mute():
    with settings(warn_only='true'):
        with hide('running', 'stdout', 'stderr', 'warnings'):
            yield

Then use _mute as a context manager:

def some_remote_task():
    # Run a remote task silently
    with _mute():
        output = remote("uname -a")

This is a lot more compact and readable than having to retype the two larger context manager lines and has the added advantage that now you can run multiple commands in that same context.

As for your question; you can easily apply arbitrary arguments to a given function using the *args syntax:

def _mute(fabric_cmd, *args):
    with settings(warn_only='true'):
        with hide('running', 'stdout', 'stderr', 'warnings'):
            return fabric_cmd(*args)

def some_remote_task():
    # Run a remote task silently
    output = _mute(remote, 'uname -a')

See *args and **kwargs? for more information on the *args arbitrary argument lists tricks.

like image 130
Martijn Pieters Avatar answered Sep 20 '22 00:09

Martijn Pieters