Best Python code snippet using ATX
proof_scanner.py
Source:proof_scanner.py
1PLUS = 1; # +2MINUS = 2; # -3INCC = 3; # ++4DECC = 4; # --5MULT = 5; # *6DIV = 6; # /7MOD = 7; # %8AND = 8; # and9OR = 9; # or10NOT = 10; # not | !11EQ = 11; # =12NE = 12; # !=13LT = 13; # <14GT = 14; # >15LE = 15; # <=16GE = 16; # >=17BECOMES = 17; # :=18LINEEND = 18; # \n19LPAREN = 19; # (20RPAREN = 20; # )21LBRAK = 21; # [22RBRAK = 22; # ]23LCURLY = 21; # {24RCURLY = 22; # }25COMMA = 23; # ,26INTEGER = 30; # `val` holds corresponding number27REAL = 31; # `val` holds corresponding number28STRING_LITERAL = 32; # `val` holds string contents29IDENT = 33; # `val` holds string identifier30INT = 40 # int31FLOAT = 41 # float32STRING = 42 # str33BOOL = 43 # bool34DO = 50; # do35END = 51; # end36IF = 52; # if37THEN = 53; # then38ELSE = 54; # else39WHILE = 55; # while40PROGRAM = 55; # program41FUNCTION = 56; # define42RETURN = 57; # return43OUTPUT = 58; # output44COLON = 80; # :45EOF = 99;46KEYWORDS = {'int': INT, 'real': FLOAT, 'str': STRING, 'bool': BOOL, 'if': IF,47 'then': THEN, 'else': ELSE, 'do': DO, 'end': END, 'while': WHILE,48 'and': AND, 'or': OR, 'not': NOT, 'program': PROGRAM, 'define': FUNCTION,49 'return': RETURN, 'output': OUTPUT}50def init(file : str, src : str) -> None:51 # Initializes scanner for new source code52 global line, lastline, errline, pos, lastpos, errpos, filename53 global sym, val, error, source, index, lastsym, lastval54 line, lastline, errline = 0, 0, 055 pos, lastpos, errpos = 0, 0, 056 sym, val, error = None, None, False57 lastsym, lastval = None, None58 source, index, filename = src, 0, file59 getChar(); getSym()60def getChar():61 global line, lastline, pos, lastpos, ch, index62 if index == len(source): ch = chr(0)63 else:64 ch, index = source[index], index + 165 lastpos = pos66 if ch == '\n':67 pos, line = 0, line + 168 else:69 lastline, pos = line, pos + 170def mark(msg):71 global errline, errpos, error, filename, sym72 if not error or lastline > errline or lastpos > errpos:73 print('file',filename,'error: line', lastline+1, 'pos', lastpos, msg,'<last sym',str(sym)+'>')74 error = True75 errline, errpos = lastline, lastpos76def number():77 global sym, val78 sym, val, frac, div, lastval = INTEGER, 0, 0, 10, val79 while '0' <= ch <= '9':80 val = 10 * val + int(ch)81 getChar()82 if ch == '.':83 getChar()84 val = REAL85 while '0' <= ch <= '9':86 val = val + int(ch) / div87 getChar(); div /= 1088 if val >= 2**31:89 mark('number too large'); val = 090def raw_string(open : str):91 global sym, val, lastval92 getChar()93 start = index - 194 while chr(0) != ch != open: getChar()95 if ch == chr(0): mark('string not terminated'); sym = None;96 else:97 sym = STRING_LITERAL98 val, lastval = source[start:index-1], val99 getChar(); # Get rid of terminating '100def identKW():101 global sym, val, lastval102 start = index - 1103 while ('A' <= ch <= 'Z') or ('a' <= ch <= 'z') or ('0' <= ch <= '9') or (ch == '_'): getChar()104 val, lastval = source[start:index-1], val105 sym = KEYWORDS[val] if val in KEYWORDS else IDENT # (USRFUNC if val in usrfunc else IDENT)106def blockcomment():107 while chr(0) != ch:108 if ch == '*':109 getChar()110 if ch == '/':111 getChar(); break112 else:113 getChar()114 if ch == chr(0): mark('comment not terminated')115 else: getChar()116# Keeps taking inputs until a newline or EOF is found117def linecomment():118 while chr(0) != ch != '\n': getChar()119# Determines the next symbol in the input120def getSym():121 global sym, lastsym122 lastsym = sym123 while chr(0) < ch <= ' ' and ch != '\n':124 getChar()125 if 'A' <= ch <= 'Z' or 'a' <= ch <= 'z': identKW()126 elif '0' <= ch <= '9': number()127 elif ch == "'" or ch == '"': raw_string(ch)128 elif ch == '\n': getChar(); sym = LINEEND129 elif ch == '+':130 getChar();131 if ch == '+': getChar(); sym = INCC132 else: sym = PLUS133 elif ch == '-':134 getChar();135 if ch == '-': getChar(); sym = DECC136 else: sym = MINUS137 elif ch == '*': getChar(); sym = MULT138 elif ch == '/':139 getChar();140 if ch == '/': linecomment()141 else: sym = DIV142 elif ch == '%': getChar(); sym = MOD143 elif ch == ':':144 getChar();145 if ch == '=': getChar(); sym = BECOMES146 else: sym = COLON147 elif ch == '=': getChar(); sym = EQ148 elif ch == '!':149 getChar();150 if ch == '=': getChar(); sym = NE151 # FOR SIMPLER WRITING152 else: sym = NOT153 elif ch == '<':154 getChar();155 if ch == '=': getChar(); sym = LE156 else: sym = LT157 elif ch == '>':158 getChar();159 if ch == '=': getChar(); sym = GE160 else: sym = GT161 elif ch == ';': getChar(); sym = LINEEND162 elif ch == '\n': getChar(); sym = LINEEND163 elif ch == ',': getChar(); sym = COMMA164 elif ch == '(': getChar(); sym = LPAREN165 elif ch == ')': getChar(); sym = RPAREN166 elif ch == '[': getChar(); sym = LBRAK167 elif ch == ']': getChar(); sym = RBRAK168 elif ch == '|':169 getChar()170 if ch == '|': getSym()171 sym = OR172 mark("boolean or operation is written as 'or'")173 elif ch == '&':174 getChar()175 if ch == '&': getSym()176 sym = AND177 mark("boolean and operation is written as 'and'")178 # FOR SIMPLER WRITING179 elif ch == '{': getChar(); sym = DO # LCURLY180 elif ch == '}': getChar(); sym = END # RCURLY181 elif ch == ';': getChar(); sym = LINEEND182 elif ch == chr(0): sym = EOF...
SC.py
Source:SC.py
1TIMES = 1; DIV = 2; MOD = 3; AND = 4; PLUS = 5; MINUS = 62OR = 7; EQ = 8; NE = 9; LT = 10; GT = 11; LE = 12; GE = 133PERIOD = 14; COMMA = 15; COLON = 16; NOT = 17; LPAREN = 184RPAREN = 19; LBRAK = 20; RBRAK = 21; LARROW = 22; RARROW = 235LBRACE = 24; RBRACE = 25; CARD = 26; COMPLEMENT = 27; UNION = 286INTERSECTION = 29; ELEMENT = 30; SUBSET = 31; SUPERSET = 327DOTDOT = 33; THEN = 34; DO = 35; BECOMES = 36; NUMBER = 378IDENT = 38; SEMICOLON = 39; ELSE = 40; IF = 41; WHILE = 429CONST = 43; TYPE = 44; VAR = 45; SET = 46; PROCEDURE = 4710PROGRAM = 48; INDENT = 49; DEDENT = 50; EOF = 51; FUNC = 5211def init(src):12 global line, lastline, pos, lastpos13 global ch, sym, val, source, index, indents14 line, lastline = 0, 115 pos, lastpos = 1, 116 ch, sym, val, source, index = '\n', None, None, src, 017 indents = [1]; getChar(); getSym()18def getChar():19 global line, lastline, pos, lastpos, ch, index20 if index == len(source): ch, index, pos = chr(0), index + 1, 121 else:22 lastpos = pos23 if ch == '\n':24 pos, line = 1, line + 125 else:26 lastline, pos = line, pos + 127 ch, index = source[index], index + 128def mark(msg):29 raise Exception('line ' + str(lastline) + ' pos ' + str(lastpos) + ' ' + msg)30def number():31 global sym, val32 sym, val = NUMBER, 033 while '0' <= ch <= '9':34 val = 10 * val + int(ch)35 getChar()36 if val >= 2**31: mark('number too large')37KEYWORDS = {'div': DIV, 'mod': MOD, 'and': AND, 'or': OR, 'then': THEN, 'do': DO,38 'else': ELSE, 'if': IF, 'while': WHILE, 'const': CONST, 'type': TYPE,39 'var': VAR, 'set': SET, 'procedure': PROCEDURE, 'program': PROGRAM}40def identKW():41 global sym, val42 start = index - 143 while ('A' <= ch <= 'Z') or ('a' <= ch <= 'z') or ('0' <= ch <= '9'): getChar()44 val = source[start : index - 1]45 sym = KEYWORDS[val] if val in KEYWORDS else IDENT46def comment():47 if ch == '/': getChar()48 else: mark('// expected')49 while chr(0) != ch != '\n': getChar()50def getSym():51 global sym, indents, newline52 if pos < indents[0]:53 indents = indents[1:]; sym = DEDENT54 else:55 while ch in ' /':56 if ch == ' ': getChar() # skip blanks between symbols57 else: comment()58 if ch == '\n': # possibly INDENT, DEDENT59 while ch == '\n': # skip blank lines60 getChar()61 while ch in ' /':62 if ch == ' ': getChar() # skip indentation63 else: comment()64 if pos < indents[0]: sym, indents = DEDENT, indents[1:]; return65 elif pos > indents[0]: sym, indents = INDENT, [pos] + indents; return66 newline = pos == indents[0]67 if 'A' <= ch <= 'Z' or 'a' <= ch <= 'z': identKW()68 elif '0' <= ch <= '9': number()69 elif ch == 'Ã': getChar(); sym = TIMES70 elif ch == '+': getChar(); sym = PLUS71 elif ch == '-': getChar(); sym = MINUS72 elif ch == '=': getChar(); sym = EQ73 elif ch == 'â ': getChar(); sym = NE74 elif ch == '<': getChar(); sym = LT75 elif ch == 'â¤': getChar(); sym = LE76 elif ch == '>': getChar(); sym = GT77 elif ch == 'â¥': getChar(); sym = GE78 elif ch == ';': getChar(); sym = SEMICOLON79 elif ch == ',': getChar(); sym = COMMA80 elif ch == ':':81 getChar()82 if ch == '=': getChar(); sym = BECOMES83 else: sym = COLON84 elif ch == '.':85 getChar();86 if ch == '.': getChar(); sym = DOTDOT87 else: sym = PERIOD88 elif ch == '¬': getChar(); sym = NOT89 elif ch == '(': getChar(); sym = LPAREN90 elif ch == ')': getChar(); sym = RPAREN91 elif ch == '[': getChar(); sym = LBRAK92 elif ch == ']': getChar(); sym = RBRAK93 elif ch == 'â': getChar(); sym = LARROW94 elif ch == 'â': getChar(); sym = RARROW95 elif ch == '{': getChar(); sym = LBRACE96 elif ch == '}': getChar(); sym = RBRACE97 elif ch == '#': getChar(); sym = CARD98 elif ch == 'â': getChar(); sym = COMPLEMENT99 elif ch == 'âª': getChar(); sym = UNION100 elif ch == 'â©': getChar(); sym = INTERSECTION101 elif ch == 'â': getChar(); sym = ELEMENT102 elif ch == 'â': getChar(); sym = SUBSET103 elif ch == 'â': getChar(); sym = SUPERSET104 elif ch == chr(0): sym = EOF...
secure-password-generator.py
Source:secure-password-generator.py
1import random2import time3charTypes = ['digit', 'letter', 'symbol']4digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']5letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']6symbols = ['!', '?', '$', '&', '#', '%', '*']7def getChar():8 charType = random.choice(charTypes)9 if charType == 'digit':10 char = random.choice(digits)11 elif charType == 'letter':12 char = random.choice(letters)13 elif charType == 'symbol':14 char = random.choice(symbols)15 return char16 17char1 = getChar()18char2 = getChar()19char3 = getChar()20char4 = getChar()21char5 = getChar()22char6 = getChar()23char7 = getChar()24char8 = getChar()25char9 = getChar()26char10 = getChar()27char11 = getChar()28char12 = getChar()29char13 = getChar()30char14 = getChar()31char15 = getChar()32char16 = getChar()33char17 = getChar()34char18 = getChar()35char19 = getChar()36char20 = getChar()37char21 = getChar()38char22 = getChar()39char23 = getChar()40char24 = getChar()41char25 = getChar()42result = char1 + char2 + char3 + char4 + char5 + char6 + char7 + char8 + char9 + char10 + char11 + char12 + char13 + char14 + char15 + char16 + char17 + char18 + char19 + char20 + char21 + char22 + char23 + char24 + char2543print('WARNING: The generated password is NOT easy to remember! It is recommended that you save it somewhere safe.')44print('Your generated password is: ' + result)...
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!!