Best Python code snippet using Kiwi_python
modules.py
Source:modules.py
...23 def get_short_description(self):24 """ Build the short description """25 pass26 @abstractmethod27 def get_long_description(self):28 """ Build the long description """29 pass30# ------------------------------------------------------------------------------31# Error Class32# ------------------------------------------------------------------------------33class Error(Module):34 """ Error Class """35 # Init36 def __init__(self, message):37 """ Create error38 >>> # dummy error Module39 >>> Error("ERROR: message goes here")40 ERROR: message goes here41 """42 # Log43 debug_print("__init__ for Error")44 self.type = "Error"45 self._message = message46 # Short description47 def get_short_description(self):48 """ Build the short description49 >>> # dummy error Module50 >>> error = Error("ERROR: message goes here")51 >>> error.get_short_description()52 'ERROR: message goes here'53 """54 # Log55 debug_print("get_short_description")56 # Return57 return self._message58 # Long description59 def get_long_description(self):60 """ Build the long description61 >>> # dummy error Module62 >>> error = Error("ERROR: message goes here")63 >>> error.get_long_description()64 'ERROR: message goes here'65 """66 # Log67 debug_print("get_long_description")68 # Return69 return self._message70# ------------------------------------------------------------------------------71# Monster Class72# ------------------------------------------------------------------------------73class Monster(Module):74 """ Monster Class """75 # Get monster by CR and/or environment76 def get_random_monster(self, cr, environment):77 """ Get a random monster by CR and environment """78 # Log79 debug_print("get_random_monster_name")80 debug_print(" cr: " + cr)81 debug_print(" environment: " + environment)82 monsters = ALL_MONSTERS83 # CR84 if cr != "":85 monsters = [monster for monster in monsters if cr == monster['CR']]86 # Environment87 if environment != "":88 monsters = [89 monster for monster in monsters90 if environment in monster['Env']91 ]92 # Get random monster93 if len(monsters) == 0:94 monster = "ERROR: No suitable monster found"95 print(monster)96 else:97 monster = random.choice(monsters)98 # Return99 return monster100 # Init101 def __init__(self, cr, environment):102 """ Create new random monster103 >>> import random104 >>> random.seed(13)105 >>> # valid monster found106 >>> Monster("2", "")107 gargoyle108 >>> # no monster found109 >>> monster = Monster("31", "")110 ERROR: No suitable monster found111 """112 # Log113 debug_print("__init__ for Monster")114 self.type = "Monster"115 # Get random monster116 self._monster = self.get_random_monster(cr, environment)117 # Get HP118 if is_error_string(self._monster):119 self._name = self._monster120 self._hp = [0, 0]121 else:122 self._name = self._monster['Name']123 self._hp = ALL_HP[self._monster['CR']]124 # Short description125 def get_short_description(self):126 """ Build the short description127 >>> import random128 >>> random.seed(13)129 >>> # valid monster found130 >>> monster = Monster("2", "")131 >>> monster.get_short_description()132 'gargoyle'133 >>> # no monster found134 >>> monster = Monster("31", "")135 ERROR: No suitable monster found136 >>> monster.get_short_description()137 'ERROR: No suitable monster found'138 """139 # Log140 debug_print("get_short_description")141 # Build description142 description = self._name143 # Log144 debug_print(" description: " + description)145 # Return146 return description147 # Long description148 def get_long_description(self):149 """ Build the long description150 >>> import random151 >>> random.seed(13)152 >>> # valid monster found153 >>> monster = Monster("2", "")154 >>> monster.get_long_description()155 'gargoyle - HP: 86-100'156 >>> # no monster found157 >>> monster = Monster("31", "")158 ERROR: No suitable monster found159 >>> monster.get_long_description()160 'ERROR: No suitable monster found'161 """162 # Log163 debug_print("get_long_description")164 # Build description165 description = self._name166 if not is_error_string(description):167 description += " - HP: " + str(self._hp[0]) + "-" + str(168 self._hp[1])169 # Log170 debug_print(" description: " + description)171 # Return172 return description173# ------------------------------------------------------------------------------174# Npc Class175# ------------------------------------------------------------------------------176class Npc(Module):177 """ Npc Class """178 # Get NPC by CR and/or environment179 def get_random_npc_type(self, cr, environment):180 """ Get a random NPC type by CR and environment """181 # Log182 debug_print("get_random_npc_type")183 debug_print(" cr: " + cr)184 debug_print(" environment: " + environment)185 npcs = ALL_NPCs186 # CR187 if cr != "":188 npcs = [npc for npc in npcs if cr == npc['CR']]189 # Environment190 if environment != "":191 npcs = [npc for npc in npcs if environment in npc['Env']]192 # Get random NPC193 if len(npcs) == 0:194 npc_type = "ERROR: No suitable NPC found"195 print(npc_type)196 else:197 npc_type = random.choice(npcs)198 # Return199 return npc_type200 # Init201 def __init__(self, cr, environment):202 """ Create new random NPC203 >>> import random204 >>> random.seed(13)205 >>> # valid NPC found206 >>> Npc("9", "")207 abjurer208 >>> # no NPC found209 >>> npc = Npc("15", "mountain")210 ERROR: No suitable NPC found211 """212 # Log213 debug_print("__init__ for Npc")214 self.type = "Npc"215 # Get NPC type216 self._type = self.get_random_npc_type(cr, environment)217 if is_error_string(self._type):218 self._class = self._type219 else:220 self._class = self._type['Name']221 # Get NPC name222 self._name = random.choice(ALL_NAMES)223 # Short description224 def get_short_description(self):225 """ Build the short description226 >>> import random227 >>> random.seed(13)228 >>> # valid NPC found229 >>> npc = Npc("9", "")230 >>> npc.get_short_description()231 'abjurer'232 >>> # no NPC found233 >>> npc = Npc("15", "mountain")234 ERROR: No suitable NPC found235 >>> npc.get_short_description()236 'ERROR: No suitable NPC found'237 """238 # Log239 debug_print("get_short_description")240 # Build description241 description = self._class242 # Log243 debug_print(" description: " + description)244 # Return245 return description246 # Long description247 def get_long_description(self):248 """ Build the long description249 >>> import random250 >>> random.seed(13)251 >>> # valid NPC found252 >>> npc = Npc("9", "")253 >>> npc.get_long_description()254 'Uthemar - abjurer'255 >>> # no NPC found256 >>> npc = Npc("15", "mountain")257 ERROR: No suitable NPC found258 >>> npc.get_long_description()259 'ERROR: No suitable NPC found'260 """261 # Log262 debug_print("get_long_description")263 # Build description264 description = self._class265 if not is_error_string(description):266 description = self._name + " - " + self._class267 # Log268 debug_print(" description: " + description)269 # Return270 return description271# ------------------------------------------------------------------------------272# Encounter Class273# ------------------------------------------------------------------------------274class Encounter(Module):275 """ Encounter Class """276 # Init277 def __init__(self, cr, environment):278 """ Create new random Encounter279 >>> import random280 >>> random.seed(13)281 >>> # valid encounter build282 >>> Encounter("", "urban")283 warlock of the great old one and meenlock284 >>> # no encounter build285 >>> encounter = Encounter("30", "dummy")286 ERROR: No suitable NPC found287 ERROR: No suitable monster found288 """289 # Log290 debug_print("__init__ for Encounter")291 debug_print(" cr: " + cr)292 debug_print(" environment: " + environment)293 self.type = "Encounter"294 self._npc = None295 self._monsters = []296 self._number_monsters = 0297 # Pick random CR if no CR298 if cr == "":299 cr = to_string(random.randint(1, 20))300 # Get tier301 tier = to_int(cr) // 5302 if tier > 4:303 tier = 4304 # Add NPC, if tier 1+305 if tier >= 1:306 npc_cr = to_string(2 * int(to_int(cr) / 3))307 npc = Npc(npc_cr, environment)308 if not is_error_string(npc.get_short_description()):309 self._npc = npc310 self._number_monsters = 1311 # Check current CR and scaling312 if self._npc is not None and not is_error_string(313 self._npc.get_short_description()):314 current_cr = to_float(self._npc._type['CR'])315 current_scale = ALL_CR_SCALES[bisect(316 ALL_CR_SCALES, (self._number_monsters + 1, ))][1]317 else:318 current_cr = 0.0319 current_scale = 1.0320 # Add monsters321 max_ind = len(ALL_CRs)322 while (current_cr * current_scale <323 to_int(cr)) or (self._number_monsters < 2):324 max_cr = int((to_int(cr) / current_scale - current_cr) / 2)325 new_cr = random.choice(ALL_CRs[1 if tier == 0 else 5:6 +326 min(max(max_cr, 0), max_ind - 6)])327 new_monster = Monster(new_cr, environment)328 if is_error_string(new_monster.get_short_description()):329 break330 else:331 self._monsters.append(new_monster)332 self._number_monsters += 1333 current_cr += to_float(new_cr)334 current_scale = ALL_CR_SCALES[bisect(335 ALL_CR_SCALES, (self._number_monsters + 1, ))][1]336 # Short description337 def get_short_description(self):338 """ Build the short description339 >>> import random340 >>> random.seed(13)341 >>> encounter = Encounter("19", "")342 >>> encounter.get_short_description()343 'warlord and ankheg'344 """345 # Log346 debug_print("get_short_description")347 # Check for valid encounter348 if self._number_monsters == 0:349 return "ERROR: no encounter matches specification"350 description = ""351 # NPC352 if self._npc is not None:353 description += self._npc.get_short_description()354 if self._number_monsters > 2:355 description += ", "356 # Monsters357 for monster in self._monsters[:-1]:358 description += monster.get_short_description()359 if self._number_monsters > 2:360 description += ", "361 if self._number_monsters == 2:362 description += " "363 description += "and "364 description += self._monsters[-1].get_short_description()365 # Log366 debug_print(" description: " + description)367 # Return368 return description369 # Long description370 def get_long_description(self):371 """ Build the long description372 >>> import random373 >>> random.seed(13)374 >>> encounter = Encounter("19", "")375 >>> encounter.get_long_description()376 'NPC: \\n\\tUthemar - warlord\\nMonster 1:\\n\\tankheg - HP: 86-100\\n'377 """378 # Log379 debug_print("get_long_description")380 # Check for valid encounter381 if self._number_monsters == 0:382 return "ERROR: no encounter matches specification"383 description = ""384 # Npc385 if self._npc is not None:386 description += ("NPC: \n\t" + self._npc.get_long_description() +387 "\n")388 # Monsters389 i = 1390 for monster in self._monsters:391 description += ("Monster " + str(i) + ":\n\t" +392 monster.get_long_description() + "\n")393 i += 1394 # Log395 debug_print(" description: " + description)396 # Return397 return description398# ------------------------------------------------------------------------------399# PlotArc Class400# ------------------------------------------------------------------------------401class PlotArc(Module):402 """ PlotArc Class """403 # Init404 def __init__(self, cr, environment):405 """ Create new random Plot Arc406 >>> import random407 >>> random.seed(13)408 >>> # valid plot arc built409 >>> PlotArc("9", "")410 abjurer is trying to place a pawn in a position of power411 >>> # no plot arc built412 >>> arc = PlotArc("15", "underdark")413 ERROR: No suitable NPC found414 """415 # Log416 debug_print("__init__ for PlotArc")417 self.type = "Plot Arc"418 # Get random plot idea419 self._plot = random.choice(ALL_PLOTS)420 # Build random NPC421 self._npc = Npc(cr, environment)422 # Set environment type, if specified423 self._environment = environment424 # Short description425 def get_short_description(self):426 """ Build the short description427 >>> import random428 >>> random.seed(13)429 >>> # valid plot arc build430 >>> arc = PlotArc("9", "")431 >>> arc.get_short_description()432 'abjurer is trying to place a pawn in a position of power'433 >>> # no plot arc built434 >>> arc = PlotArc("15", "underdark")435 ERROR: No suitable NPC found436 >>> arc.get_short_description()437 'ERROR: No suitable plot arc built'438 """439 # Log440 debug_print("get_short_description")441 # NPC442 description = self._npc.get_short_description()443 # Plot444 description += " " + self._plot445 # Environment446 if self._environment:447 description += " in the " + self._environment448 # Log449 debug_print(" description: " + description)450 # Return451 if is_error_string(description):452 return "ERROR: No suitable plot arc built"453 return description454 # Long description455 def get_long_description(self):456 """ Build the long description457 >>> import random458 >>> random.seed(13)459 >>> # valid plot arc built460 >>> arc = PlotArc("9", "")461 >>> arc.get_long_description()462 'Arnbjorg - abjurer, is trying to place a pawn in a position of power'463 >>> # no plot arc built464 >>> arc = PlotArc("15", "underdark")465 ERROR: No suitable NPC found466 >>> arc.get_short_description()467 'ERROR: No suitable plot arc built'468 """469 # Log470 debug_print("get_long_description")471 # NPC472 description = self._npc.get_long_description() + ", "473 # Plot474 description += " " + self._plot475 # Environment476 if self._environment:477 description += " in the " + self._environment478 # Log479 debug_print(" description: " + description)480 # Return481 if is_error_string(description):482 return "ERROR: No suitable plot arc built"483 return description484# ------------------------------------------------------------------------------485# Get Module from intent486# ------------------------------------------------------------------------------...
test_setup_utils.py
Source:test_setup_utils.py
...43 os.path.isfile(filepath),44 "README file %r not found!" % filepath45 )46 def test_get_long_description_without_raise_errors(self):47 long_description = get_long_description(CREOLE_PACKAGE_ROOT, raise_errors=False)48 self.assertIn("=====\nabout\n=====\n\n", long_description)49 # Test created ReSt code50 from creole.rest2html.clean_writer import rest2html51 html = rest2html(long_description)52 self.assertIn("<h1>about</h1>\n", html)53 def test_get_long_description_with_raise_errors(self):54 long_description = get_long_description(CREOLE_PACKAGE_ROOT, raise_errors=True)55 self.assertIn("=====\nabout\n=====\n\n", long_description)56 def _tempfile(self, content):57 fd = tempfile.NamedTemporaryFile()58 path, filename = os.path.split(fd.name)59 fd.write(content)60 fd.seek(0)61 return path, filename, fd62 def test_tempfile_without_error(self):63 path, filename, fd = self._tempfile(b"== noerror ==")64 try:65 long_description = get_long_description(path, filename, raise_errors=True)66 self.assertEqual(long_description, "-------\nnoerror\n-------")67 finally:68 fd.close()69 def test_get_long_description_error_handling(self):70 """71 Test if get_long_description will raised a error, if description72 produce a ReSt error.73 We test with this error:74 <string>:102: (ERROR/3) Document or section may not begin with a transition.75 """76 path, filename, fd = self._tempfile(b"----")77 try:78 self.assertRaises(SystemExit, get_long_description, path, filename, raise_errors=True)79 finally:80 fd.close()81 def test_get_long_description_error_handling2(self):82 """83 Test if get_long_description will raised a error, if description84 produce a ReSt error.85 We test with this error:86 SystemExit: ReSt2html error: link scheme not allowed87 """88 path, filename, fd = self._tempfile(b"[[foo://bar]]")89# print(get_long_description(path, filename, raise_errors=True))90 try:91 self.assertRaises(SystemExit, get_long_description, path, filename, raise_errors=True)92 finally:93 fd.close()94 def test_wrong_path_without_raise_errors(self):95 self.assertEqual(96 get_long_description("wrong/path", raise_errors=False).replace("u'", "'"),97 "[Error: [Errno 2] No such file or directory: 'wrong/path/README.creole']\n"98 )99 def test_wrong_path_with_raise_errors(self):100 self.assertRaises(IOError, get_long_description, "wrong/path", raise_errors=True)101 def test_readme_encoding(self):102 long_description = get_long_description(TEST_README_DIR, filename=TEST_README_FILENAME, raise_errors=True)103 if PY3:104 self.assertTrue(isinstance(long_description, TEXT_TYPE))105 else:106 self.assertTrue(isinstance(long_description, BINARY_TYPE))107 txt = "German Umlaute: ä ö ü à à à Ã"108 if not PY3:109 txt = txt.encode("utf-8")110 self.assertIn(txt, long_description)111if __name__ == '__main__':...
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!!