Refacturing the code, how do you make it beautiful?



  • Started writing Botta for Telegram, who's gonna help out with athletic business. The main idea is to put every computer in the base and plus the user that's currently sitting behind the computer. I'd like to start on the server so that I can an endless cycle gather an infu and announce changes.

    Write a computer data collection class wmi♪ Here's an example.

    import wmi
    import config
    

    class Computer:

    def hdd_info(ip):
        hdd = []
        wql  = 'SELECT * FROM Win32_DiskDrive'
        try:
            c = wmi.WMI(ip, user=config.admin, password=config.password)
            for computer in c.query(wql):
                hdd.append(computer.Model)
                hdd.append(computer.SerialNumber)
                hdd.append(computer.Size)
            return hdd
        except:
            return hdd
    
    
    def motherboard_info(ip):
        motherboard = []
        wql  = 'SELECT * FROM Win32_BaseBoard'
        try:
            c = wmi.WMI(ip, user=config.admin, password=config.password)
            for computer in c.query(wql):
                motherboard = [computer.Manufacturer, computer.Product]
            return motherboard
        except:
            return motherboard
    

    I'm starting to work with all this. This is a function. Main:

    ip_subnet = ["192.168.4.", "192.168.5."]

    def main():
    i = 55
    for ip in range(len(ip_subnet)):
    while i <= 255:
    ip_adr = ip_subnet[ip] + str(i)
    response = subprocess.call(["ping", "-n", "1", "-w", "200", ip_adr])
    if(response == 0):
    try:
    wql = 'SELECT * FROM Win32_computerSystem'
    c = wmi.WMI(ip_adr, user=config.admin, password=config.password)
    for computer in c.query(wql):
    check_in_db_if_ping(computer.DNSHostName, ip_adr, computer.UserName)
    except:
    pass
    else:
    check_in_db_if_not_ping(ip_adr)
    i += 1
    i = 0

    This is where I'm taking over all our subnets, and I'm getting the primary data. If the data are received, I'll transfer them to the following function, which checks whether the computer is in the database. check_in_db_if_ping♪ This function is very long, and I'm not bringing it all.

    def check_in_db_if_ping(hostname, ip, user=None):
    cursor.execute('SELECT IP FROM Computers WHERE IP = ?', (ip,))
    check_ip = cursor.fetchall()
    cursor.execute('SELECT HostName FROM Computers WHERE HostName = ?', (hostname,))
    check_hostname = cursor.fetchall()
    conn.commit()
    if len(check_ip) == 0 and len(check_hostname) == 0:
    add_to_db(hostname, ip, user)

    user = None It's worth it because the computer can be switched on, and the current user's gone off and then, at the request, there's none, there's kind of a time out there if the user's not activated for a while.

    In the pounds check_in_db_if_ping I'm checking if there's an IP address and HostName There don't exist in the database, so I'm starting to put it in the base and I'm transferring it. add_to_db

    Here's the thing. prioritywhat is the function of adding a computer to the base:

    def add_to_db(hostname, ip, user = None):
    motherboard = Computer.motherboard_info(ip)
    ram = Computer.ram_info(ip)
    cursor.execute('INSERT INTO Computers (HostName, IP, Status, OnTime, OffTime, MotherBoard, RAM) VALUES (?,?,?,?,?,?,?)',
    (hostname, ip, "Online", str(time),"-",motherboard[0] + " " + motherboard[1], str(ram[0])))
    last_id = cursor.lastrowid
    conn.commit()
    if user != None:
    cursor.execute('INSERT INTO User (ComputerId, Login) VALUES (?,?)', (last_id, user))
    conn.commit()

    I don't like this job: it's a long request and a data glue, but I don't know how to simplify it.

    Can I get all the data on the computer on the sheet sometime and just put it on the base? Is there any comments on the code?



  • I recommend that your attention be drawn to two books that are considered to be important enough to develop clean code writing skills:

    (1) "Clean code" by Robert Martin. https://www.ozon.ru/context/detail/id/5011068/

    (2) De factoring Faulera http://www.ozon.ru/context/detail/id/1308678/

    In them, you will find answers to your questions about how to decompose a function that can be taken into a separate class, why the 100-strong method is bad and how to fight it.

    Don't ignore the recommission, but really roll up the book, and you'll have an understanding of how to de facto.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2