How to do it...

  1. We'll define an abstract Command class. The other commands will be defined as subclasses. We'll push the subprocess processing into this class definition to simplify the subclasses:

        import subprocess 
        class Command: 
            def execute(self, options): 
                self.command = self.create_command(options) 
                results = subprocess.run(self.command, 
                    check=True, stdout=subprocess.PIPE) 
                self.output = results.stdout 
                return self.output 
            def create_command(self, options): 
                return ['echo', self.__class__.__name__, repr(self.options)] 

The execute() method works by first creating the OS-level command to execute. Each subclass will provide distinct rules for the commands which are wrapped. Once the command has been built, then the run() function of the ...

Get Modern Python Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.