Best Python code snippet using autotest_python
json.py
Source:json.py
...59 out = self.out60 if self.isFirst: self.isFirst = False61 else: out.write(',')62 if self.isObject[0]:63 out.write(encode_basestring(name))64 out.write(':')65 out.write(encode_basestring(value))66 else: out.write(encode_basestring(value))67 def objectStart(self, name, attributes=None):68 '''69 @see: IRender.objectStart70 '''71 self.openObject(name, attributes)72 self.isObject.appendleft(True)73 def objectEnd(self):74 '''75 @see: IRender.objectEnd76 '''77 assert self.isObject, 'No object to end'78 isObject = self.isObject.popleft()79 assert isObject, 'No object to end'80 self.out.write('}')81 def collectionStart(self, name, attributes=None):82 '''83 @see: IRender.collectionStart84 '''85 assert isinstance(name, str), 'Invalid name %s' % name86 out = self.out87 self.openObject(name, attributes)88 if not self.isFirst: out.write(',')89 out.write(encode_basestring(name))90 out.write(':[')91 self.isFirst = True92 self.isObject.appendleft(False)93 def collectionEnd(self):94 '''95 @see: IRender.collectionEnd96 '''97 assert self.isObject, 'No collection to end'98 isObject = self.isObject.popleft()99 assert not isObject, 'No collection to end'100 self.out.write(']}')101 # ----------------------------------------------------------------102 def openObject(self, name, attributes=None):103 '''104 Used to open a JSON object.105 '''106 assert isinstance(name, str), 'Invalid name %s' % name107 assert attributes is None or isinstance(attributes, dict), 'Invalid attributes %s' % attributes108 out = self.out109 if not self.isFirst: out.write(',')110 if self.isObject and self.isObject[0]:111 out.write(encode_basestring(name))112 out.write(':')113 out.write('{')114 self.isFirst = True115 if attributes:116 for attrName, attrValue in attributes.items():117 assert isinstance(attrName, str), 'Invalid attribute name %s' % attrName118 assert isinstance(attrValue, str), 'Invalid attribute value %s' % attrValue119 if self.isFirst: self.isFirst = False120 else: out.write(',')121 out.write(encode_basestring(attrName))122 out.write(':')...
js.py
Source:js.py
...14}15for i in range(0x20):16 ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i))1718def encode_basestring(s):19 """Return a JSON representation of a Python string2021 """22 def replace(match):23 return ESCAPE_DCT[match.group(0)]24 return '"' + ESCAPE.sub(replace, s) + '"'25 26def encode_unicode(s):27 """Return a JSON representation of a Python unicode2829 """30 return '"' + s.encode('unicode_escape') + '"'3132def simple_value(v):33 from uliweb.i18n.lazystr import LazyString34 35 if callable(v):36 v = v()37 if isinstance(v, LazyString) or isinstance(v, decimal.Decimal) or isinstance(v, datetime.datetime):38 return str(v)39 else:40 return v4142class JSONEncoder(object):43 def __init__(self, encoding='utf-8', unicode=False, default=None):44 self.encoding = encoding45 self.unicode = unicode46 self.default = default47 48 def iterencode(self, obj, key=False):49 if self.default:50 x = self.default(obj)51 obj = x52 if isinstance(obj, str):53 if self.unicode:54 yield encode_unicode(unicode(obj, self.encoding))55 else:56 yield encode_basestring(obj)57 elif isinstance(obj, unicode):58 if self.unicode:59 yield encode_unicode(obj)60 else:61 yield encode_basestring(obj.encode(self.encoding))62 elif obj is None:63 yield 'null'64 elif obj is True:65 yield 'true'66 elif obj is False:67 yield 'false'68 elif isinstance(obj, (int, long)):69 if key:70 yield '"' + str(obj) + '"'71 else:72 yield str(obj)73 elif isinstance(obj, float):74 if key:75 yield '"' + str(obj) + '"'76 else:77 yield str(obj)78 elif isinstance(obj, (list, tuple)):79 yield '['80 first = True81 for x in obj:82 if not first:83 yield ','84 for y in self.iterencode(x):85 yield y86 first = False87 yield ']'88 elif isinstance(obj, dict):89 yield '{'90 first = True91 for k, v in obj.iteritems():92 if not first:93 yield ','94 for x in self.iterencode(k, key=True):95 yield x96 yield ':'97 for y in self.iterencode(v):98 yield y99 first = False100 yield '}'101 elif isinstance(obj, decimal.Decimal):102 yield str(obj)103 elif isinstance(obj, datetime.datetime):104 yield '"' + obj.strftime('%Y-%m-%d %H:%M:%S') + '"'105 elif isinstance(obj, datetime.date):106 yield '"' + obj.strftime('%Y-%m-%d') + '"'107 elif isinstance(obj, datetime.time):108 yield '"' + obj.strftime('%H:%M:%S') + '"'109 else:110 yield encode_basestring(str(obj))111 112 def encode(self, obj):113 return ''.join(self.iterencode(obj))114 115def json_dumps(obj, unicode=False, **kwargs):116 return JSONEncoder(unicode=unicode, default=simple_value, **kwargs).encode(obj)
...
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!!