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 8833711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:50:43+00:00 2026-06-14T08:50:43+00:00

As a system administrator I find myself many times writing scripts that call command

  • 0

As a system administrator I find myself many times writing scripts that call command via subprocess. Sometimes, I don’t want the commands to actually execute, and I want just to see what would be executed. Hence, my codes were full in lines like this:

alaroffcmd = 'someBinary -h %s' %someHostName
...
if options.tstmd:       
   print alaroffcmd
else:
   chctxt = sp.Popen(alamoffcmd,shell=True, stdout=sp.PIPE) 
   ...

I was thinking that a ‘testmode’ would be very usefull.

As an example of usage:

lsp=nPopen('ls -l',shell=True, stdout=sp.PIPE, testmode=True)

Will just print the command to be issued. This seems redundant maybe, but in real life, I sometimes call subprocess with some very complex command, which are decided based on conditions that are determined in the script (above there is an example with someHostName)

I used this as an example how to extend a function by overriding it’s init method,. Here is how I extended subprocess.Popen, to fit my needs:

import subprocess as sp

class nPopen(sp.Popen):
    def __init__(self, args, bufsize=0, executable=None,
                 stdin=None, stdout=None, stderr=None,
                 preexec_fn=None, close_fds=False, shell=False,
                 cwd=None, env=None, universal_newlines=False,
                 startupinfo=None, creationflags=0,testmode=False):

        if testmode: 
            print args
            return None

     p = sp.Popen.__init__(self,args, bufsize, executable,
                 stdin, stdout, stderr,
                 preexec_fn, close_fds, shell,
                 cwd, env, universal_newlines,
                 startupinfo, creationflags)

        return p

This works as I expect it, but since I have never extended a class by overriding its __init__ method, I was wondering about the correctness of this, or in other words:
Is there a more Pythonic way to do this?
Should I use super for better Python3 compatibility ?

  • 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-14T08:50:45+00:00Added an answer on June 14, 2026 at 8:50 am

    I wouldn’t use a subclass at all, since it sounds like you don’t want to change the functionality of the Popen object. It sounds like you just want to change the functionality of its constructor.

    I would create a dummy Popen class that just prints out its arguments. (This is written using Python 3.x style, but it’s easy enough to translate to Python 2.x.)

    class DummyPopen(object):
        def __init__(self, *args, **kw):
            print('Popen({})'.format(
                ', '.join([repr(arg) for arg in args] +
                          ['{}={!r}'.format(*pair) for pair in kw.items()])))
            self.returncode = 1
    
        def wait(self):
            return 1
    
        # et cetera
    

    You can then trigger use of the dummy class however you want.

    def Popen(*args, testmode=False, **kw):
        if testmode:
            return DummyPopen(*args, **kw)
        return subprocess.Popen(*args, **kw)
    

    Or even:

    if testmode:
        Popen = DummyPopen
    else:
        Popen = subprocess.Popen
    

    This takes advantage of the “duck typing” conventions of Python — any object which has the same interface as a Popen object is functionally indistinguishable from a Popen object, as long as you don’t abuse isinstance() too much.

    Note that both of these methods allow you to import Popen from the module.

    from mymodule import Popen
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a content management system for updates where the administrator can
System.Type contains an UnderlyingSystemType property. MSDN states that it: Indicates the type provided by
My system administrator and i would like to run asp.net mvc2 and asp.net mvc3
There are Database Administrators (DBA) and Unix|Windows System Administrators. There are likely soon to
I would like to make a simple notice / news system for website administrators.
Background I use JScript (Microsoft's ECMAScript implementation) for a lot of Windows system administration
my line in gridview <asp:Label ID=Label2 runat=server Text='<%# new System.Text.StringBuilder(<a href=\/Administration/Pages/EditPage/?pageID=).Append(Eval(PageID)).Append(\>).Append(Eval(Name)).Append(</a>).ToString() %>'></asp:Label> fails with
System.err.println() in Eclipse doesn't print in red. I checked preferences > Run/Debug > console
System: rails 3.2.3 1.9.2p320 ubuntu 12.04 32 bit I am trying to split up
/System/Library/Services/ImageCaptureService.app - what is it? Evernote uses it for importing the images from scanners

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.