How to use str_to_type method in Ddt

Best Python code snippet using ddt_python

generate_tokens.py

Source:generate_tokens.py Github

copy

Full Screen

1from typing import List, Dict2from src.token.assignment import Assignment, AssignmentOperator3from src.token.binary_operation import BinaryOperation, BinaryOperator4from src.token.compound import Compound5from src.token.declaration import Declaration6from src.token.for_flow import ForFlow7from src.token.function_call import FunctionCall8from src.token.identifier import Identifier9from src.token.if_flow import IfFlow10from src.token.literal import Literal11from src.token.token import Token, LiteralType12# Delay execution13from src.token.unary_operation import UnaryOperation, UnaryOperator14from src.token.while_flow import WhileFlow15def str_to_cast(value, type_str):16 if type_str == "int":17 return int(value) if value else int()18 if type_str == "string" or type_str == "char":19 res = str(value) if value else str()20 return res.strip("\"")21 raise Exception("Could not cast value")22str_to_type: Dict[str, LiteralType] = {23 "int": LiteralType.INT,24 "string": LiteralType.STR,25 # TODO: Add char support if required26 "char": LiteralType.STR,27 "list": LiteralType.LIST28}29def generate_tokens(ast):30 tokens: List[Token] = []31 # find main32 nodes = get_main_nodes(ast)33 # generate token in main34 for node in nodes:35 token = tokenize_node(node)36 tokens.append(token)37 return tokens38def tokenize_node(node):39 node_type = node["_nodetype"]40 if node_type == "Decl":41 return handle_declaration(node)42 if node_type == "DeclList":43 return Compound([tokenize_node(n) for n in node["decls"]])44 if node_type == "FuncCall":45 return handle_function_call(node)46 if node_type == "Assignment":47 return handle_assignment(node)48 if node_type == "ID":49 # RHS value is in variables50 return handle_id(node)51 if node_type == "Constant":52 # RHS value is literal53 return handle_constant(node)54 if node_type == "InitList":55 exprs = node["exprs"]56 list_vals = []57 for expr in exprs:58 list_vals.append(tokenize_node(expr))59 return Literal(list_vals, LiteralType.LIST)60 if node_type == "UnaryOp":61 op = node["op"]62 return UnaryOperation(tokenize_node(node["expr"]), UnaryOperator(op))63 if node_type == "BinaryOp":64 op = node["op"]65 return BinaryOperation(tokenize_node(node["left"]), tokenize_node(node["right"]), BinaryOperator(op))66 if node_type == "Return":67 return tokenize_node(node["expr"])68 if node_type == "While":69 return WhileFlow(tokenize_node(node["cond"]), tokenize_node(node["stmt"]))70 if node_type == "If":71 return IfFlow(tokenize_node(node["cond"]), tokenize_node(node["iftrue"]), tokenize_node(node["iffalse"]))72 if node_type == "Compound":73 return Compound([tokenize_node(n) for n in node["block_items"]])74 if node_type == "For":75 return ForFlow(tokenize_node(node["init"]),76 tokenize_node(node["cond"]),77 tokenize_node(node["next"]),78 tokenize_node(node["stmt"]))79 raise Exception(f"Could not tokenize {node}")80def handle_assignment(node):81 op = node["op"]82 lvalue = node["lvalue"]83 rvalue = node["rvalue"]84 return Assignment(tokenize_node(lvalue), tokenize_node(rvalue), AssignmentOperator(op))85def handle_function_call(node):86 name = node["name"]["name"]87 args = node["args"]["exprs"] if node["args"] is not None else []88 params = []89 for arg in args:90 param = tokenize_node(arg)91 params.append(param)92 return FunctionCall(name, params)93def handle_declaration(node):94 # returns declaration variable95 init = node["init"]96 name = node["name"]97 identifier_type = get_identifier_type(node)98 if init is None:99 # no RHS value100 return Declaration(name, str_to_type[identifier_type], handle_empty_declaration(node))101 return Declaration(name, str_to_type[identifier_type], tokenize_node(init))102def handle_constant(node):103 identifier_type = node["type"]104 return Literal(str_to_cast(node["value"], identifier_type),105 str_to_type[identifier_type])106def handle_id(node):107 name = node["name"]108 return Identifier(name, 0, LiteralType.VOID)109def handle_empty_declaration(node):110 identifier_type = get_identifier_type(node)111 return Literal(str_to_cast(None, identifier_type), str_to_type[identifier_type])112def get_main_nodes(ast):113 main_node = ast["ext"][0]114 main_node_items = main_node["body"]["block_items"]115 return main_node_items116def get_identifier_type(node):117 if node is None or "_nodetype" not in node:118 raise Exception("Cannot find identifier_type of node")119 # FIXME: Feels Hacky120 if node["_nodetype"] == "ArrayDecl":121 type = get_identifier_type(node["type"])122 if type == "char":123 return "string"124 return "list"125 if node["_nodetype"] == "IdentifierType":126 return node["names"][0]...

