Best Python code snippet using avocado_python
error.py
Source:error.py
1from error_exit import error_exit2from parenthesis import parse_parenthesis3def check_parenthesis(parents):4 if parents.count("(") != parents.count(")"):5 error_exit("Bad Syntax, parenthesis unbalanced")6 _ = parse_parenthesis(parents)7 found_opening = False8 for letter in parents:9 if found_opening:10 if not (letter.isalpha() or letter == "(" or letter == "!"):11 error_exit("Bad Syntax, parenthesis with non-alphabet symbol")12 if letter == ")":13 if not (last.isalpha() or last == ")"):14 error_exit("Bad Syntax, parenthesis with non-alphabet symbol")15 found_opening = False16 if letter == "(":17 found_opening = True18 last = letter19def check_parents(parents):20 parents = parents.replace('(', '').replace(')', '').split("+")21 for parent in parents:22 if not parent:23 error_exit("Bad Syntax, + missing symbol")24 if len(parent) == 1: ## AND25 if not parent.isalpha():26 error_exit("Bad Syntax, non-alphabet symbol")27 elif len(parent) == 2: ## NOT28 if parent[0] != "!" or not parent[1].isalpha():29 error_exit("Bad Syntax, combined conditions")30 else: ## OR / XOR31 parents_or = parent.split("|")32 for parent in parents_or:33 if not parent:34 error_exit("Bad Syntax, | missing symbol")35 if len(parent) == 1: ## OR36 if not parent.isalpha():37 error_exit("Bad Syntax, non-alphabet symbol with |")38 elif len(parent) == 2: ## OR NOT39 if parent[0] != "!" or not parent[1].isalpha():40 error_exit("Bad Syntax, 2 combined conditions")41 else: ## XOR42 parents_xor = parent.split("^")43 for parent in parents_xor:44 if not parent:45 error_exit("Bad Syntax, ^ missing symbol")46 if len(parent) == 1: ## XOR47 if not parent.isalpha():48 error_exit("Bad Syntax, non-alphabet symbol with ^")49 elif len(parent) == 2: ## XOR NOT50 if parent[0] != "!" or not parent[1].isalpha():51 error_exit("Bad Syntax, many combined conditions")52 else:53 for content in parents_xor:54 i = 055 for letter in content:56 i += 157 last = letter58 j = 059 for letter in content:60 j += 161 if i == j:62 break63 if letter != "!":64 error_exit("Bad Syntax, too many combined conditions")65 if not last.isalpha():66 error_exit("Bad Syntax, too many combined conditions")67def check_children(children):68 if "(" in children or ")" in children:69 error_exit("Bad Syntax, parenthesis in conclusion")70 children = children.split("+")71 for child in children:72 if not child:73 error_exit("Bad Syntax, + missing symbol in conclusion")74 if len(child) == 1: ## AND75 if not child.isalpha():76 error_exit("Bad Syntax, non-alphabet symbol")77 elif len(child) == 2: ## NOT78 if child[0] != "!" or not child[1].isalpha():79 error_exit("Bad Syntax, combined conditions")80 else: ## OR / XOR81 children_or = child.split("|")82 for child_or in children_or:83 if not child_or:84 error_exit("Bad Syntax, | missing symbol")85 if len(child_or) == 1: ## OR86 if not child_or.isalpha():87 error_exit("Bad Syntax, non-alphabet symbol with |")88 elif len(child_or) == 2: ## OR NOT89 if child_or[0] != "!" or not child_or[1].isalpha():90 error_exit("Bad Syntax, 2 combined conditions")91 else: ## XOR92 children_xor = child_or.split("^")93 for child_xor in children_xor:94 if not child_xor:95 error_exit("Bad Syntax, ^ missing symbol")96 if len(child_xor) == 1: ## XOR97 if not child_xor.isalpha():98 error_exit("Bad Syntax, non-alphabet symbol with ^")99 elif len(child_xor) == 2: ## XOR NOT100 if child_xor[0] != "!" or not child_xor[1].isalpha():101 error_exit("Bad Syntax, many combined conditions")102 else:103 error_exit("Bad Syntax, too many combined conditions")104def check_syntax(g):105 for rule in g.rules:106 check_parenthesis(rule.parents)107 check_parents(rule.parents)108 check_children(rule.children)109def check_loop(g):110 for rule in g.rules:111 rules_tree = []112 for child in rule.children:113 for letter in child:114 if letter.isalpha():115 deduced = letter116 for rule_implied in g.rules:117 for parent in rule_implied.parents:118 for letter in parent:119 if letter == deduced:120 rules_tree.append(rule_implied)121 for rule_implied in rules_tree:122 if len(rules_tree) > 420000: ## complexity limit123 error_exit("Bad Logic, infinite loop")124 for child in rule_implied.children:125 for letter in child:126 if letter.isalpha():127 deduced = letter128 for rule_orig in g.rules:129 for parent in rule_orig.parents:130 for letter in parent:131 if letter == deduced:132 if rule_orig.parents == rule.parents and rule_orig.children == rule.children:133 error_exit("circular Logic, infinite loop")134 rules_tree.append(rule_orig)135 break136def check_contradiction(g):137 for fact in g.facts:138 rules_tree = []139 positive = []140 negative = []141 for child_rule in fact.child_rules:142 rules_tree.append(child_rule)143 for child_rule in rules_tree:144 negative_found = False145 for letter in child_rule.children:146 if letter == "!":147 negative_found = True148 if letter.isalpha():149 if negative_found == False:150 positive.append(letter)151 else:152 negative.append(letter)153 negative_found = False 154 for rule_implied in g.rules:155 for parent in rule_implied.parents:156 for letter_implied in parent:157 if letter_implied == letter:158 rules_tree.append(rule_implied)159 for pos in positive:160 for neg in negative:161 if pos == neg:162 error_exit("Bad Logic, contradiction")163def check_error(g):164 check_syntax(g)165 check_loop(g)...
parse.py
Source:parse.py
...32 logic = args.logic33 return filepath, graph, color, timer, logic34def parse_initial_facts(line, g):35 if g.initial_facts:36 error_exit("Multiple lines of initial facts")37 initial_facts = line38 i = 039 for letter in initial_facts:40 fact_found = False41 if i > 0:42 if not letter.isalpha():43 error_exit("Non-alphabet character in initial facts")44 for fact in g.facts:45 if letter == fact.symbol:46 fact_found = True47 break48 if fact_found:49 g.add_initial_fact(letter)50 else:51 g.add_fact(letter)52 g.add_initial_fact(letter)53 i += 154 if i > 26:55 error_exit("Initial facts too long")56def parse_queries(line, g):57 if g.queries:58 error_exit("Multiple lines of queries")59 queries = line60 i = 061 for letter in queries:62 if i > 0:63 g.add_queries(letter)64 i += 165 if i > 26:66 error_exit("Queries too long")67def parse_rule(line, g):68 i = 069 for letter in line:70 i += 171 if i > 100000:72 error_exit("Rule too long")73 if letter.isalpha() == True:74 found = False75 for fact in g.facts:76 if letter == fact.symbol:77 found = True78 if not found:79 g.add_fact(letter)80 g.add_rule(line)81### parse_file converts Rules, Initial facts, & Queries into a graph of fact nodes linked with parent & child rules82def parse_file(filepath):83 if not os.path.isfile(filepath):84 error_exit("Invalid filepath")85 if not os.access(filepath, os.R_OK):86 error_exit("file has no read permissions")87 allowedSymbols = {'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', '(', ')', '+', '!', '|', '^', '=', '>', '?'}88 g = graph()89 with open(filepath, 'r') as file:90 i = 091 initial_facts_found = False92 for line in file:93 i += 194 if i > 10000:95 error_exit("File too long")96 line = line.replace(" ", "").replace("\t", "").replace("\n", "").split("#")[0]97 if line != "":98 if not allowedSymbols.issuperset(line):99 error_exit("Invalid symbol in file")100 if line[0] == '=': ## Initial facts101 parse_initial_facts(line, g)102 initial_facts_found = True103 elif line[0] == '?': ## Queries104 if not initial_facts_found:105 error_exit("Queries given before initial facts")106 parse_queries(line, g)107 else: ## Rule108 if initial_facts_found:109 error_exit("Rule given after initial facts")110 if g.queries:111 error_exit("Rule given after queries")112 parse_rule(line, g)113 if initial_facts_found == False:114 error_exit("No initial facts")115 if not g.queries:116 error_exit("No queries")117 g.link_facts_rules()...
recipe-580767.py
Source:recipe-580767.py
...24 def __init__(self, tee_filename):25 try:26 self.tee_fil = open(tee_filename, "w")27 except IOError as ioe:28 error_exit("Caught IOError: {}".format(repr(ioe)))29 except Exception as e:30 error_exit("Caught Exception: {}".format(repr(e)))31 def write(self, s):32 sys.stdout.write(s)33 self.tee_fil.write(s)34 def writeln(self, s):35 self.write(s + '\n')36 def close(self):37 try:38 self.tee_fil.close()39 except IOError as ioe:40 error_exit("Caught IOError: {}".format(repr(ioe)))41 except Exception as e:42 error_exit("Caught Exception: {}".format(repr(e)))43def main():44 if len(sys.argv) != 2:45 error_exit("Usage: python {} teefile".format(sys.argv[0]))46 tee = Tee(sys.argv[1])47 tee.write("This is a test of the Tee Python class.\n")48 tee.writeln("It is inspired by the Unix tee command,")49 tee.write("which can send output to both a file and stdout.\n")50 i = 151 s = "apple"52 tee.writeln("This line has interpolated values like {} and '{}'.".format(i, s))53 tee.close()54if __name__ == '__main__':55 main()56'''57The error_exit function is as follows - put it into a separate file called 58error_exit.py in the same folder as the tee.py program, on put it on your PYTHONPATH:59# error_exit.py60# Author: Vasudev Ram61# Web site: https://vasudevram.github.io62# Blog: https://jugad2.blogspot.com63# Product store: https://gumroad.com/vasudevram64# Purpose: This module, error_exit.py, defines a function with 65# the same name, error_exit(), which takes a string message 66# as an argument. It prints the message to sys.stderr, or 67# to another file object open for writing (if given as the 68# second argument), and then exits the program.69# The function error_exit can be used when a fatal error condition occurs, 70# and you therefore want to print an error message and exit your program.71import sys72def error_exit(message, dest=sys.stderr):73 dest.write(message)74 sys.exit(1)75def main():76 error_exit("Testing error_exit.\n")77if __name__ == "__main__":78 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!!