Best Python code snippet using robotframework-pageobjects_python
state.py
Source:state.py
1"""Game state."""2import itertools3import pygame4from pygame.locals import *5from typing import Optional, Union6from common import color7from common import img8from common import state as common_state9from escape import room10from escape import state as escape_state11from . import interactions12from . import play_area13from . import play_map14from . import play_objects15from . import side_bar16class ShortEscapeEnding(escape_state.Ending):17 """The ending to kitty-escape without the congratulations card."""18 def __init__(self, screen):19 keypad = img.load(screen, factory=room.KeyPad)20 keypad.text = '9710' # opens the keypad so 'OK' will be centered21 keypad.text = 'OK'22 super().__init__(screen, color.GREY, keypad)23 def handle_tick(self, event):24 ret = super().handle_tick(event)25 if not ret:26 return False27 if self._frame == 3:28 self.active = False29 return True30class Game(common_state.GameState):31 _INTRO_TEXT = ("You've escaped the house, but you're lost in a maze. Try "32 "to find an exit.")33 _OBTAIN_FAIL_TEXT = 'Your arms are too full to pick anything up.'34 _USE_FAIL_TEXT = "You can't do anything with the {} right now."35 _END_TEXT = ("Roses are red, I've a confession to make. There is no exit, "36 "it's a closed heart shape. You've reached the end, Happy "37 "Valentine's Day!")38 def __init__(self, screen, debug, cheat):39 self._play_area = play_area.Surface(screen)40 self._side_bar = side_bar.Surface(screen)41 self._side_bar.text_area.show(self._INTRO_TEXT)42 self._debug = debug43 if debug:44 self._interact_objects = self._debug_compute_interact_objects()45 else:46 self._interact_objects = None47 if cheat:48 x, y = cheat49 for obj in itertools.chain(50 self._play_area._objects.values(),51 self._play_area._hidden_objects.values()):52 obj.move((-x * 800, -y * 800))53 super().__init__(screen)54 def _debug_compute_interact_objects(self):55 interact_objects = set()56 for name in itertools.chain(self._play_area._objects,57 self._play_area._hidden_objects):58 item = interactions.obtain(name)59 if not item:60 continue61 interact_objects.add(name)62 effects = list(item.item_effects)63 while effects:64 effect = effects.pop(0)65 if effect.type is not interactions.ItemEffectType.ADD:66 continue67 for use in interactions.use(effect.target):68 if use.activator:69 interact_objects.update(use.activator)70 effects.extend(use.item_effects)71 return interact_objects72 def _debug_draw(self):73 rects = [(self._play_area._player_feet_rect, color.BRIGHT_GREEN)]74 for name, obj in self._play_area._objects.items():75 rects.append((obj.RECT, color.BRIGHT_GREEN))76 if (name not in self._interact_objects or interactions.config(77 name, 'squares') is interactions.Squares.ALL):78 continue79 inflation = interactions.config(name, 'inflation')80 rects.append((obj.RECT.inflate(*inflation), color.LIGHT_CREAM))81 for rect, rect_color in rects:82 if isinstance(rect, play_objects._MultiRect):83 rects_to_draw = rect._get_rects()84 else:85 rects_to_draw = [rect]86 for rect in rects_to_draw:87 pygame.draw.rect(self._play_area._surface, rect_color, rect, 2)88 def draw(self):89 self._play_area.draw()90 self._side_bar.draw()91 if self._debug:92 self._debug_draw()93 pygame.display.update()94 def handle_player_movement(self, event):95 move_result: Union[bool, str] = self._play_area.handle_player_movement(96 event)97 if not move_result:98 return move_result99 if isinstance(move_result, str):100 self._side_bar.text_area.show(move_result)101 elif event.type == KEYDOWN:102 self._side_bar.text_area.show(None)103 self._side_bar.mini_map.update(104 self._play_area.current_square, self._play_area.visible_walls)105 if self._play_area.current_square == play_map.END_SQUARE:106 self._side_bar.mini_map.turn_red()107 self._side_bar.text_area.show(self._END_TEXT)108 self.draw()109 return True110 def _apply_item_effects(self, effects, pos=None):111 for effect in effects:112 if effect.type is interactions.ItemEffectType.REMOVE:113 self._side_bar.consume_item(effect.target, pos)114 else:115 assert effect.type is interactions.ItemEffectType.ADD116 self._side_bar.add_item(effect.target)117 def handle_click(self, event):118 if event.type != MOUSEBUTTONDOWN or event.button != 1 or (119 not self._play_area.collidepoint(event.pos) and120 not self._side_bar.collidepoint(event.pos)):121 return False122 click_result: Union[bool, interactions.Item] = (123 self._play_area.handle_click(event.pos))124 if click_result:125 if isinstance(click_result, interactions.Item):126 if self._side_bar.has_space_for(click_result.item_effects):127 self._play_area.apply_effects(128 click_result.play_area_effects)129 self._apply_item_effects(click_result.item_effects)130 self._side_bar.text_area.show(click_result.reason)131 else:132 self._side_bar.text_area.show(self._OBTAIN_FAIL_TEXT)133 else:134 click_result: Union[bool, str] = self._side_bar.handle_click(135 event.pos)136 assert click_result137 if isinstance(click_result, str):138 use_result: Optional[interactions.Use] = (139 self._play_area.use_item(click_result))140 if use_result:141 self._apply_item_effects(use_result.item_effects, event.pos)142 self._side_bar.text_area.show(use_result.reason)143 else:144 self._side_bar.text_area.show(145 self._USE_FAIL_TEXT.format(146 click_result.replace('_', ' ')))147 self.draw()148 return True149 def handle_debug_location_request(self, event):150 if not self._debug or event.type != KEYDOWN or event.key != K_l:151 return False152 effective_player_feet_pos = self._play_area._effective_rect(153 self._play_area._player_feet_rect).midbottom154 square = []155 offsets = []156 for i in range(2):157 shift = effective_player_feet_pos[i] - play_map.START_POS[i]158 square.append(shift // play_map.SQUARE_LENGTH)159 offsets.append(shift % play_map.SQUARE_LENGTH)160 print(tuple(square), tuple(offsets))...
529.py
Source:529.py
1from typing import List2class Solution:3 def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:4 def getAdj(i, j):5 result_e = []6 result_m = []7 def getType(i, j):8 if board[i][j] == 'M':9 result_m.append((i, j))10 elif board[i][j] == 'E':11 result_e.append((i, j))12 if i > 0 and j > 0:13 getType(i - 1, j - 1)14 if i > 0:15 getType(i - 1, j)16 if j > 0:17 getType(i, j - 1)18 if i < len(board) - 1 and j < len(board[0]) - 1:19 getType(i + 1, j + 1)20 if i < len(board) - 1:21 getType(i + 1, j)22 if j < len(board[0]) - 1:23 getType(i, j + 1)24 if i < len(board) - 1 and j > 0:25 getType(i + 1, j - 1)26 if i > 0 and j < len(board[0]) - 1:27 getType(i - 1, j + 1)28 return result_e, result_m29 def getNum(i, j):30 if board[i][j] == 'M':31 return ('X', [])32 adj_e, adj_m = getAdj(i, j)33 return (str(len(adj_m)) if len(adj_m) > 0 else 'B', adj_e)34 def handleClick(i, j):35 click_result, adj_e = getNum(i, j)36 if click_result == 'X':37 board[i][j] = click_result38 elif click_result == 'B':39 board[i][j] = click_result40 for i_n, j_n in adj_e:41 handleClick(i_n, j_n)42 else:43 board[i][j] = click_result44 return board45 i, j = click...
utils.py
Source:utils.py
1import string2import random3import json4import logging5import sys6og = logging.getLogger("ZDM_cli_test")7logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)8def randomString(stringLength=10):9 """Generate a random string of fixed length """10 letters = string.ascii_lowercase11 return ''.join(random.choice(letters) for i in range(stringLength))12def _result_to_json(click_result):13 #logging.debug(click_result.output)...
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!!