How to use _is_type method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

rnd_game.py

Source:rnd_game.py Github

copy

Full Screen

...82 return self._grid[(channel, *coord)]83 def _in_bounds(self, coord: Tuple[int, int], action: Directions = Directions.kNone) -> bool:84 row, col = coord_from_action(coord, action)85 return col >= 0 and col < self._cols and row >= 0 and row < self._rows86 def _is_type(self, coord: Tuple[int, int], element: Element, action: Directions = Directions.kNone) -> bool:87 new_coord = coord_from_action(coord, action)88 return self._in_bounds(coord, action) and self._grid_to_element(new_coord) == element89 def _has_property(self, coord: Tuple[int, int], property: ElementProperties, action: Directions = Directions.kNone) -> bool:90 new_coord = coord_from_action(coord, action)91 return self._in_bounds(coord, action) and (self._grid_to_element(new_coord).properties & property) > 092 def _move_item(self, coord: Tuple[int, int], action: Directions) -> None:93 new_coord = coord_from_action(coord, action)94 channel_old = self._grid_to_channel(coord)95 channel_new = self._grid_to_channel(new_coord)96 self._grid[(channel_old, *new_coord)] = self._grid[(channel_old, *coord)] # Move item97 self._grid[(channel_old, *coord)] = 0 # Unset previous item in old coord98 self._grid[(channel_new, *new_coord)] = 0 # Unset previous item in new coord99 self._grid[(HiddenCellType.kEmpty, *coord)] = 1 # Set previous coord to empty100 self._has_updated[new_coord] = True101 assert self._check_channel(coord) == True102 assert self._check_channel(new_coord) == True103 def _set_item(self, coord: Tuple[int, int], element: Element, id: int, action: Directions = Directions.kNone) -> None:104 new_coord = coord_from_action(coord, action)105 old_channel = self._grid_to_channel(new_coord)106 self._grid[(old_channel, *new_coord)] = 0 # Need to ensure we remove item already existing here107 new_channel = int(element.cell_type)108 self._grid[(new_channel, *new_coord)] = id109 assert self._check_channel(new_coord) == True # Ensure exactly 1 channel is set110 def _get_item(self, coord: Tuple[int, int], action: Directions = Directions.kNone) -> Element:111 new_coord = coord_from_action(coord, action)112 return self._grid_to_element(new_coord)113 def _get_id(self, coord: Tuple[int, int], action: Directions = Directions.kNone) -> int:114 new_coord = coord_from_action(coord, action)115 return self._grid_to_id(new_coord)116 def _is_type_adjacent(self, coord: Tuple[int, int], element: Element) -> bool:117 return (118 self._is_type(coord, element, Directions.kUp)119 or self._is_type(coord, element, Directions.kLeft)120 or self._is_type(coord, element, Directions.kDown)121 or self._is_type(coord, element, Directions.kRight)122 )123 def _can_roll_left(self, coord: Tuple[int, int]) -> bool:124 return (125 self._has_property(coord, ElementProperties.kRounded, Directions.kDown)126 and self._is_type(coord, kElEmpty, Directions.kLeft)127 and self._is_type(coord, kElEmpty, Directions.kDownLeft)128 )129 def _can_roll_right(self, coord: Tuple[int, int]) -> bool:130 return (131 self._has_property(coord, ElementProperties.kRounded, Directions.kDown)132 and self._is_type(coord, kElEmpty, Directions.kRight)133 and self._is_type(coord, kElEmpty, Directions.kDownRight)134 )135 def _roll_left(self, coord: Tuple[int, int], element: Element) -> None:136 self._set_item(coord, element, self._get_id(coord))137 self._move_item(coord, Directions.kLeft)138 def _roll_right(self, coord: Tuple[int, int], element: Element) -> None:139 self._set_item(coord, element, self._get_id(coord))140 self._move_item(coord, Directions.kRight)141 def _push(self, coord: Tuple[int, int], stationary: Element, falling: Element, action: Directions) -> None:142 new_coord = coord_from_action(coord, action)143 if self._is_type(new_coord, kElEmpty, action):144 # Check if the element will become stationary or falling145 next_coord = coord_from_action(new_coord, action)146 is_empty = self._is_type(next_coord, kElEmpty, Directions.kDown)147 self._set_item(new_coord, falling if is_empty else stationary, self._grid_to_id(new_coord), action)148 self._move_item(coord, action) # Move agent149 def _move_through_magic(self, coord: Tuple[int, int], element: Element) -> None:150 # Check if magic wall is still active151 if self._magic_wall_steps <= 0:152 return153 self._magic_active = True154 coord_below = coord_from_action(coord, Directions.kDown)155 # Need to ensure cell below magic wall is empty (so item can pass through)156 if self._is_type(coord_below, kElEmpty, Directions.kDown):157 self._set_item(coord, kElEmpty, 1) # Empty and dirt ids are 1158 self._increment_counter()159 self._set_item(coord_below, element, self._id_counter, Directions.kDown) # Spawned element gets new id160 def _explode(self, coord: Tuple[int, int], element: Element, action: Directions = Directions.kNone) -> None:161 new_coord = coord_from_action(coord, action)162 old_element = self._get_item(new_coord)163 exploded_element = kElementToExplosion[old_element] if old_element in kElementToExplosion else kElExplosionEmpty164 self._increment_counter()165 self._set_item(new_coord, element, self._id_counter)166 # Recursively check all directions for chain explosions167 for direction in Directions:168 if direction == Directions.kNone or not self._in_bounds(new_coord, direction):169 continue170 if self._has_property(new_coord, ElementProperties.kCanExplode, direction):171 self._explode(new_coord, exploded_element, direction)172 elif self._has_property(new_coord, ElementProperties.kConsumable, direction):173 self._increment_counter()174 self._set_item(new_coord, exploded_element, self._id_counter, direction)175 def _open_gate(self, el_gate_closed: Element) -> None:176 el_gate_open = kGateOpenMap[el_gate_closed]177 closed_gate_indices = np.transpose((self._grid[int(el_gate_closed.cell_type), :, :]).nonzero())178 # Convert closed gates to open179 for idx in closed_gate_indices:180 coord = (idx[0].item(), idx[1].item())181 self._set_item(coord, el_gate_open, self._grid_to_id(coord))182 def _update_stone(self, coord: Tuple[int, int]) -> None:183 if self._is_type(coord, kElEmpty, Directions.kDown):184 # Set to falling if gravity is on185 if not self._gravity:186 return187 self._set_item(coord, kElStoneFalling, self._grid_to_id(coord))188 self._update_stone_falling(coord)189 elif self._can_roll_left(coord): # Roll left190 self._roll_left(coord, kElStoneFalling)191 elif self._can_roll_right(coord): # Roll right192 self._roll_right(coord, kElStoneFalling)193 def _update_stone_falling(self, coord: Tuple[int, int]) -> None:194 if self._is_type(coord, kElEmpty, Directions.kDown): # Continue to fall195 self._move_item(coord, Directions.kDown)196 elif self._has_property(coord, ElementProperties.kCanExplode, Directions.kDown): # Falling stones explode items197 old_element = self._get_item(coord, Directions.kDown)198 exploded_element = kElementToExplosion[old_element] if old_element in kElementToExplosion else kElExplosionEmpty199 self._explode(coord, exploded_element, Directions.kDown)200 elif self._is_type(coord, kElWallMagicOn, Directions.kDown) or self._is_type(201 coord, kElWallMagicDormant, Directions.kDown202 ): # Convert item through magic wall203 self._move_through_magic(coord, kMagicWallConversion[self._get_item(coord)])204 elif self._is_type(coord, kElNut, Directions.kDown): # Falling on nut -> diamond205 self._increment_counter()206 self._set_item(coord, kElDiamond, self._id_counter, Directions.kDown)207 self._reward_signal |= RewardCodes.kRewardNutToDiamond208 elif self._is_type(coord, kElBomb, Directions.kDown): # Falling on bomb -> explode209 old_element = self._get_item(coord, Directions.kDown)210 exploded_element = kElementToExplosion[old_element] if old_element in kElementToExplosion else kElExplosionEmpty211 self._explode(coord, exploded_element, Directions.kDown)212 elif self._can_roll_left(coord): # Roll left213 self._roll_left(coord, kElStoneFalling)214 elif self._can_roll_right(coord): # Roll right215 self._roll_right(coord, kElStoneFalling)216 else: # Default option is for falling stone to become stationary217 self._set_item(coord, kElStone, self._grid_to_id(coord))218 def _update_diamond(self, coord: Tuple[int, int]) -> None:219 if self._is_type(coord, kElEmpty, Directions.kDown):220 # Set to falling if gravity is on221 if not self._gravity:222 return223 self._set_item(coord, kElDiamondFalling, self._grid_to_id(coord))224 self._update_diamond_falling(coord)225 elif self._can_roll_left(coord): # Roll left226 self._roll_left(coord, kElDiamondFalling)227 elif self._can_roll_right(coord): # Roll right228 self._roll_right(coord, kElDiamondFalling)229 def _update_diamond_falling(self, coord: Tuple[int, int]) -> None:230 if self._is_type(coord, kElEmpty, Directions.kDown): # Continue to fall231 self._move_item(coord, Directions.kDown)232 elif (233 self._has_property(coord, ElementProperties.kCanExplode, Directions.kDown)234 and not self._is_type(coord, kElBomb, Directions.kDown)235 and not self._is_type(coord, kElBombFalling, Directions.kDown)236 ): # Falling diamond explode items (but not bombs)237 old_element = self._get_item(coord, Directions.kDown)238 exploded_element = kElementToExplosion[old_element] if old_element in kElementToExplosion else kElExplosionEmpty239 self._explode(coord, exploded_element, Directions.kDown)240 elif self._is_type(coord, kElWallMagicOn, Directions.kDown) or self._is_type(241 coord, kElWallMagicDormant, Directions.kDown242 ): # Convert item through magic wall243 self._move_through_magic(coord, kMagicWallConversion[self._get_item(coord)])244 elif self._can_roll_left(coord): # Roll left245 self._roll_left(coord, kElDiamondFalling)246 elif self._can_roll_right(coord): # Roll right247 self._roll_right(coord, kElDiamondFalling)248 else: # Default option is for falling diamond to become stationary249 self._set_item(coord, kElDiamond, self._grid_to_id(coord))250 def _update_nut(self, coord: Tuple[int, int]) -> None:251 if self._is_type(coord, kElEmpty, Directions.kDown):252 # Set to falling if gravity is on253 if not self._gravity:254 return255 self._set_item(coord, kElNutFalling, self._grid_to_id(coord))256 self._update_nut_falling(coord)257 elif self._can_roll_left(coord): # Roll left258 self._roll_left(coord, kElNutFalling)259 elif self._can_roll_right(coord): # Roll right260 self._roll_right(coord, kElNutFalling)261 def _update_nut_falling(self, coord: Tuple[int, int]) -> None:262 if self._is_type(coord, kElEmpty, Directions.kDown): # Continue to fall263 self._move_item(coord, Directions.kDown)264 elif self._can_roll_left(coord): # Roll left265 self._roll_left(coord, kElNutFalling)266 elif self._can_roll_right(coord): # Roll right267 self._roll_right(coord, kElNutFalling)268 else: # Default option is for falling nut to become stationary269 self._set_item(coord, kElNut, self._grid_to_id(coord))270 def _update_bomb(self, coord: Tuple[int, int]) -> None:271 if self._is_type(coord, kElEmpty, Directions.kDown): 272 # Set to falling if gravity is on273 if not self._gravity:274 return275 self._set_item(coord, kElBombFalling, self._grid_to_id(coord))276 self._update_bomb_falling(coord)277 elif self._can_roll_left(coord): # Roll left278 self._roll_left(coord, kElBombFalling)279 elif self._can_roll_right(coord): # Roll right280 self._roll_right(coord, kElBombFalling)281 def _update_bomb_falling(self, coord: Tuple[int, int]) -> None:282 if self._is_type(coord, kElEmpty, Directions.kDown): # Continue to fall283 self._move_item(coord, Directions.kDown)284 elif self._can_roll_left(coord): # Roll left285 self._roll_left(coord, kElBombFalling)286 elif self._can_roll_right(coord): # Roll right287 self._roll_right(coord, kElBombFalling)288 else: # Default option is for falling bomb is to explode289 old_element = self._get_item(coord)290 exploded_element = kElementToExplosion[old_element] if old_element in kElementToExplosion else kElExplosionEmpty291 self._explode(coord, exploded_element)292 def _update_exit(self, coord: Tuple[int, int]) -> None:293 # Open exit if enough gems collected294 if self._gems_collected >= self._gems_required:295 self._set_item(coord, kElExitOpen, self._grid_to_id(coord))296 def _update_agent(self, coord: Tuple[int, int], action: Directions) -> None:297 if self._is_type(coord, kElEmpty, action) or self._is_type(coord, kElDirt, action): # Move if empty/dirt298 self._move_item(coord, action)299 elif self._is_type(coord, kElDiamond, action) or self._is_type(coord, kElDiamondFalling, action): # Collect gems300 self._gems_collected += 1301 self._current_reward += kGemPoints[self._get_item(coord, action)]302 self._reward_signal |= RewardCodes.kRewardCollectDiamond303 self._move_item(coord, action)304 elif IsActionHorz(action) and (305 self._is_type(coord, kElStone, action)306 or self._is_type(coord, kElNut, action)307 or self._is_type(coord, kElBomb, action)308 ): # Push stone, nut, or bomb horizontal309 self._push(coord, self._get_item(coord, action), kElToFalling[self._get_item(coord, action)], action)310 elif IsKey(self._get_item(coord, action)): # Collecting key, set gate open311 self._open_gate(kKeyToGate[self._get_item(coord, action)])312 self._move_item(coord, action)313 self._reward_signal |= RewardCodes.kRewardCollectKey314 elif IsOpenGate(self._get_item(coord, action)): # Walking through open gate315 coord_gate = coord_from_action(coord, action)316 if self._has_property(coord_gate, ElementProperties.kTraversable, action):317 if self._is_type(coord_gate, kElDiamond, action): # Could pass through onto diamond318 self._gems_collected += 1319 self._current_reward += kGemPoints[kElDiamond]320 self._reward_signal |= RewardCodes.kRewardCollectDiamond321 elif IsKey(self._get_item(coord_gate, action)): # Could pass through onto key322 self._open_gate(kKeyToGate[self._get_item(coord_gate, action)])323 self._reward_signal |= RewardCodes.kRewardCollectKey324 self._set_item(coord_gate, kElAgent, self._grid_to_id(coord), action)325 self._set_item(coord, kElEmpty, 1)326 self._reward_signal |= RewardCodes.kRewardWalkThroughGate327 elif self._is_type(coord, kElExitOpen, action): # Walking though exit328 self._move_item(coord, action)329 self._set_item(coord, kElAgentInExit, self._grid_to_id(coord), action) # Different from open_spiel330 self._current_reward += self._steps_remaining if self._steps_remaining is not None else kGemPoints[kElAgentInExit]331 self._reward_signal |= RewardCodes.kRewardWalkThroughExit332 def _update_firefly(self, coord: Tuple[int, int], action: Directions) -> None:333 new_direction = kRotateLeft[action]334 if self._is_type_adjacent(coord, kElAgent) or self._is_type_adjacent(coord, kElBlob): # Exploide if touching agent/blob335 old_element = self._get_item(coord)336 exploded_element = kElementToExplosion[old_element] if old_element in kElementToExplosion else kElExplosionEmpty337 self._explode(coord, exploded_element)338 elif self._is_type(coord, kElEmpty, new_direction): # First try to rotate left339 self._set_item(coord, kDirectionToFirefly[new_direction], self._grid_to_id(coord))340 self._move_item(coord, new_direction)341 elif self._is_type(coord, kElEmpty, action): # Then try to move forward342 self._set_item(coord, kDirectionToFirefly[action], self._grid_to_id(coord))343 self._move_item(coord, action)344 else: # No other options, rotate right345 self._set_item(coord, kDirectionToFirefly[kRotateRight[action]], self._grid_to_id(coord))346 def _update_butterfly(self, coord: Tuple[int, int], action: Directions) -> None:347 new_direction = kRotateRight[action]348 if self._is_type_adjacent(coord, kElAgent) or self._is_type_adjacent(coord, kElBlob): # Exploide if touching agent/blob349 old_element = self._get_item(coord)350 exploded_element = kElementToExplosion[old_element] if old_element in kElementToExplosion else kElExplosionEmpty351 self._explode(coord, exploded_element)352 elif self._is_type(coord, kElEmpty, new_direction): # First try to rotate right353 self._set_item(coord, kDirectionToButterfly[new_direction], self._grid_to_id(coord))354 self._move_item(coord, new_direction)355 elif self._is_type(coord, kElEmpty, action): # Then try to move forward356 self._set_item(coord, kDirectionToButterfly[action], self._grid_to_id(coord))357 self._move_item(coord, action)358 else: # No other options, rotate left359 self._set_item(coord, kDirectionToButterfly[kRotateLeft[action]], self._grid_to_id(coord))360 def _update_orange(self, coord: Tuple[int, int], action: Directions) -> None:361 if self._is_type(coord, kElEmpty, action): # Continue moving in direction362 self._move_item(coord, action)363 elif self._is_type_adjacent(coord, kElAgent): # Run into agent -> explode364 old_element = self._get_item(coord)365 exploded_element = kElementToExplosion[old_element] if old_element in kElementToExplosion else kElExplosionEmpty366 self._explode(coord, exploded_element)367 else: # Blocked, roll for new direction368 open_directions = [369 direction370 for direction in Directions371 if (372 direction != Directions.kNone373 and self._in_bounds(coord, direction)374 and self._is_type(coord, kElEmpty, direction)375 )376 ]377 # Roll for new direction378 if len(open_directions) > 0:379 new_direction = open_directions[self._rng.choice(len(open_directions))]380 self._set_item(coord, kDirectionToOrange[new_direction], self._grid_to_id(coord))381 def _update_magic_wall(self, coord: Tuple[int, int]) -> None:382 if self._magic_active: # Dormant383 self._set_item(coord, kElWallMagicOn, self._grid_to_id(coord))384 elif self._magic_wall_steps > 0: # Active385 self._set_item(coord, kElWallMagicDormant, self._grid_to_id(coord))386 else: # Expired387 self._set_item(coord, kElWallMagicExpired, self._grid_to_id(coord))388 def _update_blob(self, coord: Tuple[int, int]) -> None:389 if self._blob_swap != kNullElement: # Replace blob if swap element set390 self._increment_counter()391 self._set_item(coord, self._blob_swap, self._id_counter)392 else:393 self._blob_size += 1394 if self._is_type_adjacent(coord, kElEmpty) or self._is_type_adjacent(395 coord, kElDirt396 ): # Check if at least one tile blob can grow397 self._blob_enclosed = False398 # Roll if to grow and direction399 will_grow = self._rng.integers(0, 255) < self._blob_chance400 possible_directions = [Directions.kUp, Directions.kLeft, Directions.kDown, Directions.kRight]401 direction_grow = possible_directions[self._rng.choice(len(possible_directions))]402 if will_grow and (self._is_type(coord, kElEmpty, direction_grow) or self._is_type(coord, kElDirt, direction_grow)):403 self._increment_counter()404 self._set_item(coord, kElBlob, self._id_counter, direction_grow)405 def _update_explosions(self, coord: Tuple[int, int]) -> None:406 self._increment_counter()407 if kExplosionToElement[self._get_item(coord)] == kElDiamond:408 self._reward_signal |= RewardCodes.kRewardButterflyToDiamond409 self._set_item(coord, kExplosionToElement[self._get_item(coord)], self._id_counter)410 def _start_scan(self) -> None:411 # Update global flags412 if self._steps_remaining is not None:413 self._steps_remaining += -1414 self._current_reward = 0.0415 self._blob_size = 0416 self._blob_enclosed = True...

