How to write clean class or multithread Python database code

Viewed 33

I'm working on a code which needs to compare XML output from a device to SQLite (temporary, will move to MSSQL or Postgres) database for 200+ commands. If database has no entries then it should write it. To begin with, I wrote basic function in Python 3.x to brute force which does sequential compare and it works. I need to step up the game and write more elegant code and need your help. Multithreaded, Class, anything to speed up the execution as it might serve large audience? Can you please give me an example how would you do this? I may need to add GUI in the future so need provision for that as well.

  1. Main Function

    For every command in CSV file, below function fetches XML data from db for two releases, compares it and if not present, then fetches XML for a release from device writes it to database.

     def xmldiff(ip,usr,passwd,ver1,ver2,):
         for item in data:
            result1 = readfromdb(ver1,item)
            if not result1:
               rawxml = fetch_xml_data(ip,ver1,item)
               insertdb(ver1,item,rawxml)
    
            result2 = readfromdb(ver2,item)
            if not result2:
               rawxml = fetch_xml_data(ip,ver2,item)
               insertdb(ver2,item,rawxml)
    
            comparedbdata(result1,result2)
    
  2. Fetches from db for every release.

    def readfromdb(version,command):
         c.execute("SELECT value from test WHERE ostype=? and version=? and command=?",(ostype,version,command))
         datafromdb = c.fetchall()
    
  3. Get XML output from device

    def fetch_xml_data(ip,usr,pwd,command):
       stdin, stdout, stderr = ssh.exec_command(command)
       return(stdout)
    
  4. Insert into DB

    def insertdb(version,command,value):
        c.execute("INSERT INTO test (version,command,value) VALUES (?,?,?)", (version,command,value))
    
  5. Compare two command output from DB

    def comparedbdata(result1,result2):
        diff1 = [name1 for name1 in result1 if name1 not in result2]
        diff2 = [name2 for name2 in result2 if name2 not in result1]
    
0 Answers
Related