Best Python code snippet using robotframework-ioslibrary_python
data_functions.py
Source:data_functions.py
2import models3from pathlib import Path4import json5def write_asset_types_to_json():6 asset_types = db_connect.query_all(models.AssetTypes)7 type_dict = {}8 for i in asset_types:9 type_dict[i.id] = i.asset_type10 data_folder = Path("static/data/all_assoc_types.json")11 with open(data_folder, 'w') as fp:12 json.dump(type_dict, fp, indent=4)13 return type_dict14def get_associated_solutions(solution_id):15 s = db_connect.query_one_db(model=models.Solutions, column=models.Solutions.id, v=solution_id)16 assoc_solutions = {}17 if s.associated_solutions:18 for r in s.associated_solutions:19 asid = db_connect.query_one_db(model=models.Solutions, column=models.Solutions.id, v=r)20 assoc_solutions[asid.id] = asid.solution_title21 else:22 assoc_solutions = []23 return assoc_solutions24def one_solution_asset_types(solution_id):25 # This needs fixed. This should really call a function that just returns a dictionary of all asset types without writing json again26 all_asset_types = write_asset_types_to_json()27 solution_asset_types = {}28 associated_solutions = {}29 solution = db_connect.query_one_db(model=models.Solutions, column=models.Solutions.id, v=solution_id)30 for t in solution.associated_asset_types:31 a_type = db_connect.query_one_db(model=models.AssetTypes, column=models.AssetTypes.id, v=t)32 solution_asset_types[t] = a_type.asset_type33 if solution.associated_solutions:34 for a in solution.associated_solutions:35 asid = db_connect.query_one_db(model=models.Solutions, column=models.Solutions.id, v=a)36 associated_solutions[a] = asid.solution_title37 all_solution_data = {38 'all_asset_types': all_asset_types,39 'solution_asset_types': solution_asset_types,40 'associated_solutions': associated_solutions,41 }42 data_folder = Path("static/data/one_solution_data.json")43 with open(data_folder, 'w') as fp:44 json.dump(all_solution_data, fp, indent=4)45 return solution_asset_types46def one_solution_data(solution_id):47 solution_data = {}48 solution = db_connect.query_one_db(model=models.Solutions, column=models.Solutions.id, v=solution_id)49 solution_data["Solution_id"] = solution.id50 solution_data["Title"] = solution.solution_title51 solution_data["Steps"] = solution.steps52 step_number = []53 for s in solution.steps:54 step_number.append(s)55 if len(step_number) > 0:56 solution_data["Highest_Step"] = max(step_number)57 else:58 solution_data["Highest_Step"] = '0'59 solution_data["Associated_Asset_Types"] = solution.associated_asset_types60 solution_data["Primary_Asset_Type"] = solution.primary_asset_type61 data_folder = Path("static/data/one_solution.json")62 with open(data_folder, 'w') as fp:63 json.dump(solution_data, fp, indent=4)64 return None65def write_all_solution_data():66 all_solutions = db_connect.query_all(models.Solutions)67 solution_data = []68 for s in all_solutions:69 main_asset_type = db_connect.query_one_db(model=models.AssetTypes, column=models.AssetTypes.id, v=s.primary_asset_type)70 solution_data.append(71 {72 'id': s.id,73 'title': s.solution_title,74 'primary_asset_type': main_asset_type.asset_type75 }76 )77 data_folder = Path("static/data/all_solution_data.json")78 with open(data_folder, 'w') as fp:79 json.dump(solution_data, fp, indent=4)80 return None81def write_one_asset_data_to_json(asset_id):82 asset_data = {}83 asset = db_connect.query_one_db(model=models.Assets, column=models.Assets.id, v=asset_id)84 asset_data["Asset_Id"] = asset.id85 asset_data["Asset_Type"] = asset.asset_type86 asset_data["Manufacturer"] = asset.manufacturer87 asset_data["Model"] = asset.model88 asset_data["Serial_No"] = asset.serial_no89 asset_data["Dia_Asset_Tag"] = asset.dia_asset_tag90 asset_data["Description"] = asset.description91 asset_data["Department"] = asset.department92 asset_data["Location"] = asset.location93 # data_folder = Path("static/data/one_asset.json")94 # with open(data_folder, 'w') as fp:95 # json.dump(asset_data, fp, indent=4)96 return asset_data97def write_asset_data_to_json(asset_id=0):98 if asset_id != 0:99 asset_data = {}100 all_types = []101 all_manufacturers = []102 all_departments = []103 for t in db_connect.query_all(models.AssetTypes):104 all_types.append(105 {106 'id': t.id,107 'asset_type': t.asset_type108 }109 )110 for m in db_connect.query_all(models.Manufacturers):111 all_manufacturers.append(112 {113 'id': m.id,114 'manufacturer': m.manufacturer115 }116 )117 for d in db_connect.query_all(models.Departments):118 all_departments.append(119 {120 'id': d.id,121 'department': d.department122 }123 )124 asset_data['all_types'] = all_types125 asset_data['all_manufacturers'] = all_manufacturers126 asset_data['all_departments'] = all_departments127 asset_data['this_asset'] = write_one_asset_data_to_json(asset_id)128 data_folder = Path("static/data/all_asset_data.json")129 with open(data_folder, 'w') as fp:130 json.dump(asset_data, fp, indent=4)131 return None132 else:133 return None134# This makes a dictionary of { Manufacturer: { Asset_Type: [ Model no. list ] }, Manufacturer: { Asset_Type: [ Model no. list ] }}135def asset_models_by_manufacturer():136 all_manufacturers = db_connect.query_all(models.Manufacturers)137 all_asset_types = db_connect.query_all(models.AssetTypes)138 model_by_manufacturer = {}139 for m in all_manufacturers:140 asset_type_list = {}141 for t in all_asset_types:142 model_numbers_for_type = []143 q = db_connect.query_distinct_for_models(m.id, t.id)144 for model_no in q:145 model_numbers_for_type.append(model_no.model)146 asset_type_list[t.id] = model_numbers_for_type147 model_by_manufacturer[m.id] = asset_type_list148 data_folder = Path("static/data/model_by_manufacturer.json")149 with open(data_folder, 'w') as fp:150 json.dump(model_by_manufacturer, fp, indent=4)151 return model_by_manufacturer152def manufacturers_as_dict():153 all_manufacturers = db_connect.query_all(models.Manufacturers)154 manufacturer_dict = {}155 for m in all_manufacturers:156 manufacturer_dict[m.id] = m.manufacturer157 return manufacturer_dict158def departments_as_dict():159 all_departments = db_connect.query_all(models.Departments)160 dept_dict = {}161 for d in all_departments:162 dept_dict[d.id] = d.department163 return dept_dict164def software_companies_as_dict():165 all_software_companies = db_connect.query_all(models.SoftwareCompanies)166 software_co_dict = {}167 for s in all_software_companies:168 software_co_dict[s.id] = s.software_company169 return software_co_dict170def software_names_as_dict():171 all_software = db_connect.query_all(models.Software)172 software_name_dict = {}173 for s in all_software:174 software_name_dict[s.id] = s.software_name...
04_mysql_base.py
Source:04_mysql_base.py
1# coding=utf-82import mysql.connector34mydb = mysql.connector.connect(5 host="192.168.249.143",6 user="root",7 passwd="3282",8 database="python4"9)1011if __name__ == "__main__":12 print(mydb)13 # 1. å建æ°æ®åº14 mycorsor = mydb.cursor()15 # mycorsor.execute("create database python4")1617 # 2. å建表18 mycorsor.execute("use python4")19 # mycorsor.execute("create table person(name varchar(40),age int)")20 mycorsor.execute("CREATE TABLE sites (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), url VARCHAR(255))")21 # mycorsor.execute("CREATE TABLE sites (name VARCHAR(255), url VARCHAR(255))")2223 # 3. è¾åºæææ°æ®åºå表24 print("=================== 3. è¾åºæææ°æ®åºå表 ===================")25 mycorsor.execute("SHOW DATABASES")26 for database in mycorsor:27 print(database)2829 # 4. æ¥çæ°æ®è¡¨æ¯å¦å·²åå¨30 print("=================== 4. æ¥çæ°æ®è¡¨æ¯å¦å·²åå¨ ===================")31 mycorsor.execute("SHOW TABLES")32 for table in mycorsor:33 print("tpye(table) = {} table = {}".format(type(table), table))3435 # 5. 主é®è®¾ç½®36 # mycorsor.execute("ALTER TABLE sites ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")3738 # 6. æå
¥æ°æ®39 sql = "INSERT INTO sites (name,url) VALUES (%s,%s)"40 values = ("baidu", "www.baidu.com")41 mycorsor.execute(sql, values)42 mydb.commit() # æ交äºå¡43 insert_id = mycorsor.lastrowid44 print("insert into data success")45 print("1æ¡æ°æ®æå
¥æå,ID:{}".format(insert_id))4647 # 7. æ¹éæå
¥48 sql = "INSERT INTO sites (name,url) VALUES (%s,%s)"49 values = [("Google", "www.google.com"),50 ("github", "www.github.com"),51 ("Tencent", "www.qq.com"),52 ("Apple", "www.apple.com")]53 mycorsor.executemany(sql, values)54 mydb.commit()55 count = mycorsor.rowcount56 print("æå
¥{}æ°æ®æå...".format(count))5758 # 8. æ¥è¯¢æ°æ®59 print("=================== 8. æ¥è¯¢æ°æ® ===================")60 mycorsor.execute("SELECT * FROM sites")61 # è·åææè®°å½62 qyery_all = mycorsor.fetchall()63 for row in qyery_all:64 print(row)6566 print("=================== 9. ä¹å¯ä»¥è¯»åæå®çå段æ°æ® ===================")67 mycorsor.execute("SELECT name, url FROM sites")68 query_all = mycorsor.fetchall()69 for row in query_all:70 print(row)7172 print("=================== 10. å¦ææ们åªæ³è¯»åä¸æ¡æ°æ®ï¼å¯ä»¥ä½¿ç¨ fetchone() ===================")73 # mycorsor.execute("SELECT name,url FROM sites")74 # row = mycorsor.fetchone()75 # print(row)7677 print("=================== 11. where æ¡ä»¶è¯å¥ ===================")78 mycorsor.execute("SELECT * FROM sites WHERE name='Google'")79 query_all = mycorsor.fetchall()80 for row in query_all:81 print(row)8283 print("=================== 12. where æ¡ä»¶è¯å¥ ,ä¹å¯ä»¥ä½¿ç¨éé
符 % ===================")84 sql = "SELECT * FROM sites WHERE url LIKE '%oo%'"85 mycorsor.execute(sql)86 query_all = mycorsor.fetchall()87 for row in query_all:88 print(row)8990 print("=================== 12. 为äºé²æ¢æ°æ®åºæ¥è¯¢åç SQL 注å
¥çæ»å»ï¼æ们å¯ä»¥ä½¿ç¨ %s å ä½ç¬¦æ¥è½¬ä¹æ¥è¯¢çæ¡ä»¶ï¼ ===================")91 sql = "SELECT * FROM sites WHERE name = %s"92 quer_name = ("Apple",)93 mycorsor.execute(sql, quer_name)94 query_all = mycorsor.fetchall()95 for row in query_all:96 print(row)9798 print("=================== 13.æåº ===================")99 sql = "SELECT * FROM sites ORDER BY name "100 mycorsor.execute(sql)101 query_all = mycorsor.fetchall()102 for row in query_all:103 print(row)104105 print("=================== 13.æ name å段åæ¯çéåºæåº ===================")106 sql = "SELECT * FROM sites ORDER BY name DESC"107 mycorsor.execute(sql)108 query_all = mycorsor.fetchall()109 for row in query_all:110 print(row)111112 print("=================== 14.å¦ææ们è¦è®¾ç½®æ¥è¯¢çæ°æ®éï¼å¯ä»¥éè¿ LIMIT è¯å¥æ¥æå® ===================")113 # sql = "SELECT * FROM sites LIMIT 3"114 sql = "SELECT * FROM sites ORDER BY RAND() LIMIT 5"115 mycorsor.execute(sql)116 query_all = mycorsor.fetchall()117 for row in query_all:118 print(row)119120 # éæºæ¥è¯¢121 print("=================== 14.éæºæ¥è¯¢1æ¡ ===================")122 # sql = "SELECT * FROM sites WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM sites )ORDER BY id LIMIT 1;"123 sql = "SELECT * FROM sites WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM sites))) ORDER BY id LIMIT 1;"124 mycorsor.execute(sql)125 query_all = mycorsor.fetchall()126 for row in query_all:127 print(row)128129 print("=================== 15.ä»ç¬¬äºæ¡å¼å§è¯»åå 3 æ¡è®°å½ï¼ ===================")130 sql = "SELECT * FROM sites LIMIT 3 OFFSET 1"131 mycorsor.execute(sql)132 query_all = mycorsor.fetchall()133 for row in query_all:134 print(row)135136 print("=================== 16.å é¤è®°å½ ===================")137 sql = "DELETE FROM sites WHERE name = 'apple'"138 mycorsor.execute(sql)139 mydb.commit()140 delete_count = mycorsor.rowcount141 print("{}æ¡è®°å½å é¤".format(delete_count))142143 print("=================== 17.为äºé²æ¢æ°æ®åºæ¥è¯¢åç SQL 注å
¥çæ»å»ï¼æ们å¯ä»¥ä½¿ç¨ %s å ä½ç¬¦æ¥è½¬ä¹å é¤è¯å¥çæ¡ä»¶ï¼ ===================")144 sql = "DELETE FROM sites WHERE name = %s"145 values = ("baidu",)146 mycorsor.execute(sql, values)147 mydb.commit()148 delete_count = mycorsor.rowcount149 print("{}æ¡è®°å½å é¤".format(delete_count))150151 print("=================== 18.æ´æ°è¡¨æ°æ® ===================")152 sql = "UPDATE sites SET NAME = 'QQ' WHERE name = 'Tencent'"153 mycorsor.execute(sql)154 mydb.commit()155 update_count = mycorsor.rowcount156 print("{}æ¡è®°å½è¢«æ´æ°".format(update_count))157158 print("=================== 18.æ´æ°è¡¨æ°æ® ===================")159 sql = "UPDATE sites SET NAME = %s WHERE name = %s"160 values = ("Github", "github")161 mycorsor.execute(sql, values)162 mydb.commit()163 update_count = mycorsor.rowcount164 print("{}æ¡è®°å½è¢«æ´æ°".format(update_count))165166 print("=================== 19.å é¤è¡¨ ===================")167 sql = "DROP TABLE IF EXISTS sites"
...
threadlib.py
Source:threadlib.py
...22### END OF MODULE IMPORTS ###2324### START OF GLOBAL VARIABLES DECLARATION ###25# --------------------------------------------------------------- #26ALL_SERVERS_FROM_DB = ServersManipulator(Server().query_all())27ALL_SERVERS_CHANGES = ServerChangeLogManipulator(ServerChanges().query_all())28ALL_USERS_FROM_DB = UsersManipulator(User().query_all())29ALL_USER_CHANGES = UserChangeLogManipulator(UserChanges().query_all())30ALL_RPMS_FROM_DB = RPMsManipulator(RPM().query_all())31ALL_RPM_CHANGES = RPMChangeLogManipulator(RPMChanges().query_all())32# --------------------------------------------------------------- #33### END OF GLOBAL VARIABLES DECLARATION ###343536### START OF UTILITY FUNCTIONS DECLARATION37# --------------------------------------------------------------- #38def set_global_vars():39 while True:40 global ALL_SERVERS_FROM_DB, ALL_SERVERS_CHANGES, \41 ALL_USERS_FROM_DB, ALL_USER_CHANGES, \42 ALL_RPMS_FROM_DB, ALL_RPM_CHANGES43 ALL_SERVERS_FROM_DB = ServersManipulator(Server().query_all())44 ALL_SERVERS_CHANGES = ServerChangeLogManipulator(ServerChanges().query_all())45 ALL_USERS_FROM_DB = UsersManipulator(User().query_all())46 ALL_USER_CHANGES = UserChangeLogManipulator(UserChanges().query_all())47 ALL_RPMS_FROM_DB = RPMsManipulator(RPM().query_all())48 ALL_RPM_CHANGES = RPMChangeLogManipulator(RPMChanges().query_all())49 time.sleep(60)50# --------------------------------------------------------------- #51### END OF UTILITY FUNCTIONS DECLARATION525354def start_dbquery_thread():55 set_vars_thread = threading.Thread(target=set_global_vars)56 # set_vars_thread.name = time.time()
...
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!!