Best Python code snippet using grail_python
compose.py
Source:compose.py
1import ruamel.yaml2import json3import os4in_file = 'docker-compose.yml'5out_file = 'output.json'6test_passed = []7test_failed = []8try:9 yaml = ruamel.yaml.YAML(typ='safe')10 with open(in_file) as fpi:11 cpy = yaml.load(fpi)12 with open(out_file, 'w') as fpo:13 json.dump(cpy, fpo, indent=2)14 with open('output.json') as json_file:15 data = json.load(json_file)16 os.remove("output.json")17except:18 print("docker-compose.yml missing or invalid")19 exit(84)20# TEST VERSION21if data == None or "version" not in data.keys() or data["version"] != '3':22 test_failed.append("Bad version")23else:24 test_passed.append("Good version")25# services general26if data == None or "services" not in data.keys() or data["services"] == None:27 test_failed.append("no services found")28else :29 #================================# POLL SERVICES #===============================#30 if data == None or "poll" not in data["services"].keys() or data["services"]["poll"] == None:31 test_failed.append("no services poll")32 else:33 poll = data["services"]["poll"]34 ## BUILD ##35 if "build" not in poll.keys() or poll["build"] == None:36 test_failed.append("poll build")37 elif poll["build"] == "./poll" or poll["build"] == "poll/":38 test_passed.append("poll build")39 else:40 test_failed.append("poll build")41 ## RESTART ##42 if "restart" not in poll.keys() or poll["restart"] == None:43 test_failed.append("poll restart")44 elif poll["restart"] == "always" or poll["restart"] == "on-failure":45 test_passed.append("poll restart")46 else:47 test_failed.append("poll restart")48 ## ports ##49 if "ports" not in poll.keys() or poll["ports"] == None:50 test_failed.append("poll ports")51 elif poll["ports"][0] == "5000:80":52 test_passed.append("poll ports")53 else:54 test_failed.append("poll ports")55 ## network ##56 if "networks" not in poll.keys() or poll["networks"] == None:57 test_failed.append("poll networks")58 elif poll["networks"][0] == "poll-tier" and len(poll["networks"]) == 1:59 test_passed.append("poll networks")60 else:61 test_failed.append("poll networks")62 ## environment ##63 if "environment" not in poll.keys() or poll["environment"] == None:64 test_failed.append("poll environment")65 else:66 test_passed.append("poll environment")67 if "depends_on" not in poll.keys() or poll["depends_on"] == None:68 test_failed.append("poll depends_on")69 elif poll["depends_on"][0] == "redis" and len(poll["depends_on"]) == 1:70 test_passed.append("poll depends_on")71 else:72 test_failed.append("poll depends_on")73 #================================# REDIS SERVICES #===============================#74 if data == None or "redis" not in data["services"].keys():75 test_failed.append("no services redis")76 else:77 redis = data["services"]["redis"]78 # image ##79 if "image" not in redis.keys() or redis["image"] == None:80 test_failed.append("redis image")81 elif redis["image"].startswith("redis"):82 test_passed.append("redis image")83 else:84 test_failed.append("redis image")85 ## RESTART ##86 if "restart" not in redis.keys() or redis["restart"] == None:87 test_failed.append("redis restart")88 elif redis["restart"] == "always" or redis["restart"] == "on-failure":89 test_passed.append("redis restart")90 else:91 test_failed.append("redis restart")92 ## expose or ports ##93 if "ports" in redis.keys() and redis["ports"] != None:94 if redis["ports"][0].startswith("6379"):95 test_passed.append("redis expose")96 else:97 test_failed.append("redis expose")98 elif "expose" in redis.keys() and redis["expose"] != None:99 if redis["expose"][0].startswith("6379"):100 test_passed.append("redis expose")101 else:102 test_failed.append("redis expose")103 else:104 test_failed.append("redis expose")105 ## network ##106 if "networks" not in redis.keys() or redis["networks"] == None:107 test_failed.append("redis networks")108 elif "poll-tier" in redis["networks"] and "back-tier" in redis["networks"] and len(redis["networks"]) == 2:109 test_passed.append("redis networks")110 else:111 test_failed.append("redis networks")112 ## depends_on ##113 if "depends_on" not in redis.keys() or redis["depends_on"] == None:114 test_failed.append("redis depends_on")115 elif redis["depends_on"][0] == "db":116 test_passed.append("redis depends_on")117 else:118 test_failed.append("redis depends_on")119 #================================# worker SERVICES #===============================#120 if data == None or "worker" not in data["services"].keys():121 test_failed.append("no services worker")122 else:123 worker = data["services"]["worker"]124 # build ##125 if "build" not in worker.keys() or worker["build"] == None:126 test_failed.append("worker build")127 elif worker["build"] == "./worker" or worker["build"] == "worker/":128 test_passed.append("worker build")129 else:130 test_failed.append("worker build")131 ## RESTART ##132 if "restart" not in worker.keys() or worker["restart"] == None:133 test_failed.append("worker restart")134 elif worker["restart"] == "always" or worker["restart"] == "on-failure":135 test_passed.append("worker restart")136 else:137 test_failed.append("worker restart")138 ## network ##139 if "networks" not in worker.keys() or worker["networks"] == None:140 test_failed.append("worker networks")141 elif worker["networks"][0] == "back-tier" and len(worker["networks"]) == 1:142 test_passed.append("worker networks")143 else:144 test_failed.append("worker networks")145 ## depends_on ##146 if "depends_on" not in worker.keys() or worker["depends_on"] == None:147 test_failed.append("worker depends_on")148 elif "db" in worker["depends_on"] and "redis" in worker["depends_on"] and len(worker["depends_on"]) == 2:149 test_passed.append("worker depends_on")150 else:151 test_failed.append("worker depends_on")152 ## environment ##153 if "environment" not in worker.keys() or worker["environment"] == None:154 test_failed.append("worker environment")155 else:156 test_passed.append("worker environment")157 #================================# result SERVICES #===============================#158 if data == None or "result" not in data["services"].keys() or data["services"]["result"] == None:159 test_failed.append("no services result")160 else:161 result = data["services"]["result"]162 ## BUILD ##163 if "build" not in result.keys() or result["build"] == None:164 test_failed.append("result build")165 elif result["build"] == "./result" or result["build"] == "result/":166 test_passed.append("result build")167 else:168 test_failed.append("result build")169 ## RESTART ##170 if "restart" not in result.keys() or result["restart"] == None:171 test_failed.append("result restart")172 elif result["restart"] == "always" or result["restart"] == "on-failure":173 test_passed.append("result restart")174 else:175 test_failed.append("result restart")176 ## ports ##177 if "ports" not in result.keys() or result["ports"] == None:178 test_failed.append("result ports")179 elif result["ports"][0] == "5001:80":180 test_passed.append("result ports")181 else:182 test_failed.append("result ports")183 ## network ##184 if "networks" not in result.keys() or result["networks"] == None:185 test_failed.append("result networks")186 elif result["networks"][0] == "result-tier" and len(result["networks"]) == 1:187 test_passed.append("result networks")188 else:189 test_failed.append("result networks")190 ## environment ##191 if "environment" not in result.keys() or result["environment"] == None:192 test_failed.append("result environment")193 else:194 test_passed.append("result environment")195 ## depends_on ##196 if "depends_on" not in result.keys() or result["depends_on"] == None:197 test_failed.append("result depends_on")198 elif result["depends_on"][0] == "db" and len(result["depends_on"]) == 1:199 test_passed.append("result depends_on")200 else:201 test_failed.append("result depends_on")202 #================================# db SERVICES #===============================#203 if data == None or "db" not in data["services"].keys() or data["services"]["db"] == None:204 test_failed.append("no services db")205 else:206 db = data["services"]["db"]207 ## image ##208 if "image" not in db.keys() or db["image"] == None:209 test_failed.append("db image")210 elif db["image"].startswith("postgres"):211 test_passed.append("db image")212 else:213 test_failed.append("db image")214 ## RESTART ##215 if "restart" not in db.keys() or db["restart"] == None:216 test_failed.append("db restart")217 elif db["restart"] == "always" or db["restart"] == "on-failure":218 test_passed.append("db restart")219 else:220 test_failed.append("db restart")221 ## network ##222 if "networks" not in db.keys() or db["networks"] == None:223 test_failed.append("db networks")224 elif "result-tier" in db["networks"] and "back-tier" in db["networks"] and len(db["networks"]) == 2:225 test_passed.append("db networks")226 else:227 test_failed.append("db networks")228 ## environment ##229 if "environment" not in db.keys() or db["environment"] == None:230 test_failed.append("db environment")231 else:232 test_passed.append("db environment")233 ## depends_on ##234 if "volumes" not in db.keys() or db["volumes"] == None:235 test_failed.append("db volumes")236 elif "db-data:/var/lib/postgresql/data" in db["volumes"] and any([line.startswith("./schema.sql:/docker-entrypoint-initdb.d") for line in db["volumes"]]) and len(db["volumes"]) == 2:237 test_passed.append("db volumes")238 else:239 test_failed.append("db volumes")240#================================# Volumes #===============================#241if data == None or "volumes" not in data.keys() or data["volumes"] == None:242 test_failed.append("no volumes")243else:244 volumes = data["volumes"]245 if "db-data" not in volumes.keys():246 test_failed.append("wrong volumes")247 else:248 test_passed.append("good volumes")249#================================# networks #===============================#250if data == None or "networks" not in data.keys() or data["networks"] == None:251 test_failed.append("no networks")252else:253 networks = data["networks"]254 if "poll-tier" in networks.keys() and "result-tier" in networks.keys() and "back-tier" in networks.keys() and len(networks) == 3:255 test_passed.append("good networks")256 else:257 test_failed.append("wrong networks")258if (len(test_failed) == 0):259 print("GG")260else:261 for elem in test_failed:262 print(elem)263 if len(test_passed) == 0:...
check_utils.py
Source:check_utils.py
1# -------------------------2#3# Functions to check angles and calculated inertial data4#5# --------------------------67import numpy as np8910def check_angles(angle_array):11 # Checks ground truth angles1213 # Initialize Outputs14 test_passed = True15 angle_list = []1617 grad_thresh = np.array([5, 3, 3])1819 # Calculate gradient20 grad = np.gradient(angle_array, axis=0)2122 # Find changepoints23 cp_arr = np.abs(grad) > grad_thresh2425 if cp_arr[:, 0].any():26 test_passed = False27 angle_list.append('Flex')2829 if cp_arr[:, 1].any():30 test_passed = False31 angle_list.append('Add')3233 if cp_arr[:, 2].any():34 test_passed = False35 angle_list.append('Rot')3637 return test_passed, angle_list, np.stack(np.nonzero(cp_arr))383940def check_acc(acc_array):41 # Checks generated accelerometer data4243 # Initialize Outputs44 test_passed = True45 axis_list = []4647 # Thresholds for each axis48 grad_threshold = np.array([20, 15, 15])4950 # Calculate gradient51 grad = np.gradient(acc_array, axis=0)5253 bool_array = np.abs(grad) > grad_threshold5455 if (bool_array[:, 0]).any():56 test_passed = False57 axis_list.append('X')5859 if (bool_array[:, 1]).any():60 test_passed = False61 axis_list.append('Y')6263 if (bool_array[:, 2]).any():64 test_passed = False65 axis_list.append('Z')6667 return test_passed, axis_list, np.stack(np.nonzero(bool_array))686970def check_gyr(gyr_array):71 # Checks generated gyroscope data7273 # Initialize Outputs74 test_passed = True75 axis_list = []7677 # Thresholds for each axis78 gyr_threshold = np.array([12.5, 12.5, 15])7980 bool_array = np.abs(gyr_array) > gyr_threshold8182 if (bool_array[:, 0]).any():83 test_passed = False84 axis_list.append('X')8586 if (bool_array[:, 1]).any():87 test_passed = False88 axis_list.append('Y')8990 if (bool_array[:, 2]).any():91 test_passed = False92 axis_list.append('Z')93
...
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!!