本文整理汇总了Python中simplejson.compat.StringIO.getvalue方法的典型用法代码示例。如果您正苦于以下问题:Python StringIO.getvalue方法的具体用法?Python StringIO.getvalue怎么用?Python StringIO.getvalue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类simplejson.compat.StringIO
的用法示例。
在下文中一共展示了StringIO.getvalue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_tuple_array_dump
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def test_tuple_array_dump(self):
t = (1, 2, 3)
expect = json.dumps(list(t))
# Default is True
sio = StringIO()
json.dump(t, sio)
self.assertEqual(expect, sio.getvalue())
sio = StringIO()
json.dump(t, sio, tuple_as_array=True)
self.assertEqual(expect, sio.getvalue())
self.assertRaises(TypeError, json.dump, t, StringIO(),
tuple_as_array=False)
# Ensure that the "default" does not get called
sio = StringIO()
json.dump(t, sio, default=repr)
self.assertEqual(expect, sio.getvalue())
sio = StringIO()
json.dump(t, sio, tuple_as_array=True, default=repr)
self.assertEqual(expect, sio.getvalue())
# Ensure that the "default" gets called
sio = StringIO()
json.dump(t, sio, tuple_as_array=False, default=repr)
self.assertEqual(
json.dumps(repr(t)),
sio.getvalue())
示例2: main
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def main(model_path):
tag_data = tag_bank()
user_data,data_in = loadData(tag_data)
io = StringIO()
response = example(tag_data,user_data, 8, model_path)
json.dump(response, io)
io.getvalue()
示例3: test_sort_keys
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def test_sort_keys(self):
# https://github.com/simplejson/simplejson/issues/106
for num_keys in range(2, 32):
p = dict((str(x), x) for x in range(num_keys))
sio = StringIO()
json.dump(p, sio, sort_keys=True)
self.assertEqual(sio.getvalue(), json.dumps(p, sort_keys=True))
self.assertEqual(json.loads(sio.getvalue()), p)
示例4: post
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def post(arg):
#url = "http://maps.googleapis.com/maps/api/geocode/json?address=googleplex&sensor=false"
req = urllib.request.Request(arg)
response = urllib.request.urlopen(req)
data,in_data = data_in.loadData(response)
a = suggestion.auto_suggest(data)
a.find_rules()
a.suggestion_for_user()
io = StringIO()
json.dump(a.response_data_user, io)
io.getvalue()
示例5: test_decimal_defaults
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def test_decimal_defaults(self):
d = Decimal('1.1')
# use_decimal=True is the default
self.assertRaises(TypeError, json.dumps, d, use_decimal=False)
self.assertEqual('1.1', json.dumps(d))
self.assertEqual('1.1', json.dumps(d, use_decimal=True))
self.assertRaises(TypeError, json.dump, d, StringIO(),
use_decimal=False)
sio = StringIO()
json.dump(d, sio)
self.assertEqual('1.1', sio.getvalue())
sio = StringIO()
json.dump(d, sio, use_decimal=True)
self.assertEqual('1.1', sio.getvalue())
示例6: test_datetime_defaults
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def test_datetime_defaults(self):
d = datetime(2014, 1, 4)
# use_datetime=True is the default
self.assertRaises(TypeError, json.dumps, d, use_datetime=False)
self.assertEqual('"2014-01-04 00:00:00"', json.dumps(d))
self.assertEqual('"2014-01-04 00:00:00"', json.dumps(d, use_datetime=True))
self.assertRaises(TypeError, json.dump, d, StringIO(),
use_datetime=False)
sio = StringIO()
json.dump(d, sio)
self.assertEqual('"2014-01-04 00:00:00"', sio.getvalue())
sio = StringIO()
json.dump(d, sio, use_datetime=True)
self.assertEqual('"2014-01-04 00:00:00"', sio.getvalue())
示例7: check
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def check(indent, expected):
d1 = json.dumps(h, indent=indent)
self.assertEqual(d1, expected)
sio = StringIO()
json.dump(h, sio, indent=indent)
self.assertEqual(sio.getvalue(), expected)
示例8: test_asdict_not_callable_dump
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def test_asdict_not_callable_dump(self):
for f in CONSTRUCTORS:
self.assertRaises(TypeError,
json.dump, f(DeadDuck()), StringIO(), namedtuple_as_object=True)
sio = StringIO()
json.dump(f(DeadDict()), sio, namedtuple_as_object=True)
self.assertEqual(
json.dumps(f({})),
sio.getvalue())
示例9: test_namedtuple_dump_false
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def test_namedtuple_dump_false(self):
for v in [Value(1), Point(1, 2)]:
l = list(v)
sio = StringIO()
json.dump(v, sio, namedtuple_as_object=False)
self.assertEqual(
l,
json.loads(sio.getvalue()))
self.assertRaises(TypeError, json.dump, v, StringIO(),
tuple_as_array=False, namedtuple_as_object=False)
示例10: test_namedtuple_dump
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def test_namedtuple_dump(self):
for v in [Value(1), Point(1, 2), DuckValue(1), DuckPoint(1, 2)]:
d = v._asdict()
sio = StringIO()
json.dump(v, sio)
self.assertEqual(d, json.loads(sio.getvalue()))
sio = StringIO()
json.dump(v, sio, namedtuple_as_object=True)
self.assertEqual(
d,
json.loads(sio.getvalue()))
sio = StringIO()
json.dump(v, sio, tuple_as_array=False)
self.assertEqual(d, json.loads(sio.getvalue()))
sio = StringIO()
json.dump(v, sio, namedtuple_as_object=True,
tuple_as_array=False)
self.assertEqual(
d,
json.loads(sio.getvalue()))
示例11: dumps
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def dumps(self, obj, **kw):
sio = StringIO()
json.dump(obj, sio, **kw)
res = json.dumps(obj, **kw)
self.assertEquals(res, sio.getvalue())
return res
示例12: test_dump
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def test_dump(self):
sio = StringIO()
json.dump({}, sio)
self.assertEquals(sio.getvalue(), "{}")
示例13: write_popular_to_database
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def write_popular_to_database(table, list_popular_music_ids):
print "writing popular to database"
video_results_json = []
for video_id, title in list_popular_music_ids:
video_json = {}
video_json["id"] = video_id
video_json["title"] = title
if video_id in language_table:
video_json["language"] = language_table[video_id]
if video_id in geo_table:
video_json["geo"] = geo_table[video_id]
if video_id in retweet_table:
video_json["retweet_network"] = retweet_table[video_id]
if video_id in max_retweet_table:
video_json["num_retweet_was_retweeted"] = max_retweet_table[video_id][0]
video_json["max_retweet_count"] = max_retweet_table[video_id][1]['text']
if video_id in time_of_day_table:
video_json["popular_time"] = time_of_day_table[video_id]
video_results_json.append(video_json)
io = StringIO()
json.dump(video_results_json, io)
cursor.execute("INSERT into videos VALUES ('%s', 'popular', '%s')" % (table[1:], re.escape(io.getvalue())))
conn.commit()
示例14: StringIO
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
#! /usr/bin/env python
import simplejson
obj = simplejson.dumps(['foo', {'bar':('baz', None, 1.0, 2)}])
print obj
print simplejson.dumps("\"foo\bar")
print simplejson.dumps(u'\u1234')
print simplejson.dumps('\\')
print simplejson.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
from simplejson.compat import StringIO
io = StringIO()
simplejson.dump(['streaming API'], io)
print io.getvalue()
print(simplejson.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4 * ' '))
obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
print simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
print simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')[0]
print simplejson.loads('"\\"foo\\bar"') == u'"foo\x08ar'
print u'"foo\x08ar'
print simplejson.loads('"\\"foo\\bar"')
io = StringIO('["streaming API"]')
print simplejson.load(io)[0] == 'streaming API'
示例15: sio_dump
# 需要导入模块: from simplejson.compat import StringIO [as 别名]
# 或者: from simplejson.compat.StringIO import getvalue [as 别名]
def sio_dump(obj, **kw):
sio = StringIO()
json.dumps(obj, **kw)
return sio.getvalue()