How to use _iterencode method in autotest

Best Python code snippet using autotest_python

encoder.py

Source:encoder.py Github

copy

Full Screen

...256 markers, self.default, _encoder, self.indent,257 self.key_separator, self.item_separator, self.sort_keys,258 self.skipkeys, self.allow_nan)259 else:260 _iterencode = _make_iterencode(261 markers, self.default, _encoder, self.indent, floatstr,262 self.key_separator, self.item_separator, self.sort_keys,263 self.skipkeys, _one_shot)264 return _iterencode(o, 0)265266def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,267 _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,268 ## HACK: hand-optimized bytecode; turn globals into locals269 ValueError=ValueError,270 basestring=basestring,271 dict=dict,272 float=float,273 id=id,274 int=int,275 isinstance=isinstance,276 list=list,277 long=long,278 str=str,279 tuple=tuple,280 ):281282 def _iterencode_list(lst, _current_indent_level):283 if not lst:284 yield '[]'285 return286 if markers is not None:287 markerid = id(lst)288 if markerid in markers:289 raise ValueError("Circular reference detected")290 markers[markerid] = lst291 buf = '['292 if _indent is not None:293 _current_indent_level += 1294 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))295 separator = _item_separator + newline_indent296 buf += newline_indent297 else:298 newline_indent = None299 separator = _item_separator300 first = True301 for value in lst:302 if first:303 first = False304 else:305 buf = separator306 if isinstance(value, basestring):307 yield buf + _encoder(value)308 elif value is None:309 yield buf + 'null'310 elif value is True:311 yield buf + 'true'312 elif value is False:313 yield buf + 'false'314 elif isinstance(value, (int, long)):315 yield buf + str(value)316 elif isinstance(value, float):317 yield buf + _floatstr(value)318 else:319 yield buf320 if isinstance(value, (list, tuple)):321 chunks = _iterencode_list(value, _current_indent_level)322 elif isinstance(value, dict):323 chunks = _iterencode_dict(value, _current_indent_level)324 else:325 chunks = _iterencode(value, _current_indent_level)326 for chunk in chunks:327 yield chunk328 if newline_indent is not None:329 _current_indent_level -= 1330 yield '\n' + (' ' * (_indent * _current_indent_level))331 yield ']'332 if markers is not None:333 del markers[markerid]334335 def _iterencode_dict(dct, _current_indent_level):336 if not dct:337 yield '{}'338 return339 if markers is not None:340 markerid = id(dct)341 if markerid in markers:342 raise ValueError("Circular reference detected")343 markers[markerid] = dct344 yield '{'345 if _indent is not None:346 _current_indent_level += 1347 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))348 item_separator = _item_separator + newline_indent349 yield newline_indent350 else:351 newline_indent = None352 item_separator = _item_separator353 first = True354 if _sort_keys:355 items = sorted(dct.items(), key=lambda kv: kv[0])356 else:357 items = dct.iteritems()358 for key, value in items:359 if isinstance(key, basestring):360 pass361 # JavaScript is weakly typed for these, so it makes sense to362 # also allow them. Many encoders seem to do something like this.363 elif isinstance(key, float):364 key = _floatstr(key)365 elif key is True:366 key = 'true'367 elif key is False:368 key = 'false'369 elif key is None:370 key = 'null'371 elif isinstance(key, (int, long)):372 key = str(key)373 elif _skipkeys:374 continue375 else:376 raise TypeError("key " + repr(key) + " is not a string")377 if first:378 first = False379 else:380 yield item_separator381 yield _encoder(key)382 yield _key_separator383 if isinstance(value, basestring):384 yield _encoder(value)385 elif value is None:386 yield 'null'387 elif value is True:388 yield 'true'389 elif value is False:390 yield 'false'391 elif isinstance(value, (int, long)):392 yield str(value)393 elif isinstance(value, float):394 yield _floatstr(value)395 else:396 if isinstance(value, (list, tuple)):397 chunks = _iterencode_list(value, _current_indent_level)398 elif isinstance(value, dict):399 chunks = _iterencode_dict(value, _current_indent_level)400 else:401 chunks = _iterencode(value, _current_indent_level)402 for chunk in chunks:403 yield chunk404 if newline_indent is not None:405 _current_indent_level -= 1406 yield '\n' + (' ' * (_indent * _current_indent_level))407 yield '}'408 if markers is not None:409 del markers[markerid]410411 def _iterencode(o, _current_indent_level):412 if isinstance(o, basestring):413 yield _encoder(o)414 elif o is None:415 yield 'null'416 elif o is True:417 yield 'true'418 elif o is False:419 yield 'false'420 elif isinstance(o, (int, long)):421 yield str(o)422 elif isinstance(o, float):423 yield _floatstr(o)424 elif isinstance(o, (list, tuple)):425 for chunk in _iterencode_list(o, _current_indent_level):426 yield chunk427 elif isinstance(o, dict):428 for chunk in _iterencode_dict(o, _current_indent_level):429 yield chunk430 else:431 if markers is not None:432 markerid = id(o)433 if markerid in markers:434 raise ValueError("Circular reference detected")435 markers[markerid] = o436 o = _default(o)437 for chunk in _iterencode(o, _current_indent_level):438 yield chunk439 if markers is not None:440 del markers[markerid]441 ...

