Skip to content

Instantly share code, notes, and snippets.

class ExecutorFactory:
""" The factory class for creating executors"""
registry = {}
""" Internal registry for available executors """
@classmethod
def register(cls, name: str) -> Callable:
def inner_wrapper(wrapped_class: ExecutorBase) -> Callable:
class ExecutorFactory:
""" The factory class for creating executors"""
registry = {}
""" Internal registry for available executors """
@classmethod
def create_executor(cls, name: str, **kwargs) -> 'ExecutorBase':
""" Factory command to create the executor """
executor = LocalExecutor()
stdout, _ = executor.run('ls -ltr')
print(stdout)
class LocalExecutor(ExecutorBase):
def run(self, command: str) -> (str, str):
""" Runs the given command using subprocess """
args = shlex.split(command)
stdout, stderr = subprocess.Popen(args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
@geoffkoh
geoffkoh / factory_executor_base.py
Created January 26, 2020 04:06
Factory pattern using decorators
class ExecutorBase(metaclass=ABCMeta):
""" Base class for an executor """
def __init__(self, **kwargs):
""" Constructor """
pass
@abstractmethod
def run(self, command: str) -> (str, str):
""" Abstract method to run a command """