Best Python code snippet using autotest_python
encoder.py
Source:encoder.py
...161 return encode_basestring(o)162 # This doesn't pass the iterator directly to ''.join() because the163 # exceptions aren't as detailed. The list call should be roughly164 # equivalent to the PySequence_Fast that ''.join() would do.165 chunks = self.iterencode(o, _one_shot=True)166 if not isinstance(chunks, (list, tuple)):167 chunks = list(chunks)168 return ''.join(chunks)169 def iterencode(self, o, _one_shot=False):170 """Encode the given object and yield each string171 representation as available.172 For example::173 for chunk in JSONEncoder().iterencode(bigobject):174 mysocket.write(chunk)175 """176 if self.check_circular:177 markers = {}178 else:179 markers = None180 if self.ensure_ascii:181 _encoder = encode_basestring_ascii182 else:183 _encoder = encode_basestring184 def floatstr(o, allow_nan=self.allow_nan,185 _repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY):186 # Check for specials. Note that this type of test is processor187 # and/or platform-specific, so do tests which don't depend on the188 # internals.189 if o != o:190 text = 'NaN'191 elif o == _inf:192 text = 'Infinity'193 elif o == _neginf:194 text = '-Infinity'195 else:196 return _repr(o)197 if not allow_nan:198 raise ValueError(199 "Out of range float values are not JSON compliant: " +200 repr(o))201 return text202 if (_one_shot and c_make_encoder is not None203 and self.indent is None):204 _iterencode = c_make_encoder(205 markers, self.default, _encoder, self.indent,206 self.key_separator, self.item_separator, self.sort_keys,207 self.skipkeys, self.allow_nan)208 else:209 _iterencode = _make_iterencode(210 markers, self.default, _encoder, self.indent, floatstr,211 self.key_separator, self.item_separator, self.sort_keys,212 self.skipkeys, _one_shot)213 return _iterencode(o, 0)214def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,215 _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,216 ## HACK: hand-optimized bytecode; turn globals into locals217 ValueError=ValueError,218 dict=dict,219 float=float,220 id=id,221 int=int,222 isinstance=isinstance,223 list=list,224 str=str,225 tuple=tuple,226 _intstr=int.__repr__,227 ):228 if _indent is not None and not isinstance(_indent, str):229 _indent = ' ' * _indent230 def _iterencode_list(lst, _current_indent_level):231 if not lst:232 yield '[]'233 return234 if markers is not None:235 markerid = id(lst)236 if markerid in markers:237 raise ValueError("Circular reference detected")238 markers[markerid] = lst239 buf = '['240 if _indent is not None:241 _current_indent_level += 1242 newline_indent = '\n' + _indent * _current_indent_level243 separator = _item_separator + newline_indent244 buf += newline_indent245 else:246 newline_indent = None247 separator = _item_separator248 first = True249 for value in lst:250 if first:251 first = False252 else:253 buf = separator254 if isinstance(value, str):255 yield buf + _encoder(value)256 elif value is None:257 yield buf + 'null'258 elif value is True:259 yield buf + 'true'260 elif value is False:261 yield buf + 'false'262 elif isinstance(value, int):263 # Subclasses of int/float may override __repr__, but we still264 # want to encode them as integers/floats in JSON. One example265 # within the standard library is IntEnum.266 yield buf + _intstr(value)267 elif isinstance(value, float):268 # see comment above for int269 yield buf + _floatstr(value)270 else:271 yield buf272 if isinstance(value, (list, tuple)):273 chunks = _iterencode_list(value, _current_indent_level)274 elif isinstance(value, dict):275 chunks = _iterencode_dict(value, _current_indent_level)276 else:277 chunks = _iterencode(value, _current_indent_level)278 yield from chunks279 if newline_indent is not None:280 _current_indent_level -= 1281 yield '\n' + _indent * _current_indent_level282 yield ']'283 if markers is not None:284 del markers[markerid]285 def _iterencode_dict(dct, _current_indent_level):286 if not dct:287 yield '{}'288 return289 if markers is not None:290 markerid = id(dct)291 if markerid in markers:292 raise ValueError("Circular reference detected")293 markers[markerid] = dct294 yield '{'295 if _indent is not None:296 _current_indent_level += 1297 newline_indent = '\n' + _indent * _current_indent_level298 item_separator = _item_separator + newline_indent299 yield newline_indent300 else:301 newline_indent = None302 item_separator = _item_separator303 first = True304 if _sort_keys:305 items = sorted(dct.items())306 else:307 items = dct.items()308 for key, value in items:309 if isinstance(key, str):310 pass311 # JavaScript is weakly typed for these, so it makes sense to312 # also allow them. Many encoders seem to do something like this.313 elif isinstance(key, float):314 # see comment for int/float in _make_iterencode315 key = _floatstr(key)316 elif key is True:317 key = 'true'318 elif key is False:319 key = 'false'320 elif key is None:321 key = 'null'322 elif isinstance(key, int):323 # see comment for int/float in _make_iterencode324 key = _intstr(key)325 elif _skipkeys:326 continue327 else:328 raise TypeError(f'keys must be str, int, float, bool or None, '329 f'not {key.__class__.__name__}')330 if first:331 first = False332 else:333 yield item_separator334 yield _encoder(key)335 yield _key_separator336 if isinstance(value, str):337 yield _encoder(value)338 elif value is None:339 yield 'null'340 elif value is True:341 yield 'true'342 elif value is False:343 yield 'false'344 elif isinstance(value, int):345 # see comment for int/float in _make_iterencode346 yield _intstr(value)347 elif isinstance(value, float):348 # see comment for int/float in _make_iterencode349 yield _floatstr(value)350 else:351 if isinstance(value, (list, tuple)):352 chunks = _iterencode_list(value, _current_indent_level)353 elif isinstance(value, dict):354 chunks = _iterencode_dict(value, _current_indent_level)355 else:356 chunks = _iterencode(value, _current_indent_level)357 yield from chunks358 if newline_indent is not None:359 _current_indent_level -= 1360 yield '\n' + _indent * _current_indent_level361 yield '}'362 if markers is not None:363 del markers[markerid]364 def _iterencode(o, _current_indent_level):365 if isinstance(o, str):366 yield _encoder(o)367 elif o is None:368 yield 'null'369 elif o is True:370 yield 'true'371 elif o is False:372 yield 'false'373 elif isinstance(o, int):374 # see comment for int/float in _make_iterencode375 yield _intstr(o)376 elif isinstance(o, float):377 # see comment for int/float in _make_iterencode378 yield _floatstr(o)379 elif isinstance(o, (list, tuple)):380 yield from _iterencode_list(o, _current_indent_level)381 elif isinstance(o, dict):382 yield from _iterencode_dict(o, _current_indent_level)383 else:384 if markers is not None:385 markerid = id(o)386 if markerid in markers:387 raise ValueError("Circular reference detected")388 markers[markerid] = o389 o = _default(o)390 yield from _iterencode(o, _current_indent_level)391 if markers is not None:392 del markers[markerid]...
_json.py
Source:_json.py
...6 if ROUND is not None:7 o = round(o, ROUND)8 9 return '%g' % o10def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,11 _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,12 ## HACK: hand-optimized bytecode; turn globals into locals13 ValueError=ValueError,14 dict=dict,15 float=float,16 id=id,17 int=int,18 isinstance=isinstance,19 list=list,20 str=str,21 tuple=tuple,22 ):23 '''24 Overwrite json.encoder for Python 2.7 and above to not25 assign each index of a list or tuple to its own row as26 this is completely asinine behaviour 27 '''28 ## @THREE29 # Override the function30 _floatstr = _json_floatstr31 if _indent is not None and not isinstance(_indent, str):32 _indent = ' ' * _indent33 def _iterencode_list(lst, _current_indent_level):34 if not lst:35 yield '[]'36 return37 if markers is not None:38 markerid = id(lst)39 if markerid in markers:40 raise ValueError("Circular reference detected")41 markers[markerid] = lst42 buf = '['43 ## @THREEJS44 # - block the moronic functionality that puts each45 # index on its own line causing insane row counts46 #if _indent is not None:47 # _current_indent_level += 148 # newline_indent = '\n' + _indent * _current_indent_level49 # separator = _item_separator + newline_indent50 # buf += newline_indent51 #else:52 newline_indent = None53 separator = _item_separator54 first = True55 for value in lst:56 if first:57 first = False58 else:59 buf = separator60 if isinstance(value, str):61 yield buf + _encoder(value)62 elif value is None:63 yield buf + 'null'64 elif value is True:65 yield buf + 'true'66 elif value is False:67 yield buf + 'false'68 elif isinstance(value, int):69 yield buf + str(value)70 elif isinstance(value, float):71 yield buf + _floatstr(value)72 else:73 yield buf74 if isinstance(value, (list, tuple)):75 chunks = _iterencode_list(value, _current_indent_level)76 elif isinstance(value, dict):77 chunks = _iterencode_dict(value, _current_indent_level)78 else:79 chunks = _iterencode(value, _current_indent_level)80 for chunk in chunks:81 yield chunk82 if newline_indent is not None:83 _current_indent_level -= 184 yield '\n' + _indent * _current_indent_level85 yield ']'86 if markers is not None:87 del markers[markerid]88 def _iterencode_dict(dct, _current_indent_level):89 if not dct:90 yield '{}'91 return92 if markers is not None:93 markerid = id(dct)94 if markerid in markers:95 raise ValueError("Circular reference detected")96 markers[markerid] = dct97 yield '{'98 if _indent is not None:99 _current_indent_level += 1100 newline_indent = '\n' + _indent * _current_indent_level101 item_separator = _item_separator + newline_indent102 yield newline_indent103 else:104 newline_indent = None105 item_separator = _item_separator106 first = True107 if _sort_keys:108 items = sorted(dct.items(), key=lambda kv: kv[0])109 else:110 items = dct.items()111 for key, value in items:112 if isinstance(key, str):113 pass114 # JavaScript is weakly typed for these, so it makes sense to115 # also allow them. Many encoders seem to do something like this.116 elif isinstance(key, float):117 key = _floatstr(key)118 elif key is True:119 key = 'true'120 elif key is False:121 key = 'false'122 elif key is None:123 key = 'null'124 elif isinstance(key, int):125 key = str(key)126 elif _skipkeys:127 continue128 else:129 raise TypeError("key " + repr(key) + " is not a string")130 if first:131 first = False132 else:133 yield item_separator134 yield _encoder(key)135 yield _key_separator136 if isinstance(value, str):137 yield _encoder(value)138 elif value is None:139 yield 'null'140 elif value is True:141 yield 'true'142 elif value is False:143 yield 'false'144 elif isinstance(value, int):145 yield str(value)146 elif isinstance(value, float):147 yield _floatstr(value)148 else:149 if isinstance(value, (list, tuple)):150 chunks = _iterencode_list(value, _current_indent_level)151 elif isinstance(value, dict):152 chunks = _iterencode_dict(value, _current_indent_level)153 else:154 chunks = _iterencode(value, _current_indent_level)155 for chunk in chunks:156 yield chunk157 if newline_indent is not None:158 _current_indent_level -= 1159 yield '\n' + _indent * _current_indent_level160 yield '}'161 if markers is not None:162 del markers[markerid]163 def _iterencode(o, _current_indent_level):164 if isinstance(o, str):165 yield _encoder(o)166 elif o is None:167 yield 'null'168 elif o is True:169 yield 'true'170 elif o is False:171 yield 'false'172 elif isinstance(o, int):173 yield str(o)174 elif isinstance(o, float):175 yield _floatstr(o)176 elif isinstance(o, (list, tuple)):177 for chunk in _iterencode_list(o, _current_indent_level):178 yield chunk179 elif isinstance(o, dict):180 for chunk in _iterencode_dict(o, _current_indent_level):181 yield chunk182 else:183 if markers is not None:184 markerid = id(o)185 if markerid in markers:186 raise ValueError("Circular reference detected")187 markers[markerid] = o188 o = _default(o)189 for chunk in _iterencode(o, _current_indent_level):190 yield chunk191 if markers is not None:192 del markers[markerid]193 return _iterencode194# override the encoder...
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!!