Best Python code snippet using playwright-python
test_event_bus.py
Source:test_event_bus.py
...15 return redis, app, EventBus(app)16 def test_create_event_bus(self):17 redis, app, bus = self.get_bus()18 expect(bus.application).to_equal(app)19 expect(bus.handlers).to_be_empty()20 expect(bus.publish_items).to_be_empty()21 redis.subscribe.assert_called_once_with('events', bus.on_message)22 def test_can_subscribe(self):23 redis, app, bus = self.get_bus()24 callback = Mock()25 bus.subscribe('channel', 'uuid', callback)26 expect(bus.handlers).to_include('channel')27 expect(bus.handlers['channel']).to_include('uuid')28 expect(bus.handlers['channel']['uuid']).to_equal(callback)29 def test_can_unsubscribe(self):30 redis, app, bus = self.get_bus()31 bus.subscribe('channel', 'uuid', Mock())32 bus.unsubscribe('channel', 'uuid')33 expect(bus.handlers).to_be_like({34 'channel': {}35 })36 def test_can_unsubscribe_if_invalid_channel(self):37 redis, app, bus = self.get_bus()38 bus.unsubscribe('channel', 'uuid')39 expect(bus.handlers).to_be_empty()40 def test_can_unsubscribe_if_invalid_uuid(self):41 redis, app, bus = self.get_bus()42 bus.subscribe('channel', 'uuid', Mock())43 bus.unsubscribe('channel', 'uuid')44 bus.unsubscribe('channel', 'uuid')45 expect(bus.handlers).to_be_like({46 'channel': {}47 })48 def test_can_publish(self):49 redis, app, bus = self.get_bus()50 bus.publish('message')51 expect(bus.publish_items).to_include(('events', 'message'))52 def test_can_flush(self):53 redis, app, bus = self.get_bus()54 bus.publish('message')55 bus.publish('message2')56 bus.flush()57 expect(bus.publish_items).to_be_empty()58 redis.publish.assert_any_call('events', 'message')59 redis.publish.assert_any_call('events', 'message2')60 def test_on_message_returns_if_null_message(self):61 redis, app, bus = self.get_bus()62 handler_mock = Mock()63 bus.handlers['events']['uuid'] = handler_mock64 expect(bus.on_message(None)).to_be_null()65 expect(handler_mock.called).to_be_false()66 def test_on_message_when_type_not_message(self):67 redis, app, bus = self.get_bus()68 handler_mock = Mock()69 bus.handlers['events']['uuid'] = handler_mock70 value = dumps({'type': 'test'})71 bus.on_message(('type', 'events', value))...
solution_2.py
Source:solution_2.py
1def read_file(filename):2 file1 = open(filename, 'r')3 res = []4 for line in file1:5 res.append(line.strip())6 return res7def parse_lines(lines):8 seats = set()9 for x in range(len(lines[0])):10 for y in range(len(lines)):11 if lines[y][x] == "L":12 seats.add((x, y))13 return {14 "h": len(lines),15 "w": len(lines[0]),16 "seats": seats,17 }18def pretty_print(max_x, max_y, occupied, empty):19 for y in range(max_y):20 for x in range(max_x):21 if (x, y) in empty:22 print("L", end='')23 elif (x, y) in occupied:24 print("#", end='')25 else:26 print(".", end='')27 print()28# If a seat is empty(L) and there are no occupied seats adjacent to it, the seat becomes occupied.29# If a seat is occupied( # ) and five or more seats adjacent to it are also occupied, the seat becomes empty.30# Otherwise, the seat's state does not change.31def advance(w, h, occupied, empty):32 to_be_occupied = {}33 to_be_empty = {}34 for x in empty.keys():35 if len(empty[x] & occupied.keys()) == 0:36 to_be_occupied[x] = empty[x]37 else:38 to_be_empty[x] = empty[x]39 for x in occupied.keys():40 if len(occupied[x] & occupied.keys()) >= 5:41 to_be_empty[x] = occupied[x]42 else:43 to_be_occupied[x] = occupied[x]44 return (to_be_occupied, to_be_empty)45def explore_dir(board, x, y, dx, dy):46 nx = x + dx47 ny = y + dy48 if nx < 0 or nx > board["w"]:49 return None50 if ny < 0 or ny > board["h"]:51 return None52 if (nx, ny) in board["seats"]:53 return (nx, ny)54 return explore_dir(board, nx, ny, dx, dy)55def get_candidates(board, p):56 dirs = [(dx, dy) for dx in [-1, 0, 1]57 for dy in [-1, 0, 1] if not (dx == 0 and dy == 0)]58 cand = [explore_dir(board, p[0], p[1], d[0], d[1]) for d in dirs]59 # print(f"{p}: {cand}")60 return set([c for c in cand if cand is not None])61def add_candidates(board):62 new_empties = {}63 for pos in board["seats"]:64 new_empties[pos] = get_candidates(board, pos)65 return new_empties66lines = read_file("input.txt")67board = parse_lines(lines)68initial_empty = add_candidates(board)69h = board["h"]70w = board["w"]71occupied = {}72state = (occupied, initial_empty)73c = 074while True:75 new_state = advance(w, h, state[0], state[1])76 c = c + 177 if (new_state[0].keys() == state[0].keys()):78 print(f"Done in step {c} with {len(new_state[0])} occupied seats")79 # pretty_print(w, h, new_state[0], new_state[1])80 exit(1)...
emptiness_vows.py
Source:emptiness_vows.py
...12 class WhenString(Vows.Context):13 def topic(self):14 return ''15 def we_get_an_empty_string(self, topic):16 expect(topic).to_be_empty()17 class WhenList(Vows.Context):18 def topic(self):19 return []20 def we_get_an_empty_list(self, topic):21 expect(topic).to_be_empty()22 class WhenTuple(Vows.Context):23 def topic(self):24 return tuple([])25 def we_get_an_empty_tuple(self, topic):26 expect(topic).to_be_empty()27 class WhenDict(Vows.Context):28 def topic(self):29 return {}30 def we_get_an_empty_dict(self, topic):31 expect(topic).to_be_empty()32 class WhenWeGetAnError(Vows.Context):33 @Vows.capture_error34 def topic(self, last):35 expect([1]).to_be_empty()36 def we_get_an_understandable_message(self, topic):37 expect(topic).to_have_an_error_message_of("Expected topic([1]) to be empty")38 class WhenNotEmpty(Vows.Context):39 class WhenString(Vows.Context):40 def topic(self):41 return 'whatever'42 def we_get_a_not_empty_string(self, topic):43 expect(topic).Not.to_be_empty()44 class WhenList(Vows.Context):45 def topic(self):46 return ['something']47 def we_get_a_not_empty_list(self, topic):48 expect(topic).Not.to_be_empty()49 class WhenTuple(Vows.Context):50 def topic(self):51 return tuple(['something'])52 def we_get_a_not_empty_tuple(self, topic):53 expect(topic).Not.to_be_empty()54 class WhenDict(Vows.Context):55 def topic(self):56 return {"key": "value"}57 def we_get_a_not_empty_dict(self, topic):58 expect(topic).Not.to_be_empty()59 class WhenWeGetAnError(Vows.Context):60 @Vows.capture_error61 def topic(self, last):62 expect([]).not_to_be_empty()63 def we_get_an_understandable_message(self, topic):...
solution_1.py
Source:solution_1.py
1from functools import cache2def read_file(filename):3 file1 = open(filename, 'r')4 res = []5 for line in file1:6 res.append(line.strip())7 return res8def parse_lines(lines):9 seats = set()10 for x in range(len(lines[0])):11 for y in range(len(lines)):12 if lines[y][x] == "L":13 seats.add((x, y))14 return {15 "h": len(lines),16 "w": len(lines[0]),17 "seats": seats,18 }19@cache20def candidates(w, h, x, y):21 def passable(nx, ny):22 if nx == x and ny == y:23 return False24 if nx > w or nx < 0:25 return False26 if ny > h or ny < 0:27 return False28 return True29 pot = [(x+dx, y+dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]]30 return set([p for p in pot if passable(p[0], p[1])])31def pretty_print(max_x, max_y, occupied, empty):32 for y in range(max_y):33 for x in range(max_x):34 if (x, y) in empty:35 print("L", end='')36 elif (x, y) in occupied:37 print("#", end='')38 else:39 print(".", end='')40 print()41# If a seat is empty(L) and there are no occupied seats adjacent to it, the seat becomes occupied.42# If a seat is occupied( # ) and four or more seats adjacent to it are also occupied, the seat becomes empty.43# Otherwise, the seat's state does not change.44def advance(w, h, occupied, empty):45 to_be_occupied = {}46 to_be_empty = {}47 for x in empty.keys():48 if len(empty[x] & occupied.keys()) == 0:49 to_be_occupied[x] = empty[x]50 else:51 to_be_empty[x] = empty[x]52 for x in occupied.keys():53 if len(occupied[x] & occupied.keys()) >= 4:54 to_be_empty[x] = occupied[x]55 else:56 to_be_occupied[x] = occupied[x]57 return (to_be_occupied, to_be_empty)58def get_candidates(board, p):59 return candidates(board["w"], board["h"], p[0], p[1])60def add_candidates(board):61 new_empties = {}62 for pos in board["seats"]:63 new_empties[pos] = get_candidates(board, pos)64 return new_empties65lines = read_file("input.txt")66board = parse_lines(lines)67initial_empty = add_candidates(board)68h = board["h"]69w = board["w"]70occupied = {}71state = (occupied, initial_empty)72c = 073while True:74 new_state = advance(w, h, state[0], state[1])75 c = c + 176 if (new_state[0].keys() == state[0].keys()):77 print(f"Done in step {c} with {len(new_state[0])} occupied seats")78 # pretty_print(w, h, new_state[0], new_state[1])79 exit(1)...
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!!