当前位置: 首页>>代码示例>>Python>>正文


Python compat.StringIO类代码示例

本文整理汇总了Python中simplejson.compat.StringIO的典型用法代码示例。如果您正苦于以下问题:Python StringIO类的具体用法?Python StringIO怎么用?Python StringIO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了StringIO类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: check

        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)
开发者ID:ppizarror,项目名称:Hero-of-Antair,代码行数:7,代码来源:test_indent.py

示例2: test_tuple_array_dump

 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())
开发者ID:All4Gis,项目名称:instagram2qgis,代码行数:25,代码来源:test_tuple.py

示例3: main

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()
开发者ID:houshuang,项目名称:PycharmProjects,代码行数:7,代码来源:views.py

示例4: test_sort_keys

 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)
开发者ID:62484m,项目名称:QQ-Groups-Spider,代码行数:8,代码来源:test_dump.py

示例5: test_asdict_not_callable_dump

 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())
开发者ID:ppizarror,项目名称:Hero-of-Antair,代码行数:9,代码来源:test_namedtuple.py

示例6: test_namedtuple_dump_false

 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)
开发者ID:ppizarror,项目名称:Hero-of-Antair,代码行数:10,代码来源:test_namedtuple.py

示例7: post

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()
开发者ID:houshuang,项目名称:PycharmProjects,代码行数:12,代码来源:views.py

示例8: test_decimal_defaults

 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())
开发者ID:hardtail0112,项目名称:twitter-python,代码行数:14,代码来源:test_decimal.py

示例9: test_datetime_defaults

 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())
开发者ID:hobson,项目名称:simplejson,代码行数:14,代码来源:test_datetime.py

示例10: write_popular_to_database

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()
开发者ID:hm95,项目名称:twitter_music_streamer,代码行数:26,代码来源:get_video_urls.py

示例11: test_namedtuple_dump

 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()))
开发者ID:ppizarror,项目名称:Hero-of-Antair,代码行数:20,代码来源:test_namedtuple.py

示例12: dumps

 def dumps(self, obj, **kw):
     sio = StringIO()
     json.dump(obj, sio, **kw)
     res = json.dumps(obj, **kw)
     self.assertEquals(res, sio.getvalue())
     return res
开发者ID:vsajip,项目名称:simplejson,代码行数:6,代码来源:test_decimal.py

示例13: test_dump

 def test_dump(self):
     sio = StringIO()
     json.dump({}, sio)
     self.assertEquals(sio.getvalue(), "{}")
开发者ID:git109,项目名称:simplejson,代码行数:4,代码来源:test_dump.py

示例14: print

# -*-coding:utf-8 -*-

import simplejson as json
from simplejson.compat import StringIO

# from StringIO import StringIO

obj = [u"foo", {u"bar": [u"baz", None, 1.0, 2]}]

print (json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj)

# 注意对内存的内容,每次操作后指针seek好像就改变了,所以需要重新seek
io = StringIO('["streaming API"]')
# print json.load(io).__class__
# io.seek(0)
# print io.readline()

# json.load(io) ==> list
print (json.load(io)[0] == "streaming API")
io.seek(0)
print len(json.load(io))


# 读取json格式字符串并解码为 python object
f = open("test", "r")
print json.load(f)
开发者ID:johnChnia,项目名称:pythoncore,代码行数:26,代码来源:test_decoding.py

示例15: StringIO

#coding=utf8
#! /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"]')
开发者ID:cobain,项目名称:ipython,代码行数:31,代码来源:jsontest.py


注:本文中的simplejson.compat.StringIO类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。