Best Python code snippet using playwright-python
test_client.py
Source:test_client.py
...127 for option, expected in zip(route.options, options):128 assert (option.service, option.min_distance, option.max_distance) == expected129 assert route.weight == weight130def test_routes(copper_client):131 copper_client.set_route('test:helloroute', 'test:helloworld1', 'test:helloworld2')132 assert copper_client.list_routes() == ['test:helloroute']133 routes = copper_client.lookup_route('test:helloroute')134 assert len(routes) == 2135 assert_route_eq(routes[0], [('test:helloworld1', 0, 1), ('test:helloworld1', 2, 2)], 1)136 assert_route_eq(routes[1], [('test:helloworld2', 0, 1), ('test:helloworld2', 2, 2)], 1)137 copper_client.set_route('test:helloroute')138 assert copper_client.list_routes() == []139 assert copper_client.lookup_route('test:helloroute') == []140def test_routed_services(copper_client):141 def handler1(stream):142 stream.write('Hello from handler1')143 def handler2(stream):144 stream.write('Hello from handler2')145 with copper_client.publish('test:hello1', handler1):146 with copper_client.publish('test:hello2', handler2):147 with copper_client.subscribe('test:hello2') as sub:148 # Verify that requests go normally.149 with sub.open() as stream:150 assert stream.read() == 'Hello from handler2'151 # Re-route test:hello2 to point to test:hello1 and verify that152 # requests really go to a different service.153 copper_client.set_route('test:hello2', 'test:hello1')154 with sub.open() as stream:155 assert stream.read() == 'Hello from handler1'156 # Re-route test:hello1 to some non-existant service and since157 # routes always use direct service names test:hello2 should not158 # be affected by this ne wroute.159 copper_client.set_route('test:hello1', 'test:whatever')160 with sub.open() as stream:161 assert stream.read() == 'Hello from handler1'162 # Re-route test:hello2 to some non-existant service and verify163 # that requests don't go to the real service just because the164 # route has a non-existant destination.165 copper_client.set_route('test:hello2', 'test:whatever')166 with pytest.raises(NoRouteError):167 with sub.open() as stream:168 stream.read()169 # Add multiple routes for test:hello2 and verify that randomizer170 # does not consider the non-existant route as a possibility.171 copper_client.set_route('test:hello2', 'test:whatever', 'test:hello1')172 for _ in xrange(32):173 with sub.open() as stream:174 assert stream.read() == 'Hello from handler1'175 # Add a second option for test:hello2 and verify that it is used176 # when the first option cannot be reached.177 copper_client.set_route('test:hello2', ['test:whatever', 'test:hello1'])178 with sub.open() as stream:179 assert stream.read() == 'Hello from handler1'180 # Drop the route and verify that requests go normally again.181 copper_client.set_route('test:hello2')182 with sub.open() as stream:183 assert stream.read() == 'Hello from handler2'184 # Set multiple routes for test:hello2 and verify that randomizer185 # selects all of them at least once.186 counters = defaultdict(int)187 copper_client.set_route('test:hello2', 'test:hello1', 'test:hello2')188 for _ in xrange(32):189 with sub.open() as stream:190 counters[stream.read()] += 1191 assert sorted(counters) == [192 'Hello from handler1',193 'Hello from handler2',194 ]195 # Set a route that has a priority for the first service and196 # verify that requests don't go to the second service.197 copper_client.set_route('test:hello2', ['test:hello1', 'test:hello2'])198 for _ in xrange(32):199 with sub.open() as stream:200 assert stream.read() == 'Hello from handler1'201if os.environ.get('SPEEDTEST', '').strip().lower() in ('1', 'yes', 'true'):202 def test_read_speed(copper_client):203 def handler(stream):204 data = 'x' * 65536205 while True:206 n = stream.send(data)207 with copper_client.publish('test:myservice', handler):208 with copper_client.subscribe('test:myservice') as sub:209 start_time = time.time()210 stop_time = start_time + 1.0211 received = 0...
router.py
Source:router.py
...41class PathRouter:42 def __init__(self):43 self._map = []44 # self.compile = re.compile(r"(^/\w+)/?", re.S)45 def set_route(self, method: str, view_func, parsers, resp_class, path=None, re_path=None):46 if not path and not re_path:47 raise ValueError()48 for i in iter(self._map):49 if i.equal(path, method):50 return None51 self._map.append(ViewInfo(path=path, re_path=re_path, method=method,52 view=view_func, parsers=parsers,53 resp_class=resp_class))54 def get_route(self, request_path, request_method):55 """56 æ ¹æ®URLå¹é
57 å¤æ请æ±ç±»å58 å¤ææ¯å¦æ£åå¹é
59 """60 for vi in iter(self._map):61 if vi.method == request_method:62 if vi.is_re:63 result = self.match_path(vi.re_path, request_path)64 if result:65 vi.temp_vars = self.get_path_vars(vi.path_vars, result)66 return vi67 else:68 if vi.path == request_path:69 return vi70 else:71 raise NotFoundError("404 Not Found")72 def __iter__(self):73 for i in self._map:74 yield i75 @staticmethod76 def match_path(re_path, request_path):77 return re.search(re_path, request_path)78 @staticmethod79 def get_path_vars(path_vars, search_result):80 """81 è¿åå¹é
ç»æï¼82 å¦æå®ä¹äºè·¯å¾åæ°ï¼é£ä¹éååæ°å¯¹æ°æ®æ ¼å¼å83 """84 if path_vars:85 result = {}86 for pv in path_vars:87 var = search_result.group(pv["name"])88 if pv["type"] == "int":89 var = int(var)90 result[pv["name"]] = var91 return result92 return search_result.groupdict()93 def search_path(self, request_path, request_path_vars, path: str):94 """95 使ç¨æ£åå¹é
URL96 """97 result_map = {}98 result = re.search(request_path, path)99 if result:100 for pv in request_path_vars:101 var = result.group(pv["name"])102 if pv["type"] is int:103 var = int(var)104 result_map[pv["name"]] = var105 return result_map106 return None107#108# if __name__ == '__main__':109# from time import clock110#111# """112# ç®åæµè¯113# URL 50 æ¡114# """115#116# pm = PathRouter()117# pm.set_route("/index", "GET", max, None, None)118# pm.set_route("/index", "POST", abs, None, None)119# pm.set_route("/user", "GET", min, None, None)120# pm.set_route("/user", "POST", min, None, None)121# pm.set_route("/user", "DELETE", min, None, None)122# pm.set_route("/usjgers", "GET", oct, None, None)123# pm.set_route("/uNnser", "V", any, None, None)124# pm.set_route("/uYmser", "C", min, None, None)125# pm.set_route("/use4rs", "D", oct, None, None)126# pm.set_route("/usolEer", "E", any, None, None)127# pm.set_route("/ulsFer", "F", min, None, None)128# pm.set_route("/usloseSrs", "G", oct, None, None)129# pm.set_route("/usolseCr", "V", any, None, None)130# pm.set_route("/usfverA", "B", min, None, None)131# pm.set_route("/uVrsers", "T", oct, None, None)132# pm.set_route("/usolRer", "H", any, None, None)133# pm.set_route("/usoldeSrs", "G", oct, None, None)134# pm.set_route("/usseCr", "V", any, None, None)135# pm.set_route("/uvscerA", "B", min, None, None)136# pm.set_route("/uVvsers", "T", oct, None, None)137# pm.set_route("/usbRer", "H", any, None, None)138# pm.set_route("/usolbeSrs", "G", oct, None, None)139# pm.set_route("/useeCr", "V", any, None, None)140# pm.set_route("/useerA", "B", min, None, None)141# pm.set_route("/uVseyrs", "T", oct, None, None)142# pm.set_route("/usRker", "H", any, None, None)143# pm.set_route("/uscRker", "HU", any, None, None)144#145# pm.set_route("/usolbeSrs", "GA", oct, None, None)146# pm.set_route("/useeCr/<int:id>", "VCany", max, None, None)147# pm.set_route("/useerA", "BV", min, None, None)148# pm.set_route("/uVseyrs", "TT", oct, None, None)149# pm.set_route("/usRker", "HY", any, None, None)150# pm.set_route("/uscRker", "HIU", any, None, None)151#152# pm.set_route("/ulsFer", "aF", min, None, None)153# pm.set_route("/usloseSrs", "CG", oct, None, None)154# pm.set_route("/usolsseCr", "tV", any, None, None)155# pm.set_route("/usfvesrA", "itdB", min, None, None)156# pm.set_route("/uVrsers", "sdfT", oct, None, None)157# pm.set_route("/usolsRer", "dsH", any, None, None)158# pm.set_route("/usoldeSrs", "Gsd", oct, None, None)159#160# pm.set_route("/index", "PATH", max, None, None)161# pm.set_route("/index", "UPDATE", abs, None, None)162# pm.set_route("/users", "POST", min, None, None)163# pm.set_route("/user", "DELETE", min, None, None)164#165# pm.set_route("/ind2ex", "POST", abs, None, None)166# pm.set_route("/us4er", "GET", min, None, None)167# pm.set_route("/WEuser", "POST", min, None, None)168# pm.set_route("/usEer", "DELETE", min, None, None)169# pm.set_route("/us2jgers", "GET", oct, None, None)170# pm.set_route("/uNn2332ser", "V", any, None, None)171# pm.set_route("/uYR45mser", "C", min, None, None)172# pm.set_route("/us6e4rs", "D", oct, None, None)173# pm.set_route("/uso66lEer", "E", any, None, None)174#175# print([i for i in pm])176#177# s = clock()178# # s = datetime.datetime.now()179# for i in range(10000):180# cls = pm.get_route("/index", "GET")181# cls2 = pm.get_route("/index", "POST")182# cls3 = pm.get_route("/user", "GET")183# cls4 = pm.get_route("/users", "GET")184# cls5 = pm.get_route("/user", "POST")185# cls6 = pm.get_route("/userRT", "B")186# cls7 = pm.get_route("/useERT", "h")187# cls9 = pm.get_route("/usfvesrA", "itdB")...
test3.py
Source:test3.py
...21class PathRouter:22 def __init__(self):23 self._map = []24 self.compile = re.compile(r"(^/\w+)/?", re.S)25 def set_route(self, path: str, method: str, view_func, parsers, resp_class):26 for i in iter(self._map):27 if i.equal(path, method):28 return None29 self._map.append(ViewInfo(path=path, method=method, view=view_func, parsers=parsers, resp_class=resp_class))30 def get_route(self, path, method):31 """32 æ ¹æ®URLå¹é
33 å¤æ请æ±ç±»å34 å¤ææ¯å¦æ£åå¹é
35 """36 for vi in iter(self._map):37 if vi.method == method:38 if vi.re_path:39 resp, param = self.search_path(vi.path, path)40 if resp:41 vi.parameter.append(param)42 return vi43 else:44 if vi.path == path:45 return vi46 return None47 def __iter__(self):48 for i in self._map:49 yield i50 def search_path(self, v_path: str, path: str) -> (bool, None):51 """52 使ç¨æ£åå¹é
URL53 """54 d = self.compile.findall(path)55 if len(d):56 return v_path == d[0]57 return False, None58if __name__ == '__main__':59 """ 60 ç®åååæµè¯ 61 URL 50 æ¡62 """63 pm = PathRouter()64 pm.set_route("/index", "GET", max, None, None)65 pm.set_route("/index", "POST", abs, None, None)66 pm.set_route("/user", "GET", min, None, None)67 pm.set_route("/user", "POST", min, None, None)68 pm.set_route("/user", "DELETE", min, None, None)69 pm.set_route("/usjgers", "GET", oct, None, None)70 pm.set_route("/uNnser", "V", any, None, None)71 pm.set_route("/uYmser", "C", min, None, None)72 pm.set_route("/use4rs", "D", oct, None, None)73 pm.set_route("/usolEer", "E", any, None, None)74 pm.set_route("/ulsFer", "F", min, None, None)75 pm.set_route("/usloseSrs", "G", oct, None, None)76 pm.set_route("/usolseCr", "V", any, None, None)77 pm.set_route("/usfverA", "B", min, None, None)78 pm.set_route("/uVrsers", "T", oct, None, None)79 pm.set_route("/usolRer", "H", any, None, None)80 pm.set_route("/usoldeSrs", "G", oct, None, None)81 pm.set_route("/usseCr", "V", any, None, None)82 pm.set_route("/uvscerA", "B", min, None, None)83 pm.set_route("/uVvsers", "T", oct, None, None)84 pm.set_route("/usbRer", "H", any, None, None)85 pm.set_route("/usolbeSrs", "G", oct, None, None)86 pm.set_route("/useeCr", "V", any, None, None)87 pm.set_route("/useerA", "B", min, None, None)88 pm.set_route("/uVseyrs", "T", oct, None, None)89 pm.set_route("/usRker", "H", any, None, None)90 pm.set_route("/uscRker", "HU", any, None, None)91 pm.set_route("/usolbeSrs", "GA", oct, None, None)92 pm.set_route("/useeCr/<int:id>", "VCany", max, None, None)93 pm.set_route("/useerA", "BV", min, None, None)94 pm.set_route("/uVseyrs", "TT", oct, None, None)95 pm.set_route("/usRker", "HY", any, None, None)96 pm.set_route("/uscRker", "HIU", any, None, None)97 pm.set_route("/ulsFer", "aF", min, None, None)98 pm.set_route("/usloseSrs", "CG", oct, None, None)99 pm.set_route("/usolsseCr", "tV", any, None, None)100 pm.set_route("/usfvesrA", "itdB", min, None, None)101 pm.set_route("/uVrsers", "sdfT", oct, None, None)102 pm.set_route("/usolsRer", "dsH", any, None, None)103 pm.set_route("/usoldeSrs", "Gsd", oct, None, None)104 pm.set_route("/index", "PATH", max, None, None)105 pm.set_route("/index", "UPDATE", abs, None, None)106 pm.set_route("/users", "POST", min, None, None)107 pm.set_route("/user", "DELETE", min, None, None)108 pm.set_route("/ind2ex", "POST", abs, None, None)109 pm.set_route("/us4er", "GET", min, None, None)110 pm.set_route("/WEuser", "POST", min, None, None)111 pm.set_route("/usEer", "DELETE", min, None, None)112 pm.set_route("/us2jgers", "GET", oct, None, None)113 pm.set_route("/uNn2332ser", "V", any, None, None)114 pm.set_route("/uYR45mser", "C", min, None, None)115 pm.set_route("/us6e4rs", "D", oct, None, None)116 pm.set_route("/uso66lEer", "E", any, None, None)117 print([i for i in pm])118 s = clock()119 # s = datetime.datetime.now()120 for i in range(10000):121 cls = pm.get_route("/index", "GET")122 cls2 = pm.get_route("/index", "POST")123 cls3 = pm.get_route("/user", "GET")124 cls4 = pm.get_route("/users", "GET")125 cls5 = pm.get_route("/user", "POST")126 cls6 = pm.get_route("/userRT", "B")127 cls7 = pm.get_route("/useERT", "h")128 cls9 = pm.get_route("/usfvesrA", "itdB")129 path = cls.path130 method = cls5.method...
start_mininet.py
Source:start_mininet.py
...55 node_object.setMAC(macbase.format(ifnum), intf)56 ifnum += 157 for intf in node_object.intfList():58 print node,intf,node_object.MAC(intf)59def set_route(net, fromnode, prefix, gw):60 node_object = net.get(fromnode)61 node_object.cmdPrint("route add -net {} gw {}".format(prefix, gw))62def setup_addressing(net):63 reset_macs(net, 'server1', '10:00:00:00:00:{:02x}')64 reset_macs(net, 'server2', '20:00:00:00:00:{:02x}')65 reset_macs(net, 'client', '30:00:00:00:00:{:02x}')66 reset_macs(net, 'router', '40:00:00:00:00:{:02x}')67 set_ip_pair(net, 'server1','router','192.168.100.1/30','192.168.100.2/30')68 set_ip_pair(net, 'server2','router','192.168.200.1/30','192.168.200.2/30')69 set_ip_pair(net, 'client','router','10.1.1.1/30','10.1.1.2/30')70 set_route(net, 'server1', '10.1.0.0/16', '192.168.100.2')71 set_route(net, 'server1', '192.168.200.0/24', '192.168.100.2')72 set_route(net, 'server2', '10.1.0.0/16', '192.168.200.2')73 set_route(net, 'server2', '192.168.100.0/24', '192.168.200.2')74 set_route(net, 'client', '192.168.100.0/24', '10.1.1.2')75 set_route(net, 'client', '192.168.200.0/24', '10.1.1.2')76 set_route(net, 'client', '172.16.0.0/16', '10.1.1.2')77 forwarding_table = open('forwarding_table.txt', 'w') 78 table = '''192.168.100.0 255.255.255.0 192.168.100.1 router-eth079 192.168.200.0 255.255.255.0 192.168.200.1 router-eth180 10.1.0.0 255.255.0.0 10.1.1.1 router-eth281 '''82 forwarding_table.write(table)83 forwarding_table.close()84def main():85 topo = PyRouterTopo(args)86 net = Mininet(topo=topo, link=TCLink, cleanup=True, controller=None)87 setup_addressing(net)88 net.interact()89if __name__ == '__main__':90 main()
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!