Full Screen

Full Screen

ActionKeyboard.py

Source:ActionKeyboard.py Github

copy

Full Screen

1import pyautogui2class ActionKeyboard:3 """键盘操作4 """5 @staticmethod6 def hotkey(*keys):7 """8 发送热键9 Examples:10 ```python11 action: hotkey("ctrl","c")12 ```13 ```python14 action: hotkey("alt","tab")15 ```16 Args:17 *keys: 热键组合,多键组合用多个参数表示18 Returns:19 None20 """21 pyautogui.hotkey(*keys)22 @staticmethod23 def type(str_to_type, interval=0):24 """25 把指定字符串以键盘敲击的形式输入26 Args:27 str_to_type (str): 要输入的字符串28 interval (float): 每个输入字符之间的时间间隔,单位是秒29 Returns:30 None31 """32 if interval == 0:33 pyautogui.write(str_to_type)34 else:35 pyautogui.write(str_to_type, interval)36 @staticmethod37 def press(key_or_keys, interval=0):38 """39 点击特定的键40 Args:41 key_or_keys (str,List[str]): 表示键的字符串或者字符串数组42 interval (float): 第一个参数是数组,那么这里表示多个敲击之间的时间间隔,单位是秒43 Returns:44 """45 if interval == 0:46 pyautogui.press(key_or_keys)47 else:...

Full Screen

Full Screen

typing bot.py

Source:typing bot.py Github

copy

Full Screen

1from pynput.mouse import Button2from selenium import webdriver3import selenium4from selenium.webdriver.common.action_chains import ActionChains5from selenium.webdriver.common.keys import Keys6from selenium.webdriver.common.by import By7from selenium.webdriver.support import expected_conditions as EC8from selenium.webdriver.support.ui import WebDriverWait9import pynput, time1011PATH = "C:\Program Files (x86)\chromedriver.exe"12driver = webdriver.Chrome(PATH)13driver.get("https://humanbenchmark.com/tests/typing")1415str_to_type = ""16# actions = ActionChains(driver)17keyboard = pynput.keyboard.Controller()1819time.sleep(5)2021text_to_type = driver.find_elements(By.CLASS_NAME, "incomplete")22total_length = len(text_to_type)23for i in range(total_length):24 if text_to_type[i].text:25 str_to_type += text_to_type[i].text26 else:27 str_to_type += " "28 print(i + 1, "/", total_length)2930# actions.send_keys(str_to_type)31# actions.perform()32keyboard.type(str_to_type)3334# while True:35# try:36# target = driver.find_element(By.CLASS_NAME, "incomplete")37# if target.text:38# actions.send_keys(target.text)39# else:40# actions.send_keys(Keys.SPACE)41# actions.perform()42# except:43# break4445time.sleep(30)46 ...

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 Ddt 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