Full Screen

Full Screen

jsonutil.py

Source:jsonutil.py Github

copy

Full Screen

...39 raise ValueError(40 "Out of range float values are not JSON compliant: " +41 repr(o))42 return text43 return _make_iterencode(44 encode_str=super().encode,45 encode_int=int.__repr__,46 encode_float=floatstr,47 encode_dataclasses=self._encode_dataclasses,48 encode_other_raw=self.default_raw,49 encode_other=self.default,50 ultimate_fallback=self._ultimate_fallback,51 )(obj)52# noinspection PyShadowingBuiltins53def _make_iterencode(54 encode_str,55 encode_int,56 encode_float,57 encode_dataclasses,58 encode_other_raw,59 encode_other,60 ultimate_fallback,61 # HACK: hand-optimized bytecode; turn globals into locals62 iter=iter,63 next=next,64 isinstance=isinstance,65 str=str,66 list=list,67 int=int,68 float=float,69 dec=Decimal,70):71 def _iterencode_dict(o):72 it = iter(o.items())73 if not o:74 yield '{}'75 return76 yield '{'77 key, value = next(it)78 yield encode_str(str(key))79 yield ':'80 yield from _iterencode(value)81 for key, value in it:82 yield ','83 yield encode_str(str(key))84 yield ':'85 yield from _iterencode(value)86 yield '}'87 def _iterencode_list(o):88 if not o:89 yield '[]'90 return91 yield '['92 it = iter(o)93 item = next(it)94 yield from _iterencode(item)95 for item in it:96 yield ','97 yield from _iterencode(item)98 yield ']'99 def _iterencode(o):100 if isinstance(o, str):101 yield encode_str(o)102 elif o is None:103 yield 'null'104 elif o is True:105 yield 'true'106 elif o is False:107 yield 'false'108 elif isinstance(o, list):109 yield from _iterencode_list(o)110 elif isinstance(o, dec):111 yield str(o)112 elif isinstance(o, int):113 yield encode_int(o)114 elif isinstance(o, float):115 yield encode_float(o)116 elif isinstance(o, (datetime.date, datetime.datetime)):117 yield encode_str(str(o))118 else:119 try:120 yield from _iterencode_dict(o)121 except (TypeError, AttributeError):122 if encode_dataclasses and dataclasses.is_dataclass(o):123 yield from _iterencode_dict(o.__dict__)124 else:125 try:126 raw = encode_other_raw(o)127 if raw is not None:128 yield raw129 else:130 yield from _iterencode(encode_other(o))131 except TypeError as exc:132 if ultimate_fallback is not None:133 yield from _iterencode(ultimate_fallback(o))134 else:135 raise exc from None136 return _iterencode137def dumpjson(obj, **kwargs) -> str:138 return json.dumps(obj, cls=Encoder, **kwargs)139def loadjson(s):140 return json.loads(s)141def loadjson_all(s):142 dec = Decoder()143 data = s.strip()144 while data:145 obj, ix = dec.raw_decode(data)146 yield obj147 data = data[ix:].lstrip()

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