How to use scan_once method in autotest

Best Python code snippet using autotest_python

decoder.py

Source:decoder.py Github

copy

Full Screen

...139 end = _w(s, end + 1).end()140 except IndexError:141 pass142 try:143 value, end = scan_once(s, end)144 except StopIteration:145 raise ValueError(errmsg('Expecting object', s, end))146 pairs_append((key, value))147 try:148 nextchar = s[end]149 if nextchar in _ws:150 end = _w(s, end + 1).end()151 nextchar = s[end]152 except IndexError:153 nextchar = ''154 end += 1155 if nextchar == '}':156 break157 elif nextchar != ',':158 raise ValueError(errmsg("Expecting ',' delimiter", s, end - 1))159 try:160 nextchar = s[end]161 if nextchar in _ws:162 end += 1163 nextchar = s[end]164 if nextchar in _ws:165 end = _w(s, end + 1).end()166 nextchar = s[end]167 except IndexError:168 nextchar = ''169 end += 1170 if nextchar != '"':171 raise ValueError(errmsg('Expecting property name enclosed in double quotes', s, end - 1))172 if object_pairs_hook is not None:173 result = object_pairs_hook(pairs)174 return (result, end)175 else:176 pairs = dict(pairs)177 if object_hook is not None:178 pairs = object_hook(pairs)179 return (pairs, end)180def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):181 s, end = s_and_end182 values = []183 nextchar = s[end:end + 1]184 if nextchar in _ws:185 end = _w(s, end + 1).end()186 nextchar = s[end:end + 1]187 if nextchar == ']':188 return (values, end + 1)189 _append = values.append190 while True:191 try:192 value, end = scan_once(s, end)193 except StopIteration:194 raise ValueError(errmsg('Expecting object', s, end))195 _append(value)196 nextchar = s[end:end + 1]197 if nextchar in _ws:198 end = _w(s, end + 1).end()199 nextchar = s[end:end + 1]200 end += 1201 if nextchar == ']':202 break203 elif nextchar != ',':204 raise ValueError(errmsg("Expecting ',' delimiter", s, end))205 try:206 if s[end] in _ws:207 end += 1208 if s[end] in _ws:209 end = _w(s, end + 1).end()210 except IndexError:211 pass212 return (values, end)213class JSONDecoder(object):214 def __init__(self, encoding=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None):215 self.encoding = encoding216 self.object_hook = object_hook217 self.object_pairs_hook = object_pairs_hook218 self.parse_float = parse_float or float219 self.parse_int = parse_int or int220 self.parse_constant = parse_constant or _CONSTANTS.__getitem__221 self.strict = strict222 self.parse_object = JSONObject223 self.parse_array = JSONArray224 self.parse_string = scanstring225 self.scan_once = scanner.make_scanner(self)226 def decode(self, s, _w=WHITESPACE.match):227 obj, end = self.raw_decode(s, idx=_w(s, 0).end())228 end = _w(s, end).end()229 if end != len(s):230 raise ValueError(errmsg('Extra data', s, end, len(s)))231 return obj232 def raw_decode(self, s, idx=0):233 try:234 obj, end = self.scan_once(s, idx)235 except StopIteration:236 raise ValueError('No JSON object could be decoded')...

Full Screen

Full Screen

test_scanner.py

Source:test_scanner.py Github

copy

Full Screen

