Best Python code snippet using avocado_python
DataHandler.py
Source:DataHandler.py  
1import pyodbc2import json3import time4from config import config56class DataHandler:7    def __init__(self):89        self.server = config["database"]["hostname"] 10        self.database = config["database"]["database"]11        self.username = config["database"]["username"]12        self.password = config["database"]["password"]13        # self.driver = '/usr/lib/x86_64-linux-gnu/libodbc.so'14        self.driver = '{ODBC Driver 13 for SQL Server}'15        self.connstr = 'DRIVER='+self.driver+';PORT=1433;SERVER='+self.server+';PORT=1433;DATABASE='+self.database+';UID='+self.username+';PWD='+self.password16        print "Try to connect with string: " + self.connstr17        self.conn = pyodbc.connect(self.connstr)18        print "Connecting to server ..."192021        self.add_cluster_info = """ 22                                   INSERT INTO cluster_info 23                                   ([clusterId], [k], [v]) 24                                   VALUES (?,?,?)    25                                """2627        self.add_report = """ insert into report (host_IP,clusterId,role,clientIP,sysId) values  (?, ?, ?, ?, ?) """2829    def AddNode(self, data):30        cursor = self.conn.cursor()31        cursor.execute(self.add_report, data["hostIP"], data["clusterId"], data["role"], data["clientIP"], data["sysId"])32        self.conn.commit()33        cursor.close()343536    def GetNodes(self,role,clusterId):37        cursor = self.conn.cursor()38        query = "SELECT host_IP,clusterId,role, clientIP, time FROM report where role = '"+role+"' and clusterId = '"+clusterId+"' group by sysId "39        query = """ select a.host_IP,a.clusterId,a.role,a.clientIP, a.time from report a40                    left join report b41                    on a.sysId=b.sysId42                    and a.[time] < b.[time]43                    where b.sysId is NULL 44                    and a.[time] >= DATEADD (hour, -1, getdate())45                    and a.clusterId='%s' 46                    and a.role = '%s'47                    """ % (clusterId,role)4849        cursor.execute(query)50        ret = []51        for (hostIP,clusterId,role, clientIP, dtime) in cursor:52            record = {}53            record["hostIP"] = hostIP.strip()54            record["clusterId"] = clusterId.strip()55            record["role"] = role.strip()56            record["clientIP"] = clientIP.strip()57            record["time"] = time.mktime(dtime.timetuple())58            ret.append(record)59        cursor.close()6061        return ret6263    def AddClusterInfo(self, data):64        cursor = self.conn.cursor()65        cursor.execute(self.add_cluster_info, data["clusterId"], data["key"], data["value"])66        self.conn.commit()67        cursor.close()686970    def GetClusterInfo(self,clusterId,key):71        cursor = self.conn.cursor()72        query = ("SELECT TOP 1 [k], [v] FROM cluster_info where [k] = '"+key+"' and [clusterId] = '"+clusterId+"' order by [time] desc")73        cursor.execute(query)74        value = None75        for (k,v) in cursor:76            value = v.strip()77        cursor.close()78        return value        79    8081    def DeleteCluster(self,clusterId):82        cursor = self.conn.cursor()83        query = ("DELETE FROM cluster_info where clusterId = '"+clusterId+"'")84        cursor.execute(query)85        cursor.close()86        self.conn.commit()878889    def Close(self):90        self.conn.close()9192if __name__ == '__main__':93    TEST_INSERT_NODE = False94    TEST_GET_NODE = False95    TEST_INSTER_CLUSTER_INFO = True96    dataHandler = DataHandler()97    98    if TEST_INSERT_NODE:99        jobParams = {}100        jobParams["hostIP"] = "10.10.10.10"101        jobParams["clusterId"] = "1234556"102        jobParams["role"] = "worker"103        jobParams["clientIP"] =  "10.10.10.10"104        jobParams["sysId"] = "ADSCASDcAE!EDASCASDFD"105        106        dataHandler.AddNode(jobParams)107    if TEST_GET_NODE:108        print dataHandler.GetNodes("worker","1234556")109110    if TEST_INSTER_CLUSTER_INFO:111        data = {}112        data["clusterId"] = "1234"113        data["key"] = "key"114        data["value"] = "value"115        dataHandler.AddClusterInfo(data)116117    print dataHandler.GetClusterInfo("1234","key")
...lists_test.py
Source:lists_test.py  
...14def test_append(l, val):15    l.append(val)16   17@print_action_time_decorator18def test_get_node(l, val):19    if type(l) == type(Node()):20        return get_node(l, val)21    return l.index(val)2223def main():24    my_list = Node()25    regular_list = [1]2627    print("********** append value **********")28    print("Regular list:")29    test_append(regular_list, 2)30    print("Linked list:")31    test_append(my_list, 2)32    print()3334    print("********** get node **********")35    print("Regular list:")36    test_get_node(regular_list, 2)37    print("Linked list:")38    test_get_node(my_list, 2)3940    # insert data41    for i in range(5):42        my_list.append(i)4344    temp_list = Node(50)45    assert check_circular_list(my_list) == False, "the list has no circul"46    assert check_lists_merging(my_list, temp_list) == False, "2 differents lists"47    temp_list.nextval = my_list.nextval48    assert check_lists_merging(my_list, temp_list) == True, "margin lists"49    my_list.link_end_to_first()50    assert check_circular_list(my_list) == True, "the list has a circul"5152if __name__ == "__main__":
...TestCostRow.py
Source:TestCostRow.py  
...13    def test_costrow_str(self):14        result = self.cost_row.__str__()15        self.assertEqual(result, '0, 0, 1, 0, 1, 2, 0, 2, 3', "test_costrow_str failed")16        17    def test_get_node(self):18        result = self.cost_row.get_node(1).__str__()...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
