本文整理汇总了Python中six.BytesIO.tell方法的典型用法代码示例。如果您正苦于以下问题:Python BytesIO.tell方法的具体用法?Python BytesIO.tell怎么用?Python BytesIO.tell使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.BytesIO
的用法示例。
在下文中一共展示了BytesIO.tell方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_header
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def _get_header(self):
chunk = self._transport.chunk_read(0, HEADER_SIZE_GUESS)
stream = BytesIO(chunk)
magic = read_n(stream, len(MAGIC))
if magic == INCOMPLETE_MAGIC:
raise ZSCorrupt("%s: looks like this ZS file was only "
"partially written" % (self._transport.name,))
if magic != MAGIC:
raise ZSCorrupt("%s: bad magic number (are you sure this is "
"a ZS file?)" % (self._transport.name))
header_data_length, = read_format(stream, header_data_length_format)
needed = header_data_length + CRC_LENGTH
header_end = stream.tell() + needed
remaining = len(chunk) - stream.tell()
if remaining < needed:
rest = self._transport.chunk_read(len(chunk), needed - remaining)
stream = BytesIO(stream.read() + rest)
header_encoded = read_n(stream, header_data_length)
header_crc = read_n(stream, CRC_LENGTH)
if encoded_crc64xz(header_encoded) != header_crc:
raise ZSCorrupt("%s: header checksum mismatch"
% (self._transport.name,))
return _decode_header_data(header_encoded), header_end
示例2: test_detect_encoding
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def test_detect_encoding(app):
"""Test encoding detection."""
f = BytesIO(u'Γκρήκ Στρίνγκ'.encode('utf-8'))
initial_position = f.tell()
assert detect_encoding(f).lower() == 'utf-8'
assert f.tell() == initial_position
with patch('cchardet.detect', Exception):
assert detect_encoding(f) is None
示例3: unpack
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def unpack(self, data, trailing=False):
stream = BytesIO(data)
self.unpack_stream(stream)
stream.tell()
if trailing:
return stream
elif stream.tell() != len(data):
raise SuitcaseParseError("Structure fully parsed but additional bytes remained. Parsing "
"consumed %d of %d bytes" %
(stream.tell(), len(data)))
示例4: QiniuFile
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
class QiniuFile(File):
def __init__(self, name, storage, mode):
self._storage = storage
if name.startswith(self._storage.location):
name = name[len(self._storage.location):]
self._name = name.lstrip('/')
self._mode = mode
self.file = BytesIO()
self._is_dirty = False
self._is_read = False
@property
def size(self):
if self._is_dirty or self._is_read:
# Get the size of a file like object
# Check http://stackoverflow.com/a/19079887
old_file_position = self.file.tell()
self.file.seek(0, os.SEEK_END)
self._size = self.file.tell()
self.file.seek(old_file_position, os.SEEK_SET)
if not hasattr(self, '_size'):
self._size = self._storage.size(self._name)
return self._size
def read(self, num_bytes=None):
if not self._is_read:
content = self._storage._read(self._name)
self.file = BytesIO(content)
self._is_read = True
if num_bytes is None:
data = self.file.read()
else:
data = self.file.read(num_bytes)
if 'b' in self._mode:
return data
else:
return force_text(data)
def write(self, content):
if 'w' not in self._mode:
raise AttributeError("File was opened for read-only access.")
self.file.write(force_bytes(content))
self._is_dirty = True
self._is_read = True
def close(self):
if self._is_dirty:
self.file.seek(0)
self._storage._save(self._name, self.file)
self.file.close()
示例5: test_put_should_accept_file_like_objects
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def test_put_should_accept_file_like_objects(self):
"""
put()'s local_path arg should take file-like objects too
"""
local = self.path("whatever")
fake_file = StringIO()
fake_file.write("testing file-like objects in put()")
pointer = fake_file.tell()
target = "/new_file.txt"
with hide("everything"):
put(fake_file, target)
get(target, local)
eq_contents(local, fake_file.getvalue())
# Sanity test of file pointer
eq_(pointer, fake_file.tell())
示例6: from_bytes
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def from_bytes(cls, bytes):
"""
Parse a ``ClientHello`` struct.
:param bytes: the bytes representing the input.
:return: ClientHello object.
"""
construct = _constructs.ClientHello.parse(bytes)
# XXX Is there a better way in Construct to parse an array of
# variable-length structs?
extensions = []
extensions_io = BytesIO(construct.extensions_bytes)
while extensions_io.tell() < construct.extensions_length:
extension_construct = _constructs.Extension.parse_stream(
extensions_io)
extensions.append(
Extension(type=ExtensionType(extension_construct.type),
data=extension_construct.data))
return ClientHello(
client_version=ProtocolVersion(
major=construct.version.major,
minor=construct.version.minor,
),
random=Random(
gmt_unix_time=construct.random.gmt_unix_time,
random_bytes=construct.random.random_bytes,
),
session_id=construct.session_id.session_id,
# TODO: cipher suites should be enums
cipher_suites=construct.cipher_suites.cipher_suites,
compression_methods=(
construct.compression_methods.compression_methods
),
extensions=extensions,
)
示例7: is_zipstream
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def is_zipstream(data):
"""
just like zipfile.is_zipfile, but works upon buffers and streams
rather than filenames.
If data supports the read method, it will be treated as a stream
and read from to test whether it is a valid ZipFile.
If data also supports the tell and seek methods, it will be
rewound after being tested.
"""
if isinstance(data, (str, buffer)):
data = BytesIO(data)
if hasattr(data, "read"):
tell = 0
if hasattr(data, "tell"):
tell = data.tell()
try:
result = bool(_EndRecData(data))
except IOError:
result = False
if hasattr(data, "seek"):
data.seek(tell)
else:
raise TypeError("requies str, buffer, or stream-like object")
return result
示例8: build_xlsx_response
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def build_xlsx_response(self, wb, title="report"):
""" Take a workbook and return a xlsx file response """
title = generate_filename(title, '.xlsx')
myfile = BytesIO()
myfile.write(save_virtual_workbook(wb))
response = HttpResponse(
myfile.getvalue(),
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s' % title
response['Content-Length'] = myfile.tell()
return response
示例9: UniversalBytesIO
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
class UniversalBytesIO(object):
def __init__(self, container=None, charset=None):
self.charset = charset or settings.DEFAULT_CHARSET
self._container = BytesIO() if container is None else container
# These methods partially implement the file-like object interface.
# See https://docs.python.org/3/library/io.html#io.IOBase
def close(self):
self._container.close()
def write(self, content):
self._container.write(self.make_bytes(content))
def flush(self):
self._container.flush()
def tell(self):
return self._container.tell()
def readable(self):
return False
def seekable(self):
return False
def writable(self):
return True
def writelines(self, lines):
for line in lines:
self.write(line)
def make_bytes(self, value):
"""Turn a value into a bytestring encoded in the output charset."""
if isinstance(value, bytes):
return bytes(value)
if isinstance(value, six.text_type):
return bytes(value.encode(self.charset))
# Handle non-string types
return force_bytes(value, self.charset)
def get_string_value(self):
return self._container.getvalue().decode(self.charset)
def getvalue(self):
return self._container.getvalue()
if sys.version_info[0:2] < (3, 5):
def seek(self, *args, **kwargs):
pass
示例10: getSize
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def getSize(obj):
"""Calculate the size as cheap as possible
"""
# Try the cheap variants first.
# Actually the checks ensure the code never fails but beeing sure
# is better.
try:
# check if to return zero (length is zero)
if len(obj) == 0:
return 0
except:
pass
try:
# check if ``IStreamableReference``
if IStreamableReference.providedBy(obj):
size = obj.getSize()
if size is not None:
return size
except:
pass
try:
# string
if isinstance(obj, types.StringTypes):
return len(obj)
except:
pass
try:
# file like object
methods = dir(obj)
if "seek" in methods and "tell" in methods:
currentPos = obj.tell()
obj.seek(0, 2)
size = obj.tell()
obj.seek(currentPos)
return size
except:
pass
try:
# fallback: pickling the object
stream = BytesIO()
p = Pickler(stream, 1)
p.dump(obj)
size = stream.tell()
except:
size = None
return size
示例11: _save_to_cache
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def _save_to_cache(self, cache_file_name, suffix, program_source,
program_binaries, device_id_dict):
naked_suffix = suffix
suffix = "." + suffix
cache_file_name = cache_file_name + suffix + (".3" if PY3 else ".2")
if not os.path.isabs(cache_file_name):
cache_file_name = os.path.join(root.common.dirs.cache,
cache_file_name)
try:
with tarfile.open("%s.cache" % cache_file_name, "w:gz") as tar:
source_io = BytesIO()
source_io.write(program_source)
ti = tarfile.TarInfo("source" + suffix)
ti.size = source_io.tell()
ti.mode = int("666", 8)
source_io.seek(0)
tar.addfile(ti, fileobj=source_io)
for dep in set(self._scan_include_dependencies(naked_suffix)):
ti = tarfile.TarInfo(os.path.basename(dep))
ti.size = os.path.getsize(dep)
ti.mode = int("666", 8)
with open(dep, "rb") as fr:
tar.addfile(ti, fileobj=fr)
binaries_io = BytesIO()
pickler = pickle.Pickler(binaries_io, protocol=best_protocol)
binaries = {"binaries": program_binaries}
binaries.update(device_id_dict)
pickler.dump(binaries)
ti = tarfile.TarInfo("binaries.pickle")
ti.size = binaries_io.tell()
ti.mode = int("666", 8)
binaries_io.seek(0)
tar.addfile(ti, fileobj=binaries_io)
except:
self.exception("Failed to save the cache file %s:",
cache_file_name)
示例12: update_photo_ad
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def update_photo_ad(self):
# Update self.photo_ad to a 240x240 thumbnail >10 kb in size.
if not self.photo:
if self.photo_ad:
self.photo_ad.delete()
return
from PIL import Image
from six import BytesIO
from django.core.files.base import ContentFile
if hasattr(self.photo.file, 'content_type'):
PHOTO_TYPE = self.photo.file.content_type
if PHOTO_TYPE == 'image/jpeg':
PIL_TYPE = 'jpeg'
elif PHOTO_TYPE == 'image/png':
PIL_TYPE = 'png'
else:
return
else:
PIL_TYPE = 'jpeg'
# good defaults to get ~10kb JPEG images
PHOTO_AD_SIZE = (240, 240)
PIL_QUALITY = 75
# remote file size limit
PHOTO_AD_FILESIZE = 10000
image = Image.open(BytesIO(self.photo.read()))
image.thumbnail(PHOTO_AD_SIZE, Image.LANCZOS)
# in case we miss 10kb, drop the quality and recompress
for i in range(12):
temp_buffer = BytesIO()
image.save(temp_buffer, PIL_TYPE,
quality=PIL_QUALITY, optimize=True)
length = temp_buffer.tell()
if length <= PHOTO_AD_FILESIZE:
break
if PIL_TYPE == 'png':
PIL_TYPE = 'jpeg'
else:
PIL_QUALITY -= 5
temp_buffer.seek(0)
self.photo_ad.save(os.path.basename(self.photo.name),
ContentFile(temp_buffer.read()), save=False)
示例13: parse_certificate
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def parse_certificate(bytes):
"""
Parse a ``Certificate`` struct.
:param bytes: the bytes representing the input.
:return: Certificate object.
"""
construct = _constructs.Certificate.parse(bytes)
# XXX: Find a better way to parse an array of variable-length objects
certificates = []
certificates_io = BytesIO(construct.certificates_bytes)
while certificates_io.tell() < construct.certificates_length:
certificate_construct = _constructs.ASN1Cert.parse_stream(
certificates_io
)
certificates.append(certificate_construct.asn1_cert)
return Certificate(
certificate_list=certificates
)
示例14: _cloneByPickle
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
def _cloneByPickle(self, obj):
"""Returns a deep copy of a ZODB object, loading ghosts as needed.
"""
modifier = getToolByName(self, 'portal_modifier')
callbacks = modifier.getOnCloneModifiers(obj)
if callbacks is not None:
pers_id, pers_load, inside_orefs, outside_orefs = callbacks[0:4]
else:
inside_orefs, outside_orefs = (), ()
stream = BytesIO()
p = Pickler(stream, 1)
if callbacks is not None:
p.persistent_id = pers_id
p.dump(aq_base(obj))
approxSize = stream.tell()
stream.seek(0)
u = Unpickler(stream)
if callbacks is not None:
u.persistent_load = pers_load
return approxSize, u.load(), inside_orefs, outside_orefs
示例15: InputFile
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import tell [as 别名]
class InputFile(object):
max_buffer_size = 1024*1024
def __init__(self, rfile, length):
"""File-like object used to provide a seekable view of request body data"""
self._file = rfile
self.length = length
self._file_position = 0
if length > self.max_buffer_size:
self._buf = tempfile.TemporaryFile()
else:
self._buf = BytesIO()
@property
def _buf_position(self):
rv = self._buf.tell()
assert rv <= self._file_position
return rv
def read(self, bytes=-1):
assert self._buf_position <= self._file_position
if bytes < 0:
bytes = self.length - self._buf_position
bytes_remaining = min(bytes, self.length - self._buf_position)
if bytes_remaining == 0:
return b""
if self._buf_position != self._file_position:
buf_bytes = min(bytes_remaining, self._file_position - self._buf_position)
old_data = self._buf.read(buf_bytes)
bytes_remaining -= buf_bytes
else:
old_data = b""
assert bytes_remaining == 0 or self._buf_position == self._file_position, (
"Before reading buffer position (%i) didn't match file position (%i)" %
(self._buf_position, self._file_position))
new_data = self._file.read(bytes_remaining)
self._buf.write(new_data)
self._file_position += bytes_remaining
assert bytes_remaining == 0 or self._buf_position == self._file_position, (
"After reading buffer position (%i) didn't match file position (%i)" %
(self._buf_position, self._file_position))
return old_data + new_data
def tell(self):
return self._buf_position
def seek(self, offset):
if offset > self.length or offset < 0:
raise ValueError
if offset <= self._file_position:
self._buf.seek(offset)
else:
self.read(offset - self._file_position)
def readline(self, max_bytes=None):
if max_bytes is None:
max_bytes = self.length - self._buf_position
if self._buf_position < self._file_position:
data = self._buf.readline(max_bytes)
if data.endswith(b"\n") or len(data) == max_bytes:
return data
else:
data = b""
assert self._buf_position == self._file_position
initial_position = self._file_position
found = False
buf = []
max_bytes -= len(data)
while not found:
readahead = self.read(min(2, max_bytes))
max_bytes -= len(readahead)
for i, c in enumerate(readahead):
if c == b"\n"[0]:
buf.append(readahead[:i+1])
found = True
break
if not found:
buf.append(readahead)
if not readahead or not max_bytes:
break
new_data = b"".join(buf)
data += new_data
self.seek(initial_position + len(new_data))
return data
def readlines(self):
rv = []
while True:
data = self.readline()
if data:
#.........这里部分代码省略.........