text
stringlengths
0
828
return ResponseBotClient(config=config, client=api)"
1768,"def json2py(json_obj):
""""""
Converts the inputted JSON object to a python value.
:param json_obj | <variant>
""""""
for key, value in json_obj.items():
if type(value) not in (str, unicode):
continue
# restore a datetime
if re.match('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}:\d+$', value):
value = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S:%f')
elif re.match('^\d{4}-\d{2}-\d{2}$', value):
year, month, day = map(int, value.split('-'))
value = datetime.date(year, month, day)
elif re.match('^\d{2}:\d{2}:\d{2}:\d+$', value):
hour, minute, second, micro = map(int, value.split(':'))
value = datetime.time(hour, minute, second, micro)
else:
found = False
for decoder in _decoders:
success, new_value = decoder(value)
if success:
value = new_value
found = True
break
if not found:
continue
json_obj[key] = value
return json_obj"
1769,"def jsonify(py_data, default=None, indent=4, sort_keys=True):
""""""
Converts the inputted Python data to JSON format.
:param py_data | <variant>
""""""
return json.dumps(py_data, default=py2json, indent=indent, sort_keys=sort_keys)"
1770,"def py2json(py_obj):
""""""
Converts the inputted python object to JSON format.
:param py_obj | <variant>
""""""
method = getattr(py_obj, '__json__', None)
if method:
return method()
elif type(py_obj) == datetime.datetime:
return py_obj.isoformat()
elif type(py_obj) == datetime.date:
return py_obj.isoformat()
elif type(py_obj) == datetime.time:
return py_obj.isoformat()
elif type(py_obj) == set:
return list(py_obj)
elif type(py_obj) == decimal.Decimal:
return str(py_obj)
else:
# look through custom plugins
for encoder in _encoders:
success, value = encoder(py_obj)
if success:
return value
opts = (py_obj, type(py_obj))
raise TypeError('Unserializable object {} of type {}'.format(*opts))"
1771,"def register(encoder=None, decoder=None):
""""""
Registers an encoder method and/or a decoder method for processing
custom values. Encoder and decoders should take a single argument
for the value to encode or decode, and return a tuple of (<bool>
success, <variant> value). A successful decode or encode should
return True and the value.
:param encoder | <callable> || None
decoder | <callable> || None
""""""
if encoder:
_encoders.append(encoder)
if decoder:
_decoders.append(decoder)"
1772,"def xmlresponse(py_data):
""""""
Generates an XML formatted method response for the given python
data.
:param py_data | <variant>
""""""
xroot = ElementTree.Element('methodResponse')
xparams = ElementTree.SubElement(xroot, 'params')
xparam = ElementTree.SubElement(xparams, 'param')
type_map = {'bool': 'boolean',
'float': 'double',
'str': 'string',
'unicode': 'string',