Best Python code snippet using autotest_python
encoder.py
Source:encoder.py
1"""Implementation of JSONEncoder2"""3import re4from decimal import Decimal5def _import_speedups():6 try:7 from simplejson import _speedups8 return _speedups.encode_basestring_ascii, _speedups.make_encoder9 except ImportError:10 return None, None11c_encode_basestring_ascii, c_make_encoder = _import_speedups()12from simplejson.decoder import PosInf13ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]')14ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])')15HAS_UTF8 = re.compile(r'[\x80-\xff]')16ESCAPE_DCT = {17 '\\': '\\\\',18 '"': '\\"',19 '\b': '\\b',20 '\f': '\\f',21 '\n': '\\n',22 '\r': '\\r',23 '\t': '\\t',24}25for i in range(0x20):...
decoder.py
Source:decoder.py
...6import struct7from .tokens import *8class PBJSONDecodeError(ValueError):9 pass10def _import_speedups():11 try:12 # noinspection PyUnresolvedReferences13 from . import _speedups14 return _speedups.decode15 except (ImportError, AttributeError):16 return None17def _decode_int(content):18 accumulator = 019 length = len(content)20 offset = 021 while length >= 4:22 accumulator |= struct.unpack_from('!L', content, offset)[0]23 length -= 424 offset += 425 if length:26 if length > 4:27 accumulator <<= 3228 else:29 accumulator <<= (length * 8)30 if length == 3:31 b, h = struct.unpack_from('!BH', content, offset)32 accumulator |= ((b << 16) | h)33 elif length == 2:34 accumulator |= struct.unpack_from('!H', content, offset)[0]35 elif length == 1:36 accumulator |= struct.unpack_from('B', content, offset)[0]37 return accumulator38def _decode_float(float_class, content):39 if content:40 encoded = []41 for b in content:42 encoded.append(float_decode[b >> 4])43 encoded.append(float_decode[b & 0xf])44 if encoded and encoded[-1] == '.':45 encoded = encoded[:-1]46 encoded = ''.join(encoded)47 else:48 encoded = '0'49 return float_class(encoded)50def _decode_list(context, data, length=-1):51 result = []52 while length:53 if length < 0 and data[0] == TERMINATOR:54 return result, data[1:]55 item, data = _decode_one(context, data)56 result.append(item)57 length -= 158 return result, data59def _decode_dict(context, document_class, keys, data, length=-1):60 result = document_class()61 while length:62 if length < 0 and data[0] == TERMINATOR:63 return result, data[1:]64 key_token, data = data[0], data[1:]65 if key_token < 0x80:66 key_name, data = data[:key_token].decode(), data[key_token:]67 if len(keys) < 128:68 keys.append(key_name)69 else:70 key_name = keys[key_token & 0x7f]71 item, data = _decode_one(context, data)72 result[key_name] = item73 length -= 174 return result, data75def _decode_custom(context, data, custom):76 result, data = _decode_one(context, data)77 return custom(result), data78def _decode_one(context, data):79 """Return the Python representation of ``s`` (a ``bytes`` instance containing a Packed Binary JSON document)"""80 if data:81 first_byte, data = data[0], data[1:]82 token = first_byte & 0xe083 if not token:84 return context[first_byte](context, data)85 length = first_byte & 0xf86 if first_byte & 0x10:87 if length == 0xf:88 length, data = struct.unpack_from('!L', data, 0)[0], data[4:]89 elif length >= 8:90 length, data = ((length & 7) << 16) | struct.unpack_from('!H', data, 0)[0], data[2:]91 else:92 length, data = ((first_byte & 7) << 8) | data[0], data[1:]93 if token in {LIST, DICT}:94 return context[token](context, data, length)95 return context[token](context, data[:length]), data[length:]96def py_decoder(data, document_class=None, float_class=None, custom=None, unicode_errors='strict'):97 if isinstance(data, memoryview):98 data = data.tobytes()99 float_class = float_class or float100 document_class = document_class or dict101 keys = []102 context = {103 FALSE: lambda context, data: (False, data),104 TRUE: lambda context, data: (True, data),105 NULL: lambda context, data: (None, data),106 INF: lambda context, data: (float_class('inf'), data),107 NEGINF: lambda context, data: (float_class('-inf'), data),108 NAN: lambda context, data: (float_class('nan'), data),109 TERMINATED_LIST: _decode_list,110 INT: lambda context, data: _decode_int(data),111 NEGINT: lambda context, data: -_decode_int(data),112 FLOAT: lambda context, data: _decode_float(float_class, data),113 STRING: lambda context, data: data.decode(errors=unicode_errors),114 BINARY: lambda context, data: data,115 LIST: _decode_list,116 DICT: lambda context, data, length: _decode_dict(context, document_class, keys, data, length),117 CUSTOM: lambda context, data: _decode_custom(context, data, custom),118 }119 return _decode_one(context, data)[0]120# Use speedup if available121c_decoder = _import_speedups()...
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!!