samedi 3 novembre 2018

Python Design Pattern Suggestion

I need help para to beautify this code :) The method definesAction will call a Class, based on the args. There is some way to generalizing this piece of code, taking into account that the Class's are similar. Thanks in advance

Main Class

def defineAction(args):

if args.classabc is not None:
    for host in config.getList('ABC', 'hosts'):
        class_abc = ClassABC(config.getConfigs('ABC', host), args.version[0], user, password)
        class_abc.action(args.classabc)

if args.classxyz is not None:
    for host in config.getList('XYZ', 'hosts'):
        class_xyz = ClassXYZ(config.getConfigs('XYZ', host), args.version[0], user, password)
        class_xyz.action(args.classxyz)

(...)

def main():

parser.add_argument('--classabc', choices=['cmd'])
parser.add_argument('--classxyz', choices=['cmd'])
(...)

defineAction(args)

SubClasses

class ClassABC:

def __init__(self, configs, user, password):
    self.hostConfigs = configs['host']
    self.host_username = user
    self.host_password = password

def a_method(self):
    //This Method is equal in all subclasses
def b_method(self):
    //This Method is different all subclasses

a_method()
b_method
args = parser.parse_args()

Aucun commentaire:

Enregistrer un commentaire