本文整理汇总了Python中pyutil.jsonutil.dumps函数的典型用法代码示例。如果您正苦于以下问题:Python dumps函数的具体用法?Python dumps怎么用?Python dumps使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dumps函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_separators
def test_separators(self):
h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
{'nifty': 87}, {'field': 'yes', 'morefield': False} ]
expect = textwrap.dedent("""\
[
[
"blorpie"
] ,
[
"whoops"
] ,
[] ,
"d-shtaeou" ,
"d-nthiouh" ,
"i-vhbjkhnth" ,
{
"nifty" : 87
} ,
{
"field" : "yes" ,
"morefield" : false
}
]""")
d1 = json.dumps(h)
d2 = json.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : '))
h1 = json.loads(d1)
h2 = json.loads(d2)
self.assertEquals(h1, h)
self.assertEquals(h2, h)
self.assertEquals(d2, expect)
示例2: test_dictrecursion
def test_dictrecursion(self):
x = {}
x["test"] = x
try:
json.dumps(x)
except ValueError:
pass
else:
self.fail("didn't raise ValueError on dict recursion")
x = {}
{"a": x, "b": x}
# ensure that the marker is cleared
json.dumps(x)
示例3: test_dont_Featureify_results
def test_dont_Featureify_results(self):
""" _request() is required to return the exact string that the HTTP
server sent to it -- no transforming it, such as by json-decoding and
then constructing a Feature. """
EXAMPLE_RECORD_JSONSTR=json.dumps({ 'geometry' : { 'type' : 'Point', 'coordinates' : [D('10.0'), D('11.0')] }, 'id' : 'my_id', 'type' : 'Feature', 'properties' : { 'key' : 'value' , 'type' : 'object' } })
mockhttp = mock.Mock()
mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', }, EXAMPLE_RECORD_JSONSTR)
self.client.places.http = mockhttp
res = self.client.places._request("http://thing", 'POST')[1]
self.failUnlessEqual(res, EXAMPLE_RECORD_JSONSTR)
示例4: test_annotate
def test_annotate(self):
mockhttp = mock.Mock()
headers = {'status': '200', 'content-type': 'application/json'}
mockhttp.request.return_value = (headers, json.dumps(EXAMPLE_ANNOTATE_RESPONSE))
self.client.http = mockhttp
res = self.client.annotate(self.handle, EXAMPLE_ANNOTATIONS, True)
self.assertEqual(mockhttp.method_calls[0][0], 'request')
self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/features/%s/annotations.json' % (API_VERSION, self.handle))
self.assertEqual(mockhttp.method_calls[0][1][1], 'POST')
# Make sure client returns a dict.
self.failUnless(isinstance(res, dict))
示例5: test_dont_Recordify_results
def test_dont_Recordify_results(self):
""" _request() is required to return the exact string that the HTTP
server sent to it -- no transforming it, such as by json-decoding and
then constructing a Record. """
EXAMPLE_RECORD_JSONSTR=json.dumps({ 'geometry' : { 'type' : 'Point', 'coordinates' : [D('10.0'), D('11.0')] }, 'id' : 'my_id', 'type' : 'Feature', 'properties' : { 'key' : 'value' , 'type' : 'object' } })
mockagent = MockAgent(FakeSuccessResponse([EXAMPLE_RECORD_JSONSTR], {'status': '200', 'content-type': 'application/json'}))
self.client.agent = mockagent
d = self.client._request("http://thing", 'POST')
d.addCallback(get_body)
def check(res):
self.failUnlessEqual(res, EXAMPLE_RECORD_JSONSTR)
d.addCallback(check)
return d
示例6: annotate
def annotate(self, simplegeohandle, annotations, private):
if not isinstance(annotations, dict):
raise TypeError('annotations must be of type dict')
if not len(annotations.keys()):
raise ValueError('annotations dict is empty')
for annotation_type in annotations.keys():
if not len(annotations[annotation_type].keys()):
raise ValueError('annotation type "%s" is empty' % annotation_type)
if not isinstance(private, bool):
raise TypeError('private must be of type bool')
data = {'annotations': annotations,
'private': private}
endpoint = self._endpoint('annotations', simplegeohandle=simplegeohandle)
return json_decode(self._request(endpoint,
'POST',
data=json.dumps(data))[1])
示例7: test_listrecursion
def test_listrecursion(self):
x = []
x.append(x)
try:
json.dumps(x)
except ValueError:
pass
else:
self.fail("didn't raise ValueError on list recursion")
x = []
y = [x]
x.append(y)
try:
json.dumps(x)
except ValueError:
pass
else:
self.fail("didn't raise ValueError on alternating list recursion")
y = []
x = [y, y]
# ensure that the marker is cleared
json.dumps(x)
示例8: test_search
def test_search(self):
rec1 = Feature((D('11.03'), D('10.04')), simplegeohandle='SG_abcdefghijkmlnopqrstuv', properties={'name': "Bob's House Of Monkeys", 'category': "monkey dealership"})
rec2 = Feature((D('11.03'), D('10.05')), simplegeohandle='SG_abcdefghijkmlnopqrstuv', properties={'name': "Monkey Food 'R' Us", 'category': "pet food store"})
mockhttp = mock.Mock()
mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', }, json.dumps({'type': "FeatureColllection", 'features': [rec1.to_dict(), rec2.to_dict()]}))
self.client.places.http = mockhttp
self.failUnlessRaises(AssertionError, self.client.places.search, -91, 100)
self.failUnlessRaises(AssertionError, self.client.places.search, -81, 361)
lat = D('11.03')
lon = D('10.04')
res = self.client.places.search(lat, lon, query='monkeys', category='animal')
self.failUnless(isinstance(res, (list, tuple)), (repr(res), type(res)))
self.failUnlessEqual(len(res), 2)
self.failUnless(all(isinstance(f, Feature) for f in res))
self.assertEqual(mockhttp.method_calls[0][0], 'request')
self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/places/%s,%s.json?q=monkeys&category=animal' % (API_VERSION, lat, lon))
self.assertEqual(mockhttp.method_calls[0][1][1], 'GET')
示例9: test_search_by_my_ip_nonascii
def test_search_by_my_ip_nonascii(self):
rec1 = Feature((D('11.03'), D('10.04')), simplegeohandle='SG_abcdefghijkmlnopqrstuv', properties={'name': "Bob's House Of Monkeys", 'category': "monkey dealership"})
rec2 = Feature((D('11.03'), D('10.05')), simplegeohandle='SG_abcdefghijkmlnopqrstuv', properties={'name': "Monkey Food 'R' Us", 'category': "pet food store"})
mockhttp = mock.Mock()
mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', }, json.dumps({'type': "FeatureColllection", 'features': [rec1.to_dict(), rec2.to_dict()]}))
self.client.places.http = mockhttp
ipaddr = '192.0.32.10'
self.failUnlessRaises(AssertionError, self.client.places.search_by_my_ip, ipaddr) # Someone accidentally passed an ip addr to search_by_my_ip().
res = self.client.places.search_by_my_ip(query='monk❥y', category='animal')
self.failUnless(isinstance(res, (list, tuple)), (repr(res), type(res)))
self.failUnlessEqual(len(res), 2)
self.failUnless(all(isinstance(f, Feature) for f in res))
self.assertEqual(mockhttp.method_calls[-1][0], 'request')
urlused = mockhttp.method_calls[-1][1][0]
urlused = urllib.unquote(urlused).decode('utf-8')
self.assertEqual(urlused, u'http://api.simplegeo.com:80/%s/places/ip.json?q=monk❥y&category=animal' % (API_VERSION,))
self.assertEqual(mockhttp.method_calls[0][1][1], 'GET')
res = self.client.places.search_by_my_ip(query='monk❥y', category='anim❥l')
self.failUnless(isinstance(res, (list, tuple)), (repr(res), type(res)))
self.failUnlessEqual(len(res), 2)
self.failUnless(all(isinstance(f, Feature) for f in res))
self.assertEqual(mockhttp.method_calls[-1][0], 'request')
urlused = mockhttp.method_calls[-1][1][0]
urlused = urllib.unquote(urlused).decode('utf-8')
self.assertEqual(urlused, u'http://api.simplegeo.com:80/%s/places/ip.json?q=monk❥y&category=anim❥l' % (API_VERSION,))
self.assertEqual(mockhttp.method_calls[0][1][1], 'GET')
示例10: test_encoding6
def test_encoding6(self):
u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
j = json.dumps([u], ensure_ascii=False)
self.assertEquals(j, u'["%s"]' % (u,))
示例11: test_encoding4
def test_encoding4(self):
u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
j = json.dumps([u])
self.assertEquals(j, '["\\u03b1\\u03a9"]')
示例12: test_no_terms_search_by_ip
def test_no_terms_search_by_ip(self):
rec1 = Feature((D('11.03'), D('10.04')), simplegeohandle='SG_abcdefghijkmlnopqrstuv', properties={'name': "Bob's House Of Monkeys", 'category': "monkey dealership"})
rec2 = Feature((D('11.03'), D('10.05')), simplegeohandle='SG_abcdefghijkmlnopqrstuv', properties={'name': "Monkey Food 'R' Us", 'category': "pet food store"})
mockhttp = mock.Mock()
mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', }, json.dumps({'type': "FeatureColllection", 'features': [rec1.to_dict(), rec2.to_dict()]}))
self.client.places.http = mockhttp
ipaddr = '192.0.32.10'
res = self.client.places.search_by_ip(ipaddr)
self.failUnless(isinstance(res, (list, tuple)), (repr(res), type(res)))
self.failUnlessEqual(len(res), 2)
self.failUnless(all(isinstance(f, Feature) for f in res))
self.assertEqual(mockhttp.method_calls[0][0], 'request')
self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/places/%s.json' % (API_VERSION, ipaddr))
self.assertEqual(mockhttp.method_calls[0][1][1], 'GET')
示例13: test_dumps
def test_dumps(self):
self.assertEquals(json.dumps({}), '{}')
示例14: mockrequest
def mockrequest(*args, **kwargs):
self.assertEqual(args[0], 'http://api.simplegeo.com:80/%s/places' % (API_VERSION,))
self.assertEqual(args[1], 'POST')
bodyobj = json.loads(kwargs['body'])
self.failUnlessEqual(bodyobj['id'], None)
methods_called.append(('request', args, kwargs))
mockhttp.request = mockrequest2
return ({'status': '202', 'content-type': 'application/json', 'location': newloc}, json.dumps({'id': handle}))
示例15: test_radius_search_by_my_ip
def test_radius_search_by_my_ip(self):
mockhttp = mock.Mock()
mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', }, json.dumps({'type': "FeatureColllection", 'features': []}))
self.client.places.http = mockhttp
ipaddr = '192.0.32.10'
radius = D('0.01')
self.failUnlessRaises((AssertionError, TypeError), self.client.places.search_by_my_ip, ipaddr, radius=radius) # Someone accidentally passed an ip addr to search_by_my_ip().
res = self.client.places.search_by_my_ip(radius=radius)
self.failUnless(isinstance(res, (list, tuple)), (repr(res), type(res)))
self.failUnlessEqual(len(res), 0)
self.assertEqual(mockhttp.method_calls[0][0], 'request')
self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/places/ip.json?radius=%s' % (API_VERSION, radius))
self.assertEqual(mockhttp.method_calls[0][1][1], 'GET')