Best Python code snippet using sure_python
interpreter.py
Source:interpreter.py  
...51        if(not(stmt.value == None)):52            value = self.evaluate(stmt.value)53        raise ReturnException(value)54    def visit_while_stmt(self, stmt):55        while(self.is_truthful(self.evaluate(stmt.condition))):56            self.execute(stmt.body)57        return None58    def visit_if_stmt(self, stmt):59        if(self.is_truthful(self.evaluate(stmt.condition))):60            self.execute(stmt.then_branch)61        elif(not(stmt.else_branch == None)):62            self.execute(stmt.else_branch)63        return None64    def visit_block_stmt(self, stmt):65        self.execute_block(stmt.statements, Environment(self.environment))66        return None67    def visit_expression_stmt(self, stmt):68        self.evaluate(stmt.expression)69        return None70    def visit_print_stmt(self, stmt):71        value = self.evaluate(stmt.expression)72        print(value)73        return None74    def visit_var_stmt(self, stmt):75        value = None76        if(not(stmt.expression) == None):77            value = self.evaluate(stmt.expression)78        self.environment.define(stmt.name, value)79        return None80    def visit_assign_expr(self, expr):81        value = self.evaluate(expr.value)82        self.environment.assign(expr.name.lexeme, value)83        return value84    def visit_yeet_stmt(self, stmt):85        value = self.evaluate(stmt.expression)86        print(value.upper())87        return None88    def visit_logical_expr(self, expr):89        left = self.evaluate(expr.left)90        if(expr.operator.token_type == TokenType.OR):91            if(self.is_truthful(left)): 92                return left93        else:94            if(not(self.is_truthful(left))):95                return left96        return self.evaluate(expr.right)97    def visit_variable_expr(self, expr):98        return self.environment.get(expr.name.lexeme)99    def visit_literal_expr(self, expr):100        return(expr.value)101    def visit_grouping_expr(self, expr):102        return(self.evaluate(expr.expression))103    def visit_unary_expr(self, expr):104        right = self.evaluate(expr.right)105        if(expr.operator.token_type == TokenType.MINUS ):106            return(-(right))107        if(expr.operator.token_type == TokenType.BANG):108            return(not (self.is_truthful(right)))109        return(None)110    def visit_binary_expr(self, expr):111        left = self.evaluate(expr.left)112        right = self.evaluate(expr.right)113        if(expr.operator.token_type == TokenType.MINUS):114            self.check_number_operand(expr.operator, right)115            return(left - right)116        if(expr.operator.token_type == TokenType.SLASH):117            self.check_number_operands(expr.operator, left, right)118            return(left / right)119        if(expr.operator.token_type == TokenType.STAR):120            self.check_number_operands(expr.operator, left, right)121            return(left * right)122        if(expr.operator.token_type == TokenType.PLUS):123            # we need the distinciton between string and number values here as we dont want to add nunbers and strings124            if((isinstance(left, numbers.Real)) and (isinstance(right, numbers.Real))):125                return(left + right)126            if(isinstance(left, str)) and (isinstance(right, str)):127                return(left + right)128            Error.throw_runtime_error(self, expr.operator, "operands must be either 2 number or 2 strings")129        if(expr.operator.token_type == TokenType.GREATER):130            self.check_number_operands(expr.operator, left, right)131            return(left > right)132        if(expr.operator.token_type == TokenType.GREATER_EQUAL):133            self.check_number_operands(expr.operator, left, right)134            return(left >= right)135        if(expr.operator.token_type == TokenType.LESS):136            self.check_number_operands(expr.operator, left, right)137            return(left < right)138        if(expr.operator.token_type == TokenType.LESS_EQUAL):139            self.check_number_operands(expr.operator, left, right)140            return(left <= right)                                    141        if(expr.operator.token_type == TokenType.BANG_EQUAL):142            return(not(self.is_equal(left, right)))143        if(expr.operator.token_type == TokenType.EQUAL_EQUAL):144            return(self.is_equal(left, right))145        return None146    def evaluate(self, expr):147        return(expr.accept(self))148    # determines if an expression is true for a boolean operation. For this instance everything is true except for `None` and `False`149    def is_truthful(self, value):150        if(value == None):151            return False152        if(isinstance(value, bool)):153            return value154        return True155    def is_equal(self, left, right):156        # if types are different they are false157        if(not(type(left) == type(right))):158            return False159        return(left == right)160    def check_number_operand(self, operator, operand):161        if(isinstance(operand, numbers.Real)):162            return True163        Error.throw_runtime_error(operator, "operand must be a number")...validate.py
Source:validate.py  
1import os2import re3stat_header = ["TP", "TN", "FP", "FN", "accuracy", "precision", "recall", "n", "f1"]4deceptive_stat = dict.fromkeys(stat_header, 0)5truthful_stat = dict.fromkeys(stat_header, 0)6positive_stat = dict.fromkeys(stat_header, 0)7negative_stat = dict.fromkeys(stat_header, 0)8ground_truth_dict = {}9with open("percepoutput.txt", "r") as output_reader:10    for line in output_reader:11        l = line.split(" ")12        is_deceptive = "deceptive" in l[2]13        is_truthful = "truthful" in l[2]14        is_pos = "positive" in l[2]15        is_neg = "negative" in l[2]16        if is_truthful and is_deceptive:17            raise ValueError("value is both truthful and deceptive: {}".format(l[2]))18        if is_pos and is_neg:19            raise ValueError("value is both positive and negative: {}".format(l[2]))20        # deceptive truthful21        if is_truthful and l[0] == "truthful":22            truthful_stat["TP"] += 123            deceptive_stat["TN"] += 124        elif is_truthful and l[0] == "deceptive":25            truthful_stat["FP"] += 126            deceptive_stat["FN"] += 127        elif is_deceptive and l[0] == "deceptive":28            truthful_stat["TN"] += 129            deceptive_stat["TP"] += 130        elif is_deceptive and l[0] == "truthful":31            truthful_stat["FN"] += 132            deceptive_stat["FP"] += 133        # positive negative34        if is_pos and l[1] == "positive":35            positive_stat["TP"] += 136            negative_stat["TN"] += 137        elif is_pos and l[1] == "negative":38            positive_stat["FP"] += 139            negative_stat["FN"] += 140        elif is_neg and l[1] == "negative":41            positive_stat["TN"] += 142            negative_stat["TP"] += 143        elif is_neg and l[1] == "positive":44            positive_stat["FN"] += 145            negative_stat["FP"] += 146def cal_stat(stat):47    # Ref: https://blog.exsilio.com/all/accuracy-precision-recall-f1-score-interpretation-of-performance-measures/48    stat["n"] = stat["TP"] + stat["TN"] + stat["FP"] + stat["FN"]49    # Accuracy = TP+TN/TP+FP+FN+TN50    stat["accuracy"] = (stat["TP"] + stat["TN"])/(stat["n"])51    # Precision = TP/TP+FP52    stat["precision"] = (stat["TP"])/(stat["TP"] + stat["FP"])53    # Recall = TP/TP+FN54    stat["recall"] = (stat["TP"])/(stat["TP"] + stat["FN"])55    # F1 Score = 2*(Recall * Precision) / (Recall + Precision)56    stat["f1"] = 2*(stat["recall"] * stat["precision"])/(stat["recall"] + stat["precision"])57def print_stat(_class, stat):58    return "{} {:.2f} {:.2f} {:.2f}".format(_class, stat["recall"], stat["precision"], stat["f1"])59cal_stat(truthful_stat)60cal_stat(deceptive_stat)61cal_stat(positive_stat)62cal_stat(negative_stat)63dc = print_stat("deceptive", deceptive_stat)64tf = print_stat("truthful", truthful_stat)65ng = print_stat("negative", negative_stat)66ps = print_stat("positive", positive_stat)67f1 = "Mean F1: {:.4f}".format((deceptive_stat["f1"] + truthful_stat["f1"] + negative_stat["f1"] + positive_stat["f1"])/4)68output = 50*"#" + "\n"69output += "Result: recall | precision | f1 \n"70output += dc + "\n"71output += tf + "\n"72output += ng + "\n"73output += ps + "\n"74output += f1 + "\n"75output += 50*"#" + "\n"76print(output)77with open("result.txt", "a") as result_writer:78    result_writer.write(output)...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!!