...4# Purpose: Performs unit testing on the simplejson.scanner module.5# NOTES:6"""7 -- Removed test cases 0089, 0090, and 0091 as they are8 redundant since _scan_once() is called by the scan_once()9 function anyway.10"""11from unittest import TestCase12import simplejson.decoder as decoder13import simplejson.errors as errors14import simplejson.scanner as scanner15class ScannerContext(object):16 """Defines a context object utilized by several test17 classes to configure a scanner.18 """19 def __init__(self, parse_object=decoder.JSONObject,20 parse_array=decoder.JSONArray,21 parse_string=decoder.scanstring,22 match_number=scanner.NUMBER_RE.match, encoding=None,23 strict=True, parse_float=None, parse_int=None,24 parse_constant=None, object_hook=None,25 object_pairs_hook=None, memo={}):26 """Returns an instance of a ScannerContext object.27 Args:28 Returns:29 (ScannerContext): A ScannerContext object.30 """31 self.parse_object = parse_object32 self.parse_array = parse_array33 self.parse_string = parse_string34 self.match_number = match_number35 self.encoding = encoding36 self.strict = strict37 self.parse_float = parse_float38 self.parse_int = parse_int39 self.parse_constant = parse_constant40 self.object_hook = object_hook41 self.object_pairs_hook = object_pairs_hook42 self.memo = memo43class TestScanner(TestCase):44 """Implements unit testing on the simplejson.scanner module.45 The tests performed by this module make sane attempts to be46 as thorough as possible but are not exhaustive.47 """48 def test_make_scanner_correct(self):49 """50 Description: Ensures that the py_make_scanner() function51 correctly configures the scan_once() function with52 the correct parameters.53 Input: A context containing default values for each54 member.55 Output: An instance of the py_make_scanner:scan_once()56 function.57 Test Case: Corresponds to test case TEST-0087.58 """59 context = ScannerContext()60 scan_once = scanner.py_make_scanner(context)61 # TODO: Perform testing here62 def test_make_scanner_none(self):63 """64 Description: Ensures that the py_make_scanner() function65 errors out when given a None type context parameter.66 Input:67 This test case accepts no input.68 Output:69 (Error)70 Test Case: Corresponds to test case TEST-0088.71 """72 context = None73 self.assertRaises(74 TypeError, # should raise a TypeError75 scanner.py_make_scanner,76 context77 )78 def test_make_scanner_scan_once_correct(self):79 """80 Description: Tests that the py_make_scanner:scan_once()81 function properly scans a JSON token given correct82 parameters.83 Input:84 (str, int): ("["abc", "def", "ghi"]", 0)85 Output:86 (list): ["abc", "def", "ghi"]87 Test Case: Corresponds to test case TEST-0092.88 """89 test_input = "[\"abc\", \"def\", \"ghi\"]"90 test_output = ["abc", "def", "ghi"]91 context = ScannerContext()92 scan_once = scanner.py_make_scanner(context)93 output, _ = scan_once(test_input, 0)94 self.assertEqual(output, test_output)95 def test_make_scanner_scan_once_none(self):96 """97 Description: Tests that the py_make_scanner:scan_once()98 function errors out when given a None type as the input99 string.100 Input:101 (None, int): (None, 0)102 Output:103 (JSONDecodeError)104 Test Case: Corresponds to test case TEST-0093.105 """106 test_input = (None, 0)107 context = ScannerContext()108 scan_once = scanner.py_make_scanner(context)109 self.assertRaises(110 errors.JSONDecodeError,111 scan_once,112 test_input[0],113 test_input[1]114 )115 def test_make_scanner_scan_once_iindex(self):116 """117 Description: Tests that the py_make_scanner:scan_once()118 function errors out when given a valid JSON string as the119 input string and an invalid index.120 Input:121 (str, int): ("["abc", "def", "ghi"]", -1)122 Output:123 (JSONDecodeError)124 Test Case: Corresponds to test case TEST-0094.125 """126 test_input = ("[\"abc\", \"def\", \"ghi\"]", -1)127 context = ScannerContext()128 scan_once = scanner.py_make_scanner(context)129 self.assertRaises(130 errors.JSONDecodeError,131 scan_once,...

Full Screen

Full Screen

standard.py

