0

I am writing a Python script which will ssh into a box(using parakimo) and run a command. I wanted to make a object for the stdout/stderr and print out the output.

  1. Am I passing the list correctly into the class? and am I handing the list correctly in the class?

  2. I want to utilize the __str__ method so I can just print(object). How can I do this correctly?

  3. I intend to use 2 instances of this class because I will be doing 2 separate sss.exec_command(command). Am I wasting memory by wrapping this function in a class? I wanted to utilize the OO to be efficient, organized, and try to utilize python's OO features.
class read_log:
    def __init__(self, exit_status, *tuple_list):
        self.exit_status = exit_status
        self.tuple_list = tuple_list
        if exit_status:
            output = stderr.readlines()
        else:
            output =  stdout.readlines()
            del output[0]
            del output[-1]
    def __str__(self) 
        return ''.join(output)    

def create():
    stdin, stdout, stderr=ssh.exec_command("/home/one/script.sh")
    exit_status = stdout.channel.recv_exit_status()
    x = readlog(exit_status, stdin, stdout, stderr)
    print(x)

1 Answer 1

1

You capture the stdin, stdout and stderr inputs as one catch-all argument, but then try to refer to those names again in the __init__ function.

Just make them separate parameters:

class read_log:
    def __init__(self, exit_status, stdin, stdout, stderr):
        self.exit_status = exit_status
        self.tuple_list = (stdin, stdout, stderr)

Use *args syntax when you need to allow for a variable number of arguments, not for catching a fixed number of arguments that you then otherwise not use. I stored the 3 arguments as a tuple onto your instance anyway, in case you are using self.tuple_list elsewhere.

You need to store output on the instance:

if exit_status:
    self.output = stderr.readlines()
else:
    self.output =  stdout.readlines()
    del self.output[0]
    del self.output[-1]

and in __str__ refer to that instance attribute:

def __str__(self) 
    return ''.join(self.output)

The latter del statements can also be replaced with a slice:

if exit_status:
    self.output = stderr.readlines()
else:
    self.output =  stdout.readlines()[1:-1]
Sign up to request clarification or add additional context in comments.

2 Comments

I keep getting <__main__.ReadLog instance at 0x7f39fd648878>. Why is this? By the way, I changed the class name to ReadLog instead of read_log. Also, what exactly is std* objects?
That is the repr() representation if your instance. Use print or str() to see your __str__ method at work, or define a __repr__ method to override the result of repr(). The std* objects are file streams, the default ('standard' or 'std') input, output and error streams all processes on UNIX systems have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.