Best Python code snippet using autotest_python
tree_printer.py
Source:tree_printer.py  
...4    def decorator(func):5        setattr(cls,func.__name__,func)6        return func7    return decorator8def indent_text(string, i):9    out = ""10    for i in range(i):11        out += "|  "12    out += string13    return out14class TreePrinter:15    @add_to_class(matrix_ast.Error)16    def print_tree(self, indent=0):17        pass18    @add_to_class(matrix_ast.Program)19    def print_tree(self):20        print("PROGRAM")21        indent = 022        for element in self.children:23            element.print_tree(indent + 1)24    @add_to_class(matrix_ast.ConditionalStatement)25    def print_tree(self, indent):26        self.if_statement.print_tree(indent)27        if self.ELSE:28            print(indent_text("ELSE", indent))29            self.else_body.print_tree(indent + 1)30    @add_to_class(matrix_ast.IfStatement)31    def print_tree(self, indent):32        print(indent_text("IF", indent))33        print(indent_text("CONDITION", indent + 1))34        self.sentence.print_tree(indent + 2)35        print(indent_text("BODY", indent + 1))36        self.body.print_tree(indent + 2)37    @add_to_class(matrix_ast.WhileStatement)38    def print_tree(self, indent):39        print(indent_text("WHILE", indent))40        print(indent_text("CONDITION", indent + 1))41        self.sentence.print_tree(indent + 2)42        print(indent_text("BODY", indent + 1))43        self.body.print_tree(indent + 2)44    @add_to_class(matrix_ast.ForStatement)45    def print_tree(self, indent):46        print(indent_text("FOR", indent))47        print(indent_text("EXPRESSION", indent+1))48        print(indent_text(self.assign, indent+2))49        print(indent_text(self.id, indent+3))50        self.range.print_tree(indent + 3)51        print(indent_text("BODY", indent+1))52        self.body.print_tree(indent + 2)53    @add_to_class(matrix_ast.Range)54    def print_tree(self, indent):55        print(indent_text("RANGE", indent))56        self.left.print_tree(indent + 1)57        self.right.print_tree(indent + 1)58    @add_to_class(matrix_ast.Sentence)59    def print_tree(self, indent):60        print(indent_text(self.operator, indent))61        if self.left is not None:62            self.left.print_tree(indent + 1)63        self.right.print_tree(indent + 1)64    @add_to_class(matrix_ast.Zeros)65    def print_tree(self, indent):66        print(indent_text(self.function, indent))67        self.expression.print_tree(indent + 1)68    @add_to_class(matrix_ast.Ones)69    def print_tree(self, indent):70        print(indent_text(self.function, indent))71        self.expression.print_tree(indent + 1)72    @add_to_class(matrix_ast.Eye)73    def print_tree(self, indent):74        print(indent_text(self.function, indent))75        self.expression.print_tree(indent + 1)76    @add_to_class(matrix_ast.BinaryOperator)77    def print_tree(self, indent):78        print(indent_text(self.operator, indent))79        self.left.print_tree(indent + 1)80        self.right.print_tree(indent + 1)81    @add_to_class(matrix_ast.SingleOperator)82    def print_tree(self, indent):83        print(indent_text(self.operator, indent))84        self.expression.print_tree(indent + 1)85    @add_to_class(matrix_ast.Assignment)86    def print_tree(self, indent):87        print(indent_text(self.operator, indent))88        self.left.print_tree(indent + 1)89        self.right.print_tree(indent + 1)90    @add_to_class(matrix_ast.MatrixCall)91    def print_tree(self, indent):92        print(indent_text("MATRIX_CALL", indent))93        self.expression.print_tree(indent + 1)94        self.x.print_tree(indent + 1)95        self.y.print_tree(indent + 1)96    @add_to_class(matrix_ast.Return)97    def print_tree(self, indent):98        print(indent_text("RETURN", indent))99        self.expression.print_tree(indent + 1)100    @add_to_class(matrix_ast.Print)101    def print_tree(self, indent):102        print(indent_text("PRINT", indent))103        for element in self.sequence:104            element.print_tree(indent + 1)105    @add_to_class(matrix_ast.InstructionBlock)106    def print_tree(self, indent):107        for element in self.sequence:108            element.print_tree(indent)109    @add_to_class(matrix_ast.Matrix)110    def print_tree(self, indent):111        print(indent_text("MATRIX", indent))112        for element in self.rows:113            element.print_tree(indent + 1)114    @add_to_class(matrix_ast.Row)115    def print_tree(self, indent):116        print(indent_text("ROW", indent))117        for element in self.sequence:118            element.print_tree(indent + 1)119    @add_to_class(matrix_ast.Integer)120    def print_tree(self, indent):121        print(indent_text(str(self.value), indent))122    @add_to_class(matrix_ast.Float)123    def print_tree(self, indent):124        print(indent_text(str(self.value), indent))125    @add_to_class(matrix_ast.String)126    def print_tree(self, indent):127        print(indent_text(self.value, indent))128    @add_to_class(matrix_ast.Id)129    def print_tree(self, indent):130        print(indent_text(str(self.value), indent))131    @add_to_class(matrix_ast.Break)132    def print_tree(self, indent):133        print(indent_text("BREAK", indent))134    @add_to_class(matrix_ast.Continue)135    def print_tree(self, indent):...encode_command.py
Source:encode_command.py  
1from webdnn.backend.code_generator.allocator import Allocation2from webdnn.backend.code_generator.command_buffer import CommandBuffer3def encode_command(builder: CommandBuffer):4    generated_lines = []5    indent_level = 16    indent_text = "    "7    inputs = []8    outputs = []9    call_option = {}10    for code in builder.codes:11        if code[0] == "declare":12            # (declare, typename, varname, initial_val, const)13            _, varname, initial_val, _ = code[1:]14            if initial_val is None:15                generated_lines.append(f"{indent_text}var {varname};")16            else:17                generated_lines.append(f"{indent_text}var {varname} = {initial_val};")18        elif code[0] == "load":19            #  (load, typename, varname, buffer_key, const)20            typename, varname, buffer_key, _ = code[1:]21            value = builder.buffer_injector.value_map[buffer_key]22            if isinstance(value, Allocation):23                if value in inputs:24                    index = inputs.index(value)25                else:26                    inputs.append(value)27                    index = len(inputs) - 128                if typename is None:29                    generated_lines.append(f"{indent_text}{varname} = input_arrays[{index}];")30                else:31                    generated_lines.append(f"{indent_text}var {varname} = input_arrays[{index}];")32            else:33                call_option[buffer_key] = value34                if typename is None:35                    generated_lines.append(f"{indent_text}{varname} = option['{buffer_key}'];")36                else:37                    generated_lines.append(f"{indent_text}var {varname} = option['{buffer_key}'];")38        elif code[0] == "exec":39            #  (Exec, expression)40            expression, = code[1:]41            generated_lines.append(f"{indent_text}{expression}")42        elif code[0] == "enterFor":43            #  (EnterFor, counter, initial_val, max_val, step_value)44            counter, initial_val, max_val, step_value = code[1:]45            generated_lines.append(f"{indent_text}for ({counter} = {initial_val}; {counter} < {max_val}; {counter} += {step_value}) {{")46            indent_level += 147            indent_text = "    " * indent_level48        elif code[0] == "exitFor":49            #  (ExitFor,)50            indent_level -= 151            indent_text = "    " * indent_level52            generated_lines.append(f"{indent_text}}}")53        elif code[0] == "enterBlockScope":54            #  (EnterBlockScope,)55            generated_lines.append(f"{indent_text}(function(){{")56            indent_level += 157            indent_text = "    " * indent_level58        elif code[0] == "exitBlockScope":59            #  (ExitBlockScope,)60            indent_level -= 161            indent_text = "    " * indent_level62            generated_lines.append(f"{indent_text}}})();")63        elif code[0] == "comment":64            #  (comment, text)65            text, = code[1:]66            generated_lines.append(f"{indent_text}//{text}")67        else:68            raise NotImplementedError(f"Unknown OP code: {code}")69    generated_lines = "\n".join(generated_lines)70    source = f"""71%%FUNC_NAME%%: function(input_arrays, output_arrays, option) {{72{generated_lines}73}},""" \74        .replace("%%INITIAL_PARALLEL_POSITION%%", "0") \75        .replace("%%PARALLEL_SIZE%%", "1")...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!!