Source:standard.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2#3# Copyright (c) 2021~2999 - Cologler <skyoflw@gmail.com>4# ----------5#6# ----------7import re8from json import JSONDecoder9class StringPlugin:10 def make_scanner(self, context: JSONDecoder):11 parse_string = context.parse_string12 strict = context.strict13 def match(s, idx, nextchar):14 return nextchar == '"'15 def parse(m, s, idx, scan_once):16 return parse_string(s, idx + 1, strict)17 return match, parse18class ObjectPlugin:19 def make_scanner(self, context: JSONDecoder):20 parse_object = context.parse_object21 strict = context.strict22 object_hook = context.object_hook23 object_pairs_hook = context.object_pairs_hook24 memo = context.memo25 def match(s, idx, nextchar):26 return nextchar == '{'27 def parse(m, s, idx, scan_once):28 return parse_object((s, idx + 1), strict,29 scan_once, object_hook, object_pairs_hook, memo)30 return match, parse31class ArrayPlugin:32 def make_scanner(self, context: JSONDecoder):33 parse_array = context.parse_array34 def match(s, idx, nextchar):35 return nextchar == '['36 def parse(m, s, idx, scan_once):37 return parse_array((s, idx + 1), scan_once)38 return match, parse39class NullPlugin:40 def make_scanner(self, context: JSONDecoder):41 def match(s, idx, nextchar):42 return nextchar == 'n' and s[idx:idx + 4] == 'null'43 def parse(m, s, idx, scan_once):44 return None, idx + 445 return match, parse46class TruePlugin:47 def make_scanner(self, context: JSONDecoder):48 def match(s, idx, nextchar):49 return nextchar == 't' and s[idx:idx + 4] == 'true'50 def parse(m, s, idx, scan_once):51 return True, idx + 452 return match, parse53class FalsePlugin:54 def make_scanner(self, context: JSONDecoder):55 def match(s, idx, nextchar):56 return nextchar == 'f' and s[idx:idx + 5] == 'false'57 def parse(m, s, idx, scan_once):58 return False, idx + 559 return match, parse60class NumberPlugin:61 # copy from cpython\Lib\json\scanner.py62 _NUMBER_RE = re.compile(63 r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?',64 (re.VERBOSE | re.MULTILINE | re.DOTALL))65 def make_scanner(self, context: JSONDecoder):66 match_number = self._NUMBER_RE.match67 parse_float = context.parse_float68 parse_int = context.parse_int69 def match(s, idx, nextchar):70 return match_number(s, idx)71 def parse(m, s, idx, scan_once):72 integer, frac, exp = m.groups()73 if frac or exp:74 res = parse_float(integer + (frac or '') + (exp or ''))75 else:76 res = parse_int(integer)77 return res, m.end()78 return match, parse79SPEC_PLUGINS = (80 StringPlugin(),81 ObjectPlugin(),82 ArrayPlugin(),83 NullPlugin(),84 TruePlugin(),85 FalsePlugin(),86 NumberPlugin(),87)88class NaNPlugin:89 def make_scanner(self, context: JSONDecoder):90 parse_constant = context.parse_constant91 def match(s, idx, nextchar):92 return nextchar == 'N' and s[idx:idx + 3] == 'NaN'93 def parse(m, s, idx, scan_once):94 return parse_constant('NaN'), idx + 395 return match, parse96class InfinityPlugin:97 def make_scanner(self, context: JSONDecoder):98 parse_constant = context.parse_constant99 def match(s, idx, nextchar):100 return nextchar == 'I' and s[idx:idx + 8] == 'Infinity'101 def parse(m, s, idx, scan_once):102 return parse_constant('Infinity'), idx + 8103 return match, parse104class NegInfinityPlugin:105 def make_scanner(self, context: JSONDecoder):106 parse_constant = context.parse_constant107 def match(s, idx, nextchar):108 return nextchar == '-' and s[idx:idx + 9] == '-Infinity'109 def parse(m, s, idx, scan_once):110 return parse_constant('-Infinity'), idx + 9111 return match, parse112STANDARD_PLUGINS = SPEC_PLUGINS + (113 NaNPlugin(),114 InfinityPlugin(),115 NegInfinityPlugin(),...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful