Best Python code snippet using lemoncheesecake
test_rule_set_parser.py
Source:test_rule_set_parser.py
1import pytest2from fact_values.fact_value import FactValue3from fact_values.fact_value_type import FactValueType4from nodes.dependency import Dependency5from nodes.dependency_type import DependencyType6from nodes.line_type import LineType7from nodes.meta_type import MetaType8from nodes.metadata_line import MetadataLine9from nodes.node import Node10from nodes.node_set import NodeSet11from rule_parser.rule_set_parser import RuleSetParser12class RuleSetParserTest:13 meta_parent_1 = "INPUT the groom's name AS TEXT"14 meta_parent_2 = "FIXED the groom's homepage IS https://www.theGroomHomepage.com.au"15 meta_parent_3 = "FIXED the wedding booking schedule for the venue AS LIST"16 first_key_group = "ITEM"17 meta_child_1 = "ITEM someone"18 meta_child_2 = "ITEM somebody"19 meta_child_3 = "ITEM nobody"20 first_key_group_2 = "AND"21 first_key_group_3 = "OR"22 value_con = "person must meet military service criteria"23 comparison = "enlistment date >= 01/07/1951"24 exp_con = "yearly period of service by 6/04/1994 IS CALC (6/04/1994 - enlistment date)"25 rule_set_parser = RuleSetParser()26 def __init__(self):pass27 def handle_parent_value_con(self):28 self.rule_set_parser.handle_parent(self.value_con, 6)29 def handle_child_com(self):30 self.rule_set_parser.handle_child(self.value_con, self.comparison, self.first_key_group_2, 7)31 def handle_parent_exp_con(self):32 self.rule_set_parser.handle_parent(self.exp_con, 8)33 def handle_parent_meta_input(self):34 self.rule_set_parser.handle_parent(self.meta_parent_1, 1)35 def handle_parent_meta_fixed(self):36 self.rule_set_parser.handle_parent(self.meta_parent_2, 2)37 def handle_parent_meta_with_child(self):38 self.rule_set_parser.handle_parent(self.meta_parent_3, 3)39 def handle_child_meta_item(self):40 self.rule_set_parser.handle_child(self.meta_parent_3, self.meta_child_1, "", 4)41 def handle_child_meta_item_2(self):42 self.rule_set_parser.handle_child(self.meta_parent_3, self.meta_child_2, "", 5)43 def create_dependency_matrix(self):44 self.rule_set_parser.get_node_set().set_dependency_matrix(45 self.rule_set_parser.create_dependency_matrix())46@pytest.fixture(scope="function")47def rule_set_parser_test():48 instance = RuleSetParserTest()49 yield instance50 return instance51def test_handle_parent(rule_set_parser_test):52 rule_set_parser_test.handle_parent_value_con()53 rule_set_parser_test.handle_parent_exp_con()54 rule_set_parser_test.handle_parent_meta_input()55 rule_set_parser_test.handle_parent_meta_fixed()56 rule_set_parser_test.handle_parent_meta_with_child()57 rule_set_parser_test.create_dependency_matrix()58 passTest = True59 node_set: NodeSet = rule_set_parser_test.rule_set_parser.get_node_set()60 is_value_con = node_set.get_node_dictionary().get('person must meet military service criteria').get_line_type() \61 is LineType.VALUE_CONCLUSION62 if is_value_con is not True:63 passTest = False64 is_expr_con = node_set.get_node_dictionary().get('yearly period of service by 6/04/1994 IS CALC (6/04/1994 - enlistment date)').get_line_type()\65 is LineType.EXPR_CONCLUSION66 if is_expr_con is not True:67 passTest = False68 fact_value: FactValue = node_set.get_input_dictionary()["the groom's name"]69 is_string = fact_value.get_value_type() is FactValueType.STRING70 if is_string is not True:71 passTest = False72 second_fact_value: FactValue = node_set.get_fact_dictionary()["the groom's homepage"]73 is_url = second_fact_value.get_value_type() is FactValueType.URL74 value_is_correct = second_fact_value.get_value() == "https://www.theGroomHomepage.com.au"75 if is_url is not True and value_is_correct is True:76 passTest = False77 third_fact_value: FactValue = node_set.get_fact_dictionary().get("the wedding booking schedule for the venue")78 is_list = third_fact_value.get_value_type() == FactValueType.LIST79 if is_list is not True:80 passTest = False81 assert passTest is True82def test_handle_child(rule_set_parser_test):83 # rule_set_parser_test.handle_parent_value_con()84 rule_set_parser_test.handle_child_com()85 # rule_set_parser_test.handle_parent_exp_con()86 # rule_set_parser_test.handle_parent_meta_input()87 # rule_set_parser_test.handle_parent_meta_fixed()88 # rule_set_parser_test.handle_parent_meta_with_child()89 rule_set_parser_test.handle_child_meta_item()90 rule_set_parser_test.handle_child_meta_item_2()91 rule_set_parser_test.create_dependency_matrix()92 passTest = True93 node_set: NodeSet = rule_set_parser_test.rule_set_parser.get_node_set()94 node: Node = node_set.get_node_dictionary()["enlistment date >= 01/07/1951"]95 is_comparison = node.get_line_type() is LineType.COMPARISON96 parent_node: Node = node_set.get_node_dictionary()['person must meet military service criteria']97 dependency: DependencyType = node_set.get_dependency_matrix()\98 .get_dependency_type(parent_node.get_node_id(), node.get_node_id())99 is_and_dependency = dependency == DependencyType.get_and()100 child_node_list: list = node_set.get_dependency_matrix().get_to_child_dependency_list(parent_node.get_node_id())101 has_one_item = len(child_node_list) == 1102 correct_child = child_node_list[0] == node.get_node_id()103 if is_comparison is not True \104 and is_and_dependency is not True \105 and has_one_item is not True \106 and correct_child is not True:107 passTest = False108 fixed_item: FactValue = node_set.get_fact_dictionary()["the wedding booking schedule for the venue"]109 child_item_list = list(fixed_item.get_value())110 has_two_items = len(child_item_list) == 2111 has_all_items = True112 print("*****************: ", node_set.get_fact_dictionary())113 if FactValue(child_item_list[0]).get_value() != "someone" and \114 FactValue(child_item_list[1]).get_value() != "somebody":115 has_all_items = False116 if has_two_items is not True and has_all_items is not True:117 passTest = False...
quest_system.py
Source:quest_system.py
1from ursina import *2import my_json3import game4import callbacks5import pda6from language_system import TKey78system = None910class Quests():11 def __init__(self):12 global system13 self.quests_list = []14 self.active_quest = None15 self.player = None16 system = self17 self.q_file = my_json.read("assets/gameplay/quests")1819 def add_quest(self,quest_id):20 for q in self.q_file:21 if q["id"] == quest_id:22 self.quests_list.append (23 quest_element (q["id"], q["title"], q["description"], q["quest_items"],24 q["reward"],q["pda"] if "pda" in q else None))25 if "pda" in q:26 invoke(game.enable_pda_in_pause, True, delay=0.0001)27 invoke(game.show_pda_icon_ui, True, delay=0.0001)28 break2930 invoke(game.show_message,TKey ("message.new.quest"), 5,delay=0.001)3132 invoke(callbacks.on_quest_add,self)3334 def has_quest(self,quest_id):35 for q in self.quests_list:36 if q.id == quest_id:37 return True38 break39 else:40 return False41 break4243 def get_quest(self,quest_id):44 for q in self.quests_list:45 if q.id == quest_id:46 return q47 break4849class quest_element():50 def __init__(self,id=None,title="",description="",quest_items=[],reward={},pda=None):51 self.id = id52 self.title = TKey(title)53 self.description = "<default>"+TKey(description)54 self.quest_items = quest_items55 self.reward = reward56 self.items_file = my_json.read ("assets/gameplay/items")57 self.pda = pda5859 if self.quest_items:60 translated_keys = []61 for qi in self.quest_items:62 translated_keys.append(TKey(self.items_file[qi]["name"]))6364 self.description += "\n\n>> <rgb(238, 157, 49)>"+"<default>, <rgb(238, 157, 49)>".join(translated_keys)6566 #quest fail67 def fail(self):68 for q in system.quests_list:69 if q.id == self.id:70 system.quests_list.remove(q)7172 def complete(self):73 has_all_items = False74 q_item_need_count = 075 print("[DEBUG QUEST] need item count [{0}]".format(q_item_need_count))76 if "money" in self.reward and self.quest_items is None:77 system.player.money += self.reward["money"]78 invoke (game.show_message, "Quest complete!\n+{0}$".format (self.reward["money"]), 5, delay=0.001)79 for q in system.quests_list:80 if q.id == self.id:81 system.quests_list.remove(q)8283 for itm in self.quest_items:84 for inv_itm in game.get_player().inventory.items_in_inventory:85 if itm == inv_itm.item_id:86 print("Item [{0}] in inventory!".format(itm))87 q_item_need_count += 188 print ("[DEBUG QUEST] need item count [{0}]".format (q_item_need_count))89 if q_item_need_count == len(self.quest_items):90 has_all_items = True9192 if has_all_items:93 if "money" in self.reward:94 game.get_player().money += self.reward["money"]95 invoke (game.show_message, "Quest complete!\n+{0}$".format(self.reward["money"]), 5, delay=0.001)9697 if "items" in self.reward:98 for items in self.reward["items"]:99 game.get_player().inventory.add_item(items)100101 if "add_e_key" in self.reward:102 for add_e_key in self.reward["add_e_key"]:103 game.add_e_key(add_e_key)104 if "del_e_key" in self.reward:105 for del_e_key in self.reward["del_e_key"]:106 game.del_e_key(del_e_key)107108 for q_itm in self.quest_items:109 game.get_player().inventory.delete_item(q_itm)110 print("[{0}] was deleted from inventory by quest!".format(q_itm))111112 if self.pda:113 game.get_player().pause_menu.pda_window.delete_marker(self.pda["id"])114 print("[DEBUG QUEST]: Quest marker [{0}] was deleted!".format(self.pda["id"]))115116 for q in system.quests_list:117 if q.id == self.id:118 system.quests_list.remove(q)119 else:120 invoke (game.show_message, "You don't have quest items\nfor complete this quest!", 5, delay=0.001)121122123124125if __name__ == "__main__":126 app = Ursina()127
...
Drone.py
Source:Drone.py
...22 if self.time_to_ready > 0:23 self.time_to_ready -= 124 def is_ready(self):25 return self.time_to_ready == 026 def has_all_items(self) -> bool:27 return self.order.has_all_items()28 def get_remaining_load(self) -> int:29 return max(self.max_load - self.equipment.count(), 0)30 def set_order(self, order: Order):31 self.order = order32 self.status = DroneStatus.NO_TARGET33 logging.debug(f"Turn {self.turn}: {self} will be delivering {order}")34 def fly_to_load(self, warehouse: Warehouse):35 logging.debug(f"Turn {self.turn}: {self} will be flying to {warehouse}")36 fly_time = self.fly_to(warehouse)37 loading_time = self.load(self.order.items, warehouse.items)38 logging.debug(f"Turn {self.turn}: {self} will take next task in {fly_time}+{loading_time}={fly_time+loading_time} turns")39 self.time_to_ready = fly_time + loading_time40 self.status = DroneStatus.NO_TARGET41 def load(self, order_items: ItemList, warehouse_items: ItemList):42 time_to_load = 043 for item in order_items.values():44 if item.type in warehouse_items.keys():45 result_quantity = min(self.get_remaining_load(), item.quantity, warehouse_items[item.type].quantity)46 if result_quantity != 0:47 self.equipment[item.type].quantity += result_quantity48 order_items[item.type].quantity -= result_quantity49 warehouse_items[item.type].quantity -= result_quantity50 time_to_load += 151 logging.debug(f"Turn {self.turn}: {self} will be loading {self.equipment}")52 return time_to_load # Loading takes 1 turn for each type53 def unload(self):54 logging.debug(f"Turn {self.turn}: {self} will be unloading {self.equipment}")55 self.equipment.clear()56 return 1 # Unloading always takes 1 turn57 def fly_to_order(self):58 logging.debug(f"Turn {self.turn}: {self} will be flying to {self.order}")59 fly_time = self.fly_to(self.order)60 unloading_time = self.unload()61 logging.debug(f"Turn {self.turn}: {self} will take next task in {fly_time}+{unloading_time}={fly_time+unloading_time} turns")62 self.time_to_ready += fly_time + unloading_time63 if self.order.has_all_items():64 self.status = DroneStatus.READY_TO_SCORE65 else:66 self.status = DroneStatus.NO_TARGET67 def fly_to(self, target) -> int:68 time_to_ready = math.ceil(Utilities.calc_distance(self.coordinates, target.coordinates))69 self.coordinates = target.coordinates70 return time_to_ready71 def calc_score(self, max_turns, turn) -> int:72 score = math.ceil((max_turns - (turn + 1.0)) / max_turns * 100.0)73 logging.debug(f"Turn {self.turn}: {self} finished {self.order}, score = {score}")74 self.status = DroneStatus.NO_ORDER75 self.order = None76 return score77 def __repr__(self):...
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!!