Best Python code snippet using lisa_python
string.py
Source:string.py  
...31            if offset > 0 and data[offset - 1] != 0x00:32                # Ensure non-string data precedes our test data33                data[offset - 1] = 0x0334        cls.data = bytes(data)35    def _validate_entry(self, result, exp_entry):36        exp_off = exp_entry[0]37        exp_addr = self.addr + exp_off38        exp_len = len(exp_entry[1])39        self.assertTrue(isinstance(result, dict))40        self.assertEqual(result['src_off'], exp_off)41        self.assertEqual(result['src_addr'], exp_addr)42        self.assertEqual(result['src_size'], exp_len)43    def test_find(self):44        hunter = StringHunter(self.data, self.addr)45        with self.subTest('Any string @ 0'):46            result = hunter.find(None, min_len=5, max_len=35)47            self._validate_entry(result, self.expected[0])48        with self.subTest('Any string @ 297'):49            result = hunter.find(None, min_len=3, max_len=49, start=297)50            self._validate_entry(result, self.expected[3])51        with self.subTest('Any string @ 332 w/ no bounds'):52            result = hunter.find(None, start=332)53            self._validate_entry(result, self.expected[4])54            result = hunter.find(None, start=332, min_len=-1)55            self._validate_entry(result, self.expected[4])56            result = hunter.find(None, start=332, max_len=-1)57            self._validate_entry(result, self.expected[4])58            result = hunter.find(None, start=332, min_len=-1, max_len=-1)59            self._validate_entry(result, self.expected[4])60        with self.subTest('Any string @ 332 w/ bounds: (min=48, max=100)'):61            result = hunter.find(None, min_len=48, max_len=100, start=332)62            self._validate_entry(result, self.expected[4])63        with self.subTest('Any string @ 332 w/ bounds (min=48, max=48)'):64            result = hunter.find(None, min_len=48, max_len=48, start=332)65            self._validate_entry(result, self.expected[4])66        with self.subTest('Overconstrained - no results'):67            with self.assertRaises(HunterResultNotFound):68                result = hunter.find(None, min_len=49, start=332)69                self._validate_entry(result, self.expected[4])70        with self.subTest('Custom pattern, str'):71            result = hunter.find('[a-zA-Z ]+world!?')72            self._validate_entry(result, self.expected[1])73        with self.subTest('Custom pattern, bytes'):74            result = hunter.find(b'([a-zA-Z ]+world!?|0123456789)')75            self._validate_entry(result, self.expected[1])76            result = hunter.find(b'([a-zA-Z ]+world!?|0123456789)', start=300)77            self._validate_entry(result, self.expected[-1])78        with self.subTest('Start offset=100'):79            hunter = StringHunter(self.data, self.addr, start_offset=100)80            result = hunter.find(None)81            self._validate_entry(result, self.expected[1])82        with self.subTest('Start offset=100, End offset=145'):83            hunter = StringHunter(self.data, self.addr, start_offset=100, end_offset=145)84            result = hunter.find(None)85            self._validate_entry(result, self.expected[1])86            hunter = StringHunter(self.data, self.addr, start_offset=100, end_offset=145)87            result = hunter.find(None, min_len=29, max_len=29)88            self._validate_entry(result, self.expected[1])89        with self.subTest('Start offset=100, End offset=144'):90            with self.assertRaises(HunterResultNotFound):91                hunter = StringHunter(self.data, self.addr, start_offset=100, end_offset=144)92                result = hunter.find(None)93                self._validate_entry(result, self.expected[1])94    def test_string_at(self):95        hunter = StringHunter(self.data, self.addr)96        result = hunter.string_at(self.addr + 501)97        expected = self.expected[-1][1][:-1]   # [Last item], [second field], [:trim null byte]98        self.assertEqual(expected.decode('ascii'), result)99        result = hunter.string_at(self.addr + 501, min_len=10, max_len=11)100        expected = self.expected[-1][1][:-1]   # [Last item], [second field], [:trim null byte]101        self.assertEqual(expected.decode('ascii'), result)102        with self.assertRaises(HunterResultNotFound):103            result = hunter.string_at(self.addr + 502, min_len=11)104    def test_find_iter_no_gaps(self):105        match = [False] * len(self.expected)106        hunter = StringHunter(self.data, self.addr)107        for result in hunter.finditer(None):...Schema.py
Source:Schema.py  
...20            pass21        else:22            schema = {k:None for k in schema}23        return schema24    def _validate_entry(self, obj, k, v, prop_getter, throw=False):25        match = True26        missing = False27        mistyped = False28        try:29            t = prop_getter(k)30        except (KeyError, AttributeError):31            missing = True32            match = False33            t = None34        except (TypeError,):35            mistyped = True36            match = False37            t = None38        else:39            if v is None:40                pass41            elif isinstance(v, (type, tuple)):42                match = isinstance(t, v)43            elif isinstance(v, Schema):44                match = v.validate(t, throw=throw)45            else:46                match = v(t)47        if not match and throw:48            if missing:49                raise KeyError("object {} doesn't match schema {}; key {} is missing".format(50                    obj, self, k51                ))52            elif mistyped:53                raise KeyError("object {} doesn't match schema {}; doesn't support attributes".format(54                    obj, self55                ))56            else:57                raise KeyError("object {} doesn't match schema {}; value {} doesn't match schema type {}".format(58                    obj, self, t, v59                ))60        return (t, match)61    def validate(self, obj, throw=True):62        """63        Validates that `obj` matches the provided schema64        and throws an error if not65        :param obj:66        :type obj:67        :param throw:68        :type throw:69        :return:70        :rtype:71        """72        prop_getter = ( lambda k,obj=obj:getattr(obj, k) ) if not hasattr(obj, '__getitem__') else obj.__getitem__73        for k,v in self.schema.items():74            if not self._validate_entry(obj, k, v, prop_getter, throw=throw)[1]:75                return False76        else:77            return True78    def to_dict(self, obj, throw=True):79        """80        Converts `obj` into a plain `dict` representation81        :param obj:82        :type obj:83        :return:84        :rtype:85        """86        res = {}87        prop_getter = ( lambda k,obj=obj:getattr(obj, k) ) if not hasattr(obj, '__getitem__') else obj.__getitem__88        for k, v in self.schema.items():89            t, m = self._validate_entry(obj, k, v, prop_getter, throw=throw)90            if not m:91                return None92            else:93                res[k] = t94        if self.sub_schema is not None:95            for k, v in self.sub_schema.items():96                t, m = self._validate_entry(obj, k, v, prop_getter, throw=False)97                if m:98                    res[k] = t99        return res100    def __repr__(self):...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!!
