Best Python code snippet using slash
vm_translator.py
Source:vm_translator.py  
...240        '''241        self._write_text(hack)242        self.write_call('Sys.init', 0)243    def write_label(self, label):244        self._validate_label(label)245        hack = f'''246        // ------------------------------------------------------------------------------247        // label {label}248        // ------------------------------------------------------------------------------249        (Label{label})250        '''251        self._write_text(hack)252    def write_goto(self, label):253        self._validate_label(label)254        hack = f'''255        // ------------------------------------------------------------------------------256        // goto {label}257        // ------------------------------------------------------------------------------258        @Label{label}259        0;JMP260        '''261        self._write_text(hack)262    def write_if(self, label):263        self._validate_label(label)264        hack = f'''265        // ------------------------------------------------------------------------------266        // if-goto {label}267        // ------------------------------------------------------------------------------268        # decrement stack pointer269        @SP270        M=M-1271        # pop stack to D272        A=M273        D=M274        @Label{label}275        D;JNE276        '''277        self._write_text(hack)278    def write_function(self, label, k):279        self._validate_label(label)280        hack = f'''281        // ------------------------------------------------------------------------------282        // function {label} {k}283        // ------------------------------------------------------------------------------284        ({self._function_label(label)})285        '''286        self._write_text(hack)287        for _i in range(k):288            self._write_push_constant(0)289    @classmethod290    def _function_label(cls, label):291        return f'{cls.FUNCTION_LABEL_PREFIX}.{label}'292    def write_return(self):293        hack = f'''294        // ------------------------------------------------------------------------------295        // return296        // ------------------------------------------------------------------------------297        # FRAME = LCL298        @LCL299        D=M300        @R13301        M=D302        # RET = *(FRAME-5)303        @5304        A=D-A305        D=M306        @R14307        M=D308        # *ARG = pop()309        @SP310        M=M-1311        A=M312        D=M313        @ARG314        A=M315        M=D316        # SP = ARG+1317        @ARG318        D=M+1319        @SP320        M=D321        # THAT = *(FRAME - 1)322        @R13323        M=M-1324        A=M325        D=M326        @THAT327        M=D328        # THIS = *(FRAME - 2)329        @R13330        M=M-1331        A=M332        D=M333        @THIS334        M=D335        # ARG = *(FRAME - 3)336        @R13337        M=M-1338        A=M339        D=M340        @ARG341        M=D342        # LCL = *(FRAME - 4)343        @R13344        M=M-1345        A=M346        D=M347        @LCL348        M=D349        # goto RET350        @R14351        A=M352        0;JMP353        '''354        self._write_text(hack)355    def write_call(self, label, n):356        return_label = self._generate_return_label()357        function_label = self._function_label(label)358        hack = f'''359        // ------------------------------------------------------------------------------360        // call {label} {n}361        // ------------------------------------------------------------------------------362        # push return-address363        @{return_label}364        D=A365        @SP366        A=M367        M=D368        @SP369        M=M+1370        # push LCL371        @LCL372        D=M373        @SP374        A=M375        M=D376        @SP377        M=M+1378        # push ARG379        @ARG380        D=M381        @SP382        A=M383        M=D384        @SP385        M=M+1386        # push THIS387        @THIS388        D=M389        @SP390        A=M391        M=D392        @SP393        M=M+1394        # push THAT395        @THAT396        D=M397        @SP398        A=M399        M=D400        @SP401        M=M+1402        # ARG = SP-n-5403        @{n}404        D=A405        @5406        D=D+A407        @SP408        D=M-D409        @ARG410        M=D411        # LCL = SP412        @SP413        D=M414        @LCL415        M=D416        # goto f417        @{function_label}418        0;JMP419        # (return-address)420        ({return_label})421        '''422        self._write_text(hack)423    def _validate_label(self, label):424        if re.search(r'[^A-Za-z0-9:._]', label):425            raise ValueError(f'Invalid label: {label}')426    def write_arithmetic(self, command):427        self._write_text(f'''428            // ------------------------------------------------------------------------------429            // {command}430            // ------------------------------------------------------------------------------431        ''')432        if command in self.bin_ops:433            op = self.bin_ops[command]434            hack = f'''435            # decrement stack pointer436            @SP437            M=M-1...test_maps.py
Source:test_maps.py  
...144    ]145    for marker_name in marker_names:146        result = maps._validate_marker_name(marker_name)147        assert result == marker_name148def test_validate_label():149    maps = Maps()150    # invalid value151    with raises(InvalidLabelError) as exception:152        label = "00"153        result = maps._validate_label(label)154    # invalid value155    with raises(InvalidLabelError) as exception:156        label = "invalid_"157        result = maps._validate_label(label)158    # valid value159    label = "a"160    result = maps._validate_label(label)161    assert result == label162    # valid value163    label = "0"164    result = maps._validate_label(label)165    assert result == label166    # valid value167    label = "99"168    result = maps._validate_label(label)169    assert result == label170    # valid value171    label = "valid"172    result = maps._validate_label(label)173    assert result == label174    # valid value175    label = "valid "176    result = maps._validate_label(label)177    assert result == label178def test_validate_color():179    maps = Maps()180    # invalid value181    with raises(InvalidColorError) as exception:182        color = "invalid"183        result = maps._validate_color(color)184    # valid value185    color = "00f"186    result = maps._validate_color(color)187    assert result == color188    # valid value189    color = "0000ff"190    result = maps._validate_color(color)...base.py
Source:base.py  
...25        """returns a list of names26        for compatibility with multi-index27        """28        return [self.name]29    def _validate_label(self, label):30        """Perform label validation"""31        if label is None:32            return label33        if isinstance(label, pd.Timestamp):34            label = label.to_pydatetime()35        label, error = self.field.validate(label, {}, loc=self.name)36        if error:37            raise ValidationError([error], self.schema)38        if isinstance(label, BaseModel):39            label = label.dict()40        return label41    def validate_label(self, label):42        if isinstance(label, dict) and self.name in label:43            label = label[self.name]44        if isinstance(label, slice):45            start = self._validate_label(label.start)46            stop = self._validate_label(label.stop)47            step = self._validate_label(label.step)48            if start is None and stop is None:49                label = None50            elif step is not None:51                return list(range(start, stop, step))52            else:53                return slice(start, stop, step)54        if isinstance(label, list) and self.field.type_ is not list:55            return [self._validate_label(val) for val in label]56        if isinstance(label, tuple) and self.field.type_ is not tuple:57            return tuple(self._validate_label(val) for val in label)58        return self._validate_label(label)59    def reduce(self, docs, labels):60        return docs61    def label_options(self, query):62        return query.unique(self.name)63    def __repr__(self):64        type_ = self.field.type_ if self.field else "UNKNOWN"...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!!
