本文整理汇总了Python中six.moves.StringIO.tell方法的典型用法代码示例。如果您正苦于以下问题:Python StringIO.tell方法的具体用法?Python StringIO.tell怎么用?Python StringIO.tell使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.StringIO
的用法示例。
在下文中一共展示了StringIO.tell方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_is_xml
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import tell [as 别名]
def test_is_xml(self):
xmlfile = StringIO('<?xml version="1.0"?>\n<tag>\n')
htmlfile = StringIO('<html>\n<head></head>\n<body><p></p></body>\n</html>')
self.assertEqual(summary.isxml_stream(xmlfile), True)
self.assertEqual(xmlfile.tell(), 0)
self.assertEqual(summary.isxml_stream(htmlfile), False)
示例2: TestStreamStructure
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import tell [as 别名]
class TestStreamStructure(TestInMemoryStructure):
def json(self, head):
return jl(
jd(
(q(self.LIST_TOKEN), jl(*(q(w) for w in self.LIST))),
(q(self.DICT_TOKEN), jd(*((q(k), q(v)) for k,v in self.DICT.items()))),
(q(self.HEAD_TOKEN), jd(*((q(k), q(v)) for k,v in self.HEAD.items()))),
),
q(head),
)
def get_read_store(self):
self.stream = StringIO(self.json(self.HEAD_TOKEN))
return structure.JSONStreamReadStructure(self.stream)
def get_write_store(self):
self.stream = StringIO()
return structure.JSONStreamWriteStructure(self.stream)
def verify_dict(self, received, expect):
tools.assert_equal(dict(expect), received)
def verify_list(self, received, expect):
tools.assert_equal(list(expect), received)
def verify_write(self, store, head):
# Nothing written to stream until close().
tools.assert_equal(0, self.stream.tell())
tools.assert_equal('', self.stream.read())
store.close()
self.stream.seek(0)
tools.assert_equal(self.json(head), self.stream.read())
def test_no_close(self):
store = self.get_read_store()
tools.assert_raises(AttributeError, getattr, store, 'close')
示例3: open
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import tell [as 别名]
def open(self,f=None,mode='r',encrypt=False,sign=False):
"""write: encrypt =bool or list of recipients, sign = sender or bool(default_key)
read: encrypt = bool encrypted data expected, sign= expected key or True=defaultkey
"""
from six.moves import StringIO
from six import next, PY3, BytesIO
self.mode,self.encrypt,self.sign = mode,encrypt,sign
if self.required is True: self.required = self.columns
if encrypt or sign: assert self.gpg, 'gpg not intialized'
self.origfile = self.file = f
assert mode in ('r','w'), 'invalid mode'
if mode=='r':
if sign:
if sign is True:
fingerprint = self.gpg.default_key
if type(fingerprint) == tuple: fingerprint = fingerprint[0]
else:
if type(sign) == tuple: sign = sign[0]
fingerprint = self.gpg.find_key(sign)
assert fingerprint, "sender key not found"
if self.fileformat=='csv':
import re
if encrypt:
if PY3 and isinstance(f,StringIO):
result = self.gpg.decrypt_str(f.getvalue())
else:
result = self.gpg.decrypt_file(f)
assert result.ok, "decryption failed"
if sign: assert result.valid and result.fingerprint==fingerprint, 'invalid signature'
f = StringIO(str(result))
elif sign:
if PY3 and isinstance(f,StringIO):
result = self.gpg.verify_str(f.getvalue())
f = StringIO(self.gpg.without_signature(f.getvalue()))
else:
result = self.gpg.verify_file(f)
f.seek(0)
f = StringIO(self.gpg.without_signature(f.read()))
assert result.valid and result.fingerprint==fingerprint, 'invalid signature'
self.file = f
dialect = self.dialect
if not dialect:
pos = f.tell()
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(pos) # rewind
if not PY3:
import unicodecsv
reader = unicodecsv.reader
else:
reader = csv.reader
reader = reader(f,dialect=dialect)
preamble = next(reader)
assert len(preamble), 'invalid file format'
assert preamble[0]==self.dataformat, "file format not supported"
preamble = re.match(r'^(\d+).(\d+)',preamble[1])
assert int(preamble.group(2))<=self.version[0], "format version not supported"
fields = next(reader)
self.csv = reader
else: # self.fileformat in ('json','jsondict','json-file','jsondict-file'):
import json
if self.fileformat in ('json-file','jsondict-file'):
self.file = f = json.load(f)
data, encrypted, signed, result = json_decrypt(f,self.gpg)
assert data, 'invalid input'
if encrypt: assert encrypted==bool(encrypt), 'encryption expected'
if sign:
assert signed==bool(sign), 'signature expected'
assert result.valid and result.fingerprint==fingerprint, 'invalid signature'
assert 'format' in data and data['format']==self.dataformat, "file format not supported"
assert 'version' in data and data['version'][0]<=self.version[0], "file version not supported"
assert 'fields' in data , "fields missing"
fields = data['fields']
self.rows = data['data']
columns, unknown = [], []
for field in fields:
if field in self.columns: columns.append(field)
elif self.ignore: unknown.append(field)
else: assert False, "unknown field '%s'" % field
if self.required:
for field in self.required:
assert field in columns, "missing required field '%s'" % field
self.fields = fields
self.read_columns = (columns,unknown)
elif mode=='w':
assert self.fileformat in ('json','jsondict') or self.file, 'file missing'
if self.fileformat=='csv':
if encrypt or sign: self.file = StringIO()
else: self.file = f
if not PY3:
import unicodecsv
writer = unicodecsv.writer
else:
writer = csv.writer
self.csv = writer(self.file,lineterminator='\n',dialect=self.dialect)
self.csv.writerow((self.dataformat,'%i.%i' % tuple(self.version)))
self.csv.writerow(self.columns)
else: # self.fileformat in ('json','jsondict'):
self.rows = []