Full Screen

Full Screen

Validators.py

Source:Validators.py Github

copy

Full Screen

...20 condition: bool, message: str, exception_type: BaseException = WLCAssertionException21) -> bool:22 if not condition:23 raise exception_type(message)24def _is_type(in_obj, expect_type: type, message: str = None) -> bool:25 _assert(26 isinstance(expect_type, type),27 "Nested assertions? they do happen." "expect_type param must be a type.",28 )29 _assert(30 isinstance(in_obj, expect_type),31 exception_type=IncorrectTypeError,32 message=message33 if message is not None34 else f"Expected an object of type {expect_type}, but input object"35 f" was of type {type(in_obj)}",36 )37@validator38def simple(39 condition: bool = False,40 message_on_failure: str = "",41 message_for_remediation: str = "",42 exception_type: BaseException = WLCAssertionException,43) -> bool:44 """Simple validator to check that a condition is true,45 raises an exception with message and remediation information if not"""46 if not condition:47 raise exception_type(48 message=message_on_failure, msg_remedial=message_for_remediation49 )50@validator51def baseurl(url: str):52 """Validate that the base_uri parameter is conformant"""53 _is_type(54 url,55 str,56 f"Base URL parameter should be a string, but was actually of type {type(url)}",57 )58 _assert(59 Patterns.URLPROTO.match(url),60 f"Base URL parameter ('{url}') must begin with http(s)://",61 )62 _assert(63 not url.endswith("/"),64 f"Base URL parameter ('{url}') must not end with a forward-slash",65 )66@validator67def length(item, size: int, message: str = None):68 """Validate that the length of an object is what is expected"""69 _assert(70 len(item) == size,71 exception_type=IncorrectLengthError,72 message=message73 if message is not None74 else f"Object expected to be of size {size}, but was actually {len(item)}",75 )76@validator77def credential(credentials: tuple):78 """Validate that a credentials parameter is correct"""79 _is_type(80 credentials,81 tuple,82 "Credentials parameter must be a tuple, but "83 f"given object was actually {type(credentials)}",84 )85 length(86 credentials,87 size=2,88 message=(89 "Credentials parameter must contain "90 "a username and a password, but given "91 f"object contained {len(credentials)} item(s)"92 ),93 )94 _is_type(95 credentials[0],96 str,97 f"Username must be a string, but was actually {type(credentials[0])}",98 )99 _is_type(100 credentials[1],101 str,102 f"Password must be a string, but was actually {type(credentials[1])}",103 )104@validator105def response_code(response: Response, expect: tuple[int] = (200,)):106 """Validate the response code of a requests Response object"""107 _is_type(108 response,109 expect_type=Response,110 message="Expected a Response object, but"111 f"got an object of type {type(response)} instead.",112 )113 _is_type(114 expect,115 expect_type=tuple,116 message="Expected a tuple of expected response codes, but got an "117 f"object of type {type(expect)} instead.",118 )119 for single_expected_code in expect:120 _is_type(121 single_expected_code,122 expect_type=int,123 message="Expected a tuple of expected response code integers, "124 f"but one or more was of type {type(single_expected_code)} instead.",125 )126 _assert(127 response.status_code in expect,128 exception_type=UnexpectedResponseStatusError,129 message=f"{response.request.method} '{response.url}' returned status "130 f"{response.status_code}, expected one of {expect}",...

Full Screen

Full Screen

types_.py

Source:types_.py Github

copy

Full Screen

...40 else:41 return MatchResult.success("got %s" % jsonify(actual))42 else:43 return MatchResult.failure("got %s (%s)" % (jsonify(actual), self._get_value_type_name(actual)))44def _is_type(types, type_name):45 def wrapper(value_matcher=None):46 return IsValueOfType(47 types, type_name, is_(value_matcher) if value_matcher is not None else None48 )49 wrapper.__doc__ = "Test if value is of type %s" % type_name50 return wrapper51is_integer = _is_type([int], "an integer")52is_float = _is_type([float], "a float")53is_bool = _is_type([bool], "a boolean")54is_str = _is_type(STRING_TYPES, "a string")55is_dict = _is_type([dict], "a collection")...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Lemoncheesecake automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful