Best Python code snippet using avocado_python
test_domtree.py
Source:test_domtree.py
...28 self.assertEqual(m, 1.0)29 self.assertEqual(M, 8.0)30 dt.apply_leaf(l0)31 l1 = dt.get_leaf(1)32 self.assertEqual(l1.get_domains(0), {0: RealDomain(-math.inf, 2)})33 self.assertEqual(l1.get_domain(0, 0), RealDomain(-math.inf, 2))34 self.assertTrue( l1.is_reachable(0, 0, 0))35 self.assertTrue( l1.is_reachable(0, 0, 1))36 self.assertTrue( l1.is_reachable(0, 0, 3))37 self.assertTrue( l1.is_reachable(0, 0, 4))38 self.assertFalse(l1.is_reachable(0, 0, 2))39 #self.assertFalse(l1.is_reachable(0, 0, 5)) // unreachable because 0, 2 blocked40 #self.assertFalse(l1.is_reachable(0, 0, 6)) // ^41 m, M = l1.get_tree_bounds(0, 0)42 self.assertEqual(m, 1.0)43 self.assertEqual(M, 2.0)44 l2 = dt.get_leaf(2)45 self.assertEqual(l2.get_domains(0), {0: RealDomain(2, math.inf)})46 self.assertEqual(l2.get_domain(0, 0), RealDomain(2, math.inf))47 self.assertTrue( l2.is_reachable(0, 0, 0))48 self.assertFalse(l2.is_reachable(0, 0, 1))49 #self.assertFalse(l2.is_reachable(0, 0, 3)) // unreachable because 0, 1 blocked50 #self.assertFalse(l2.is_reachable(0, 0, 4)) // ^51 self.assertTrue( l2.is_reachable(0, 0, 2))52 self.assertTrue( l2.is_reachable(0, 0, 5))53 self.assertTrue( l2.is_reachable(0, 0, 6))54 m, M = l2.get_tree_bounds(0, 0)55 self.assertEqual(m, 4.0)56 self.assertEqual(M, 8.0)57 # with root_domain58 dt = DomTree(at, {0: RealDomain(0, 2)})59 self.assertEqual(dt.get_root_domain(0, 0), RealDomain(0, 2))60 l0 = dt.get_leaf(0)61 self.assertEqual(l0.get_domains(0), {0: RealDomain(0, 2)})62 self.assertEqual(l0.get_domain(0, 0), dt.get_root_domain(0, 0))63 self.assertTrue( l0.is_reachable(0, 0, 0))64 self.assertTrue( l0.is_reachable(0, 0, 1))65 self.assertTrue( l0.is_reachable(0, 0, 3))66 self.assertTrue( l0.is_reachable(0, 0, 4))67 self.assertFalse(l0.is_reachable(0, 0, 2))68 #self.assertTrue(l0.is_reachable(0, 0, 5)) # hidden by 0, 269 #self.assertTrue(l0.is_reachable(0, 0, 6))70 m, M = l0.get_tree_bounds(0, 0)71 self.assertEqual(m, 1.0)72 self.assertEqual(M, 2.0)73 l0.find_best_split()74 self.assertEqual(l0.get_best_split(), (0, LtSplit(0, 1)))75 self.assertEqual(l0.score, 2)76 self.assertEqual(l0.balance, 0)77 dt.apply_leaf(l0)78 self.assertEqual(dt.get_leaf(1).get_domains(0), {0: RealDomain(0, 1)})79 self.assertEqual(dt.get_leaf(2).get_domains(0), {0: RealDomain(1, 2)})80 def test_serialize(self):81 at = AddTree.read("tests/models/xgb-calhouse-hard.json")82 dt = DomTree(at, {})83 self.assertEqual(dt.num_instances(), 1)84 l0 = dt.get_leaf(0);85 nbytes1 = len(pickle.dumps(l0))86 print("num of kb", nbytes1)87 dt = DomTree([(at, {}), (at, {0: RealDomain(0, 1)})])88 self.assertEqual(dt.num_instances(), 2)89 l0 = dt.get_leaf(0);90 nbytes2 = len(pickle.dumps(l0))91 print("num of kb", nbytes2)92 self.assertLess(nbytes2, 2*nbytes1)93 l0.find_best_split()94 print(l0.get_best_split())95 l0c = pickle.loads(pickle.dumps(l0))96 self.assertEqual(l0c.num_instances(), 2)97 self.assertEqual(l0.get_domains(0), {})98 self.assertEqual(l0c.get_domains(0), {})99 self.assertNotEqual(l0.get_domains(1), {})100 self.assertNotEqual(l0c.get_domains(1), {})101 self.assertEqual(l0c.get_domains(1), l0.get_domains(1))102 #self.assertEqual(nbytes2, len(pickle.dumps(l0c)))103 self.assertLess(abs(nbytes2-len(pickle.dumps(l0c))), 100)104 print(l0c.get_best_split())105 self.assertNotEqual(l0c.get_best_split(), None)106 self.assertEqual(l0.get_best_split(), l0c.get_best_split())107 self.assertEqual(l0.score, l0c.score)108 self.assertEqual(l0.balance, l0c.balance)109 def test_get_domains1(self):110 at = AddTree()111 at.base_score = 10112 t = at.add_tree();113 t.split(t.root(), 0, 2)114 t.split( t.left(t.root()), 0, 1)115 t.split(t.right(t.root()), 1) # bool split116 t.set_leaf_value( t.left( t.left(t.root())), 1.0)117 t.set_leaf_value(t.right( t.left(t.root())), 2.0)118 t.set_leaf_value( t.left(t.right(t.root())), 4.0)119 t.set_leaf_value(t.right(t.right(t.root())), 8.0)120 dt = DomTree(at, {0: RealDomain(0, 2)})121 l0 = dt.get_leaf(0)122 l0.find_best_split()123 dt.apply_leaf(l0)124 self.assertEqual(dt.get_leaf(1).get_domains(0), {0: RealDomain(0, 1)})125 self.assertEqual(dt.get_leaf(2).get_domains(0), {0: RealDomain(1, 2)})126 def test_get_domains2(self):127 at = AddTree()128 at.base_score = 10129 t = at.add_tree();130 t.split(t.root(), 0)131 t.split( t.left(t.root()), 1, 2) # bool split132 t.split(t.right(t.root()), 1, 3)133 t.set_leaf_value( t.left( t.left(t.root())), 1.0)134 t.set_leaf_value(t.right( t.left(t.root())), 2.0)135 t.set_leaf_value( t.left(t.right(t.root())), 4.0)136 t.set_leaf_value(t.right(t.right(t.root())), 8.0)137 dt = DomTree(at, {})138 l0 = dt.get_leaf(0);139 l0.find_best_split();140 self.assertEqual(l0.get_best_split(), (0, BoolSplit(0)))141 dt.apply_leaf(l0)142 self.assertEqual(dt.get_leaf(1).get_domains(0), {0: BoolDomain(True)})143 self.assertEqual(dt.get_leaf(2).get_domains(0), {0: BoolDomain(False)})144 def test_two_trees(self):145 at = AddTree()146 at.base_score = 10147 t = at.add_tree();148 t.split(t.root(), 0, 2)149 t.split( t.left(t.root()), 0, 1)150 t.split(t.right(t.root()), 0, 3)151 t.set_leaf_value( t.left( t.left(t.root())), 0.1)152 t.set_leaf_value(t.right( t.left(t.root())), 0.2)153 t.set_leaf_value( t.left(t.right(t.root())), 0.3)154 t.set_leaf_value(t.right(t.right(t.root())), 0.4)155 t = at.add_tree();156 t.split(t.root(), 0, 3)157 t.split( t.left(t.root()), 1, 1.2)158 t.split(t.right(t.root()), 1, 3.3)159 t.split(t.right(t.right(t.root())), 2)160 t.set_leaf_value( t.left( t.left(t.root())), 0.1)161 t.set_leaf_value(t.right( t.left(t.root())), 0.2)162 t.set_leaf_value( t.left(t.right(t.root())), 0.3)163 t.set_leaf_value( t.left(t.right(t.right(t.root()))), 0.5)164 t.set_leaf_value(t.right(t.right(t.right(t.root()))), 0.6)165 print(at)166 dt = DomTree(at, {})167 l0 = dt.get_leaf(0)168 l0.find_best_split()169 self.assertEqual(l0.get_best_split(), (0, LtSplit(0, 3.0)))170 self.assertEqual(l0.score, 9)171 self.assertEqual(l0.balance, 1)172 l0.mark_unreachable(0, 1, 2)173 l0.find_best_split()174 self.assertEqual(l0.get_best_split(), (0, LtSplit(0, 3.0)))175 self.assertEqual(l0.score, 6)176 self.assertEqual(l0.balance, 4)177 l0 = dt.get_leaf(0)178 l0.find_best_split()179 dt.apply_leaf(l0)180 def test_l1(l1):181 self.assertFalse(l1.is_reachable(0, 0, 6))182 self.assertFalse(l1.is_reachable(0, 1, 2))183 #self.assertFalse(l1.is_reachable(0, 1, 5)) # hidden by 1, 2184 #self.assertFalse(l1.is_reachable(0, 1, 6)) # ^185 #self.assertFalse(l1.is_reachable(0, 1, 7)) # ^186 #self.assertFalse(l1.is_reachable(0, 1, 8)) # ^187 self.assertTrue( l1.is_reachable(0, 0, 0))188 self.assertTrue( l1.is_reachable(0, 0, 1))189 self.assertTrue( l1.is_reachable(0, 1, 1))190 l1 = dt.get_leaf(1)191 test_l1(l1)192 def test_l2(l2):193 self.assertFalse(l2.is_reachable(0, 0, 1))194 #self.assertFalse(l2.is_reachable(0, 0, 3)) # hidden by 0, 1195 #self.assertFalse(l2.is_reachable(0, 0, 4)) # ^196 self.assertFalse(l2.is_reachable(0, 0, 5))197 self.assertFalse(l2.is_reachable(0, 1, 1))198 #self.assertFalse(l2.is_reachable(0, 1, 3)) # hidden by 1, 1199 #self.assertFalse(l2.is_reachable(0, 1, 4)) # ^200 self.assertTrue( l2.is_reachable(0, 0, 0))201 self.assertTrue( l2.is_reachable(0, 0, 6))202 self.assertTrue( l2.is_reachable(0, 1, 2))203 l2 = dt.get_leaf(2)204 test_l2(l2)205 x = pickle.dumps(l1)206 l1c = pickle.loads(pickle.dumps(l1))207 test_l1(l1c)208 x = pickle.dumps(l2)209 l2c = pickle.loads(pickle.dumps(l2))210 test_l2(l2c)211 self.assertEqual(l1c.get_domains(0), l1.get_domains(0))212 self.assertEqual(l2c.get_domains(0), l2.get_domains(0))213 def test_merge(self):214 at = AddTree()215 at.base_score = 10216 t = at.add_tree();217 t.split(t.root(), 0, 2)218 t.split( t.left(t.root()), 0, 1)219 t.split(t.right(t.root()), 1)220 t.set_leaf_value( t.left( t.left(t.root())), 0.1)221 t.set_leaf_value(t.right( t.left(t.root())), 0.2)222 t.set_leaf_value( t.left(t.right(t.root())), 0.3)223 t.set_leaf_value(t.right(t.right(t.root())), 0.4)224 dt = DomTree([(at, {}), (at, {})])225 l0_0 = dt.get_leaf(0)226 l0_1 = dt.get_leaf(0)227 l0_0.mark_unreachable(0, 0, 1)228 l0_0.mark_unreachable(1, 0, 2)229 self.assertFalse(l0_0.is_reachable(0, 0, 1))230 self.assertTrue( l0_0.is_reachable(0, 0, 2))231 self.assertFalse(l0_0.is_reachable(1, 0, 2))232 self.assertTrue( l0_0.is_reachable(1, 0, 1))233 l0_1.mark_unreachable(0, 0, 2)234 l0_1.mark_unreachable(1, 0, 1)235 self.assertFalse(l0_1.is_reachable(0, 0, 2))236 self.assertTrue( l0_1.is_reachable(0, 0, 1))237 self.assertFalse(l0_1.is_reachable(1, 0, 1))238 self.assertTrue( l0_1.is_reachable(1, 0, 2))239 l0_m = DomTreeLeaf.merge([l0_0, l0_1]);240 self.assertFalse(l0_m.is_reachable(0, 0, 2))241 self.assertFalse(l0_m.is_reachable(0, 0, 1))242 self.assertFalse(l0_m.is_reachable(1, 0, 2))243 self.assertFalse(l0_m.is_reachable(1, 0, 1))244 def test_multi_instance(self):245 at = AddTree()246 at.base_score = 10247 t = at.add_tree();248 t.split(t.root(), 0, 2)249 t.split( t.left(t.root()), 0, 1)250 t.split(t.right(t.root()), 1)251 t.set_leaf_value( t.left( t.left(t.root())), 0.1)252 t.set_leaf_value(t.right( t.left(t.root())), 0.2)253 t.set_leaf_value( t.left(t.right(t.root())), 0.3)254 t.set_leaf_value(t.right(t.right(t.root())), 0.4)255 dt = DomTree([(at, {0: RealDomain(0, 2)}), (at, {})])256 self.assertEqual(dt.num_instances(), 2)257 l0 = dt.get_leaf(0)258 l0.find_best_split()259 self.assertEqual(l0.get_best_split(), (1, LtSplit(0, 2.0)))260 self.assertEqual(l0.score, 4)261 self.assertEqual(l0.balance, 0)262 dt.apply_leaf(l0)263 l1 = dt.get_leaf(1)264 l1.find_best_split()265 self.assertEqual(l1.get_best_split(), (0, LtSplit(0, 1.0)))266 dt.apply_leaf(l1)267 l3, l4 = dt.get_leaf(3), dt.get_leaf(4)268 self.assertEqual(l3.get_domains(0), {0: RealDomain(0, 1)})269 self.assertEqual(l4.get_domains(0), {0: RealDomain(1, 2)})270 self.assertEqual(l3.get_domains(1), {0: RealDomain(-math.inf, 2)})271 self.assertEqual(l4.get_domains(1), {0: RealDomain(-math.inf, 2)})272 #def _test_calhouse(self): # Prune removed273 # at = AddTree.read("tests/models/xgb-calhouse-hard.json")274 # dt = DomTree(at, {})275 # l0 = dt.get_leaf(0)276 # l0.find_best_split()277 # _, fid, sval = l0.get_best_split()278 # split_score = l0.score279 # split_balance = l0.balance280 # 281 # dt.split(l0)282 # num_leafs = at.num_leafs();283 # ftypes = AddTreeFeatureTypes(at)284 # doms_l = [RealDomain() for i in ftypes.feat_ids()]285 # doms_l[fid].hi = sval286 # doms_r = [RealDomain() for i in ftypes.feat_ids()]287 # doms_r[fid].lo = sval288 # at_l = at.prune(doms_l)289 # at_r = at.prune(doms_r)290 # num_leafs_l = at_l.num_leafs()291 # num_leafs_r = at_r.num_leafs()292 # score = num_leafs - num_leafs_l + num_leafs - num_leafs_r293 # balance = abs(num_leafs_l - num_leafs_r)294 # self.assertEqual(score, split_score)295 # self.assertEqual(balance, split_balance)296 # #for i in range(len(at)):297 # # p = TreePlot()298 # # p.g.attr(label=f"X{fid} split at {sval}")299 # # p.add_subspace(at[i], dt.get_leaf(1))300 # # p.add_subspace(at[i], dt.get_leaf(2))301 # # p.render(f"/tmp/plots/test2-{i}")302 def test_img_multisplit(self):303 def plt(at, ll, lr, instance, split):304 for i in range(len(at)):305 p = TreePlot()306 idl = ll.domtree_leaf_id()307 idr = lr.domtree_leaf_id()308 p.g.attr(label=f"X{split.feat_id} split at {split.split_value} (ids={idl}, {idr})")309 p.add_domtree_leaf(instance, at[i], ll)310 p.add_domtree_leaf(instance, at[i], lr)311 p.render(f"/tmp/plots/multisplit-{idl}-{idr}-{i}")312 at = AddTree.read("tests/models/xgb-img-easy.json")313 dt = DomTree(at, {})314 l0 = dt.get_leaf(0)315 l0.find_best_split()316 b0 = l0.get_best_split()317 print("l0", b0)318 dt.apply_leaf(l0)319 l1 = dt.get_leaf(1)320 l2 = dt.get_leaf(2)321 l1.find_best_split()322 l2.find_best_split()323 b1 = l1.get_best_split()324 b2 = l2.get_best_split()325 plt(at, l1, l2, *b0)326 print("l1", b1)327 print("l2", b2)328 self.assertNotEqual(b0, b1)329 self.assertNotEqual(b0, b2)330 dt.apply_leaf(l1)331 l3 = dt.get_leaf(3)332 l4 = dt.get_leaf(4)333 l3.find_best_split()334 l4.find_best_split()335 b3 = l3.get_best_split()336 b4 = l4.get_best_split()337 plt(at, l3, l4, *b1)338 print("l3", b3)339 print("l4", b4)340 self.assertNotEqual(b1, b3)341 self.assertNotEqual(b1, b4)342 def test_bin_mnist(self):343 at = AddTree.read("tests/models/xgb-mnist-bin-yis1-easy.json")344 dt = DomTree(at, {})345 l0 = dt.get_leaf(0)346 l0.find_best_split()347 b0 = l0.get_best_split()348 print("l0", b0)349 self.assertEqual(at[0].get_split(0), b0[1])350 dt.apply_leaf(l0)351 print(dt.tree())352 l1 = dt.get_leaf(1)353 l2 = dt.get_leaf(2)354 self.assertEqual(l1.get_domains(0), {b0[1].feat_id: BoolDomain(True)})355 self.assertEqual(l2.get_domains(0), {b0[1].feat_id: BoolDomain(False)})356 self.assertTrue(l1.is_reachable(0, 0, 1))357 self.assertFalse(l1.is_reachable(0, 0, 2))358 self.assertTrue(l2.is_reachable(0, 0, 2))359 self.assertFalse(l2.is_reachable(0, 0, 1))360if __name__ == "__main__":361 #z3.set_pp_option("rational_to_decimal", True)362 #z3.set_pp_option("precision", 3)363 #z3.set_pp_option("max_width", 130)...
test_save_visited_domains.py
Source:test_save_visited_domains.py
1from pytest import mark, fixture2pytestmark = mark.asyncio3@fixture4def get_domains(redis_client, domains_key):5 async def _get_domains(key=domains_key):6 result = await redis_client.execute('SMEMBERS', key)7 return sorted(result)8 return _get_domains9@fixture10def get_bins(redis_client, timestamps_key):11 async def _get_bins(bins=timestamps_key):12 result = await redis_client.execute(13 'ZRANGE', bins, 0, -1, 'WITHSCORES',14 )15 return result16 return _get_bins17@mark.parametrize('domain', [18 'ya.ru', '127.0.0.1', 'example', 'поÑÑа.ÑÑ',19])20async def test_save_domain(21 domain,22 test_storage, visit_timestamp, domains_key, get_domains, get_bins,23):24 domains = [domain]25 await test_storage.save_visited_domains(26 domains=domains,27 visit_timestamp=visit_timestamp,28 )29 assert domains == await get_domains()30 assert [domains_key, str(visit_timestamp)] == await get_bins()31async def test_save_domains(32 test_storage, visit_timestamp, domains_key, domains, get_domains,33 get_bins,34):35 await test_storage.save_visited_domains(36 domains=domains,37 visit_timestamp=visit_timestamp,38 )39 assert domains == await get_domains()40 assert [domains_key, str(visit_timestamp)] == await get_bins()41async def test_several_calls(42 test_storage, domains, get_domains, get_bins, visit_timestamp,43 domains_key44):45 await test_storage.save_visited_domains(46 domains=domains,47 visit_timestamp=visit_timestamp,48 )49 extra_domains = ['extra.domain']50 extra_visit_time = visit_timestamp + 151 extra_bin = test_storage.domains_key(extra_visit_time)52 await test_storage.save_visited_domains(53 domains=extra_domains,54 visit_timestamp=extra_visit_time,55 )56 assert domains == await get_domains()57 assert extra_domains == await get_domains(extra_bin)58 assert [domains_key, str(visit_timestamp),59 extra_bin, str(extra_visit_time)] == await get_bins()60async def test_empty_list(test_storage, visit_timestamp, get_domains,61 get_bins):62 await test_storage.save_visited_domains(63 domains=[],64 visit_timestamp=visit_timestamp,65 )66 assert [] == await get_domains()67 assert [] == await get_bins()68async def test_save_to_existing_bin(69 test_storage, domains, get_domains, get_bins, visit_timestamp,70 domains_key,71):72 await test_storage.save_visited_domains(73 domains=domains,74 visit_timestamp=visit_timestamp,75 )76 extra_domain = 'extra.domain'77 assert extra_domain not in domains78 domains_plus = domains + [extra_domain, domains[0]]79 await test_storage.save_visited_domains(80 domains=domains_plus,81 visit_timestamp=visit_timestamp,82 )83 expected_domains = sorted(list(set(domains_plus)))84 assert expected_domains == await get_domains()...
main.py
Source:main.py
...6import dcollector.providers.digitalocean as digitalocean7import dcollector.providers.file as local8import dcollector.utils.utils as utils9import dcollector.providers.cycognito as cycognito10def get_domains():11 """12 Pulls domains from the relevant providers and remove dups13 :return:14 """15 return utils.remove_dups(16 aws.get_domains() +17 gcp.get_domains() +18 local.get_domains() +19 prisma.get_domains() +20 digitalocean.get_domains() +21 cycognito.get_domains()22 )23def get_enabled_providers():24 """25 Returns status of providers based on environment variables26 :return:27 """28 return {"aws": aws.is_enabled(), "gcp": gcp.is_enabled(), "dg": digitalocean.is_enabled(),29 "prisma": prisma.is_enabled(), "cycognito": cycognito.is_enabled(), "local file": local.is_enabled()}30def main(providers):31 """32 Creates a file will all the pulled domains33 :return:34 """35 if providers:36 enabled_providers = get_enabled_providers()37 print("Status of environment variables of providers (True = exists, False = not exists):")38 for provider in enabled_providers:39 print(f"{provider} : {enabled_providers[provider]}")40 return 041 # Get records42 domain_list = get_domains()43 44 # save records as json file45 f = open('domains.json', 'w')46 f.write(json.dumps(domain_list))47 print(f' Successfully extracted {len(domain_list)} records')48 return 049def interactive():50 """51 Getting args from user and passing to main52 :return:53 """54 parser = argparse.ArgumentParser(description='Collect DNS records from various DNS and cloud providers.')55 parser.add_argument('-lp', '--list-providers', help='listing loaded providers', action='store_true',56 dest="list_providers")...
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!!