Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8487541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:16:31+00:00 2026-06-10T21:16:31+00:00

I am using the Python library, Fabric , to do some remote server maintenance.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:16:32+00:00Added an answer on June 10, 2026 at 9:16 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the python library SGMLParser to parse some html. I encounter an html
I am using Python Imaging Library to do some processing, and I'd like to
I am using Python's AWeber API (https://github.com/aweber/AWeber-API-Python-Library), and I frequently get these exceptions. I
I am attempting to get synonyms for a word using the python library NLTK.
I'm using Python's suds library which tries to fetch xml.xsd over the network. Unfortunately,
I am using the Python avro library . I want to send an avro
I'm using a open-source Python library in my project. This library logs a lot
I'm trying to modify a python library that I downloaded and am using. But
I am using DICOM with Python and Pydicom library, when I have used .dcm
I'm using the boto library in Python to connect to DynamoDB. The following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.