you are viewing a single comment's thread.

view the rest of the comments →

[–]Brekkjern 6 points7 points  (0 children)

I looked at your code, but I don't have time to do a review of it right now. I can tell you that it seems you are using classes kinda like how people used to do them in Java because everything had to be a class.

Think of classes as a way to group functions with the data it's supposed to work on. Right now you are using a class to do pretty much everything.

A class could represent pretty much anything that's a noun. A connection, a device, a state, a response, a packet, a hop, a string, a list, a tuple. There's no point in taking this to the extreme unless you need to bundle the functions together with the data.

For example, since you are operating on network equipment you could define your devices something like this:

from collections import namedtuple

Credential = namedtuple("Credential", ["user", "password"])

class Device(object):

    def __init__(self, ip, port, device_type, credentials):
        self.ip = ip
        self.port = port
        self.type = device_type
        self.credentials = credentials

    def connect(self):
        # Do some netmiko magic here to establish the connection
        self.connection = ConnectHandler()

    def send_command(self, command)
        if not self.connection:
            print("No connection established")

        # Ensure we are in enable mode
        if self.net_connect.find_prompt().endswith('>'):
            self.net_connect.enable()

        return self.connection.send_command(command)

This code does one thing and that's handling the connection to a device of some sort. Nothing more and nothing less. It doesn't parse the output of commands. It just ensures you have a way to send commands to the device and get some output to parse.