本文整理汇总了Python中io.SEEK_CUR属性的典型用法代码示例。如果您正苦于以下问题:Python io.SEEK_CUR属性的具体用法?Python io.SEEK_CUR怎么用?Python io.SEEK_CUR使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类io
的用法示例。
在下文中一共展示了io.SEEK_CUR属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: seek
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def seek(self, offset: int, whence: int = io.SEEK_SET) -> int:
self._check_open()
self._check_random()
if whence == io.SEEK_SET:
ok = self.dev.seek(offset)
elif whence == io.SEEK_CUR:
ok = self.dev.seek(self.tell() + offset)
elif whence == io.SEEK_END:
ok = self.dev.seek(len(self) + offset)
else:
raise io.UnsupportedOperation("whence = {} is not "
"supported!".format(whence))
if not ok:
raise QtOSError(self.dev, msg="seek failed!")
return self.dev.pos()
示例2: seek
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def seek(self, offset, whence=io.SEEK_SET):
if whence == io.SEEK_CUR:
offset = self.tell() + offset
elif whence == io.SEEK_END:
offset = self.dir.byte_size + offset
if offset < 0:
raise ValueError('New position is before the start of the stream')
if offset > self.dir.byte_size:
# logging.debug("overseek %d bytes, padding with zeros" % (offset - self.dir.byte_size))
self.pos = self.dir.byte_size
bytes_left = offset - self.dir.byte_size
min_seek_size = self.storage.sector_size * 4
while bytes_left:
bytes_to_write = min(min_seek_size, offset - self.dir.byte_size)
zeros = bytearray(bytes_to_write)
self.write(zeros)
bytes_left -= bytes_to_write
self.pos = offset
return offset
示例3: __init__
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def __init__(self, fobj):
"""
fobj is a file-like object as an icns resource
"""
# signature : (start, length)
self.dct = dct = {}
self.fobj = fobj
sig, filesize = nextheader(fobj)
if sig != b"icns":
raise SyntaxError("not an icns file")
i = HEADERSIZE
while i < filesize:
sig, blocksize = nextheader(fobj)
if blocksize <= 0:
raise SyntaxError("invalid block header")
i += HEADERSIZE
blocksize -= HEADERSIZE
dct[sig] = (i, blocksize)
fobj.seek(blocksize, io.SEEK_CUR)
i += blocksize
示例4: __init__
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def __init__(self, fobj):
"""
fobj is a file-like object as an icns resource
"""
# signature : (start, length)
self.dct = dct = {}
self.fobj = fobj
sig, filesize = nextheader(fobj)
if sig != b'icns':
raise SyntaxError('not an icns file')
i = HEADERSIZE
while i < filesize:
sig, blocksize = nextheader(fobj)
if blocksize <= 0:
raise SyntaxError('invalid block header')
i += HEADERSIZE
blocksize -= HEADERSIZE
dct[sig] = (i, blocksize)
fobj.seek(blocksize, io.SEEK_CUR)
i += blocksize
示例5: seek
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def seek(self, offset, whence=io.SEEK_SET):
if self.mode == WRITE:
if whence != io.SEEK_SET:
if whence == io.SEEK_CUR:
offset = self.offset + offset
else:
raise ValueError('Seek from end not supported')
if offset < self.offset:
raise OSError('Negative seek in write mode')
count = offset - self.offset
chunk = bytes(1024)
for i in range(count // 1024):
self.write(chunk)
self.write(bytes(count % 1024))
elif self.mode == READ:
self._check_not_closed()
return self._buffer.seek(offset, whence)
return self.offset
示例6: seek
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def seek(self, offset, whence=io.SEEK_SET):
if self.mode == WRITE:
if whence != io.SEEK_SET:
if whence == io.SEEK_CUR:
offset = self.offset + offset
else:
raise ValueError('Seek from end not supported')
if offset < self.offset:
raise OSError('Negative seek in write mode')
count = offset - self.offset
chunk = b'\0' * 1024
for i in range(count // 1024):
self.write(chunk)
self.write(b'\0' * (count % 1024))
elif self.mode == READ:
self._check_not_closed()
return self._buffer.seek(offset, whence)
return self.offset
示例7: test_seek_and_read
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def test_seek_and_read(self):
def mkfile(basename, content):
part = os.path.join(self.tmpdir.name, basename)
with fopen(part, 'wb') as f:
f.write(content)
return part
content = b'abc\nd\nefgh\nij'
part1 = mkfile('1', content[:4])
part2 = mkfile('2', content[4:5])
part3 = mkfile('3', content[5:])
with MultiFileReader(part1, part2, part3) as m:
self.assertEqual(m.size, len(content))
m.seek(2)
self.assertEqual(m.read(2), content[2:4])
m.seek(1)
self.assertEqual(m.read(len(content) - 2), content[1:-1])
m.seek(-1, whence=io.SEEK_END)
self.assertEqual(m.read(10), content[-1:])
m.seek(4)
m.seek(-2, whence=io.SEEK_CUR)
self.assertEqual(m.read(3), content[2:5])
示例8: testSliceFileStartEnd
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def testSliceFileStartEnd(self):
with tempfile.TemporaryFile() as f:
f.write(b'123456789')
f.flush()
part = FileSlice(f, 0, 5)
self.assertEqual(len(part), 5)
self.assertEqual(part.read(), b'12345')
self.assertEqual(part.read(3), b'')
part.seek(0, io.SEEK_SET)
self.assertEqual(part.read(3), b'123')
self.assertEqual(part.tell(), 3)
part.seek(-3, io.SEEK_CUR)
self.assertEqual(part.tell(), 0)
part.seek(-2, io.SEEK_END)
self.assertEqual(part.tell(), 3)
self.assertEqual(part.readall(), b'45')
with self.assertRaises(IOError):
part.write('abc')
with self.assertRaises(IOError):
part.writelines(['foo', 'bar'])
示例9: testSliceFileStartLength
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def testSliceFileStartLength(self):
with tempfile.TemporaryFile() as f:
f.write(b'123456789')
f.flush()
part = FileSlice(f, 0, length=5)
self.assertEqual(len(part), 5)
self.assertEqual(part.read(), b'12345')
self.assertEqual(part.read(3), b'')
part.seek(0)
self.assertEqual(part.read(3), b'123')
self.assertEqual(part.tell(), 3)
part.seek(-3, io.SEEK_CUR)
self.assertEqual(part.readall(), b'12345')
with self.assertRaises(IOError):
part.write('abc')
with self.assertRaises(IOError):
part.writelines(['foo', 'bar'])
示例10: testSanityChecks
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def testSanityChecks(self):
with tempfile.TemporaryFile() as f:
f.write(b'123456789')
f.flush()
with self.assertRaises(ValueError):
part = FileSlice(f, -5, -2)
with self.assertRaises(ValueError):
part = FileSlice(f, 0, -2)
with self.assertRaises(ValueError):
part = FileSlice(f, -10, 2)
with self.assertRaises(ValueError):
part = FileSlice(f, 10, 2)
with self.assertRaises(ValueError):
part = FileSlice(f, 10, length=-2)
part = FileSlice(f, 1, 5)
with self.assertRaises(ValueError):
part.seek(8)
with self.assertRaises(ValueError):
part.seek(8, io.SEEK_SET)
part.seek(3)
with self.assertRaises(ValueError):
part.seek(4, io.SEEK_CUR)
with self.assertRaises(ValueError):
part.seek(-5, io.SEEK_END)
示例11: seek
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def seek(self, offset, whence=io.SEEK_SET):
if whence == io.SEEK_SET:
desired_pos = self._start + offset
if whence == io.SEEK_CUR:
desired_pos = self._handle.tell() + offset
if whence == io.SEEK_END:
desired_pos = self._end + offset
if desired_pos < self._start:
raise ValueError("Seeking before the file slice")
if desired_pos > self._end:
raise ValueError("Seekeing past the end of file slice")
ret = self._handle.seek(desired_pos, io.SEEK_SET)
if ret:
return ret - self._start
else:
return ret
示例12: seek
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def seek(self, offset, whence=io.SEEK_SET):
if whence == io.SEEK_CUR:
self.offset += offset
elif whence == io.SEEK_END:
self.offset = self.cumsizes[-1] + offset
elif whence == io.SEEK_SET:
self.offset = offset
if self.offset < 0:
raise Exception("Trying to seek before the start of the file!")
if self.offset >= self.cumsizes[-1]:
return self.offset
i = self._findStencil( self.offset )
offsetInsideStencil = self.offset - self.cumsizes[i]
assert offsetInsideStencil >= 0
assert offsetInsideStencil < self.sizes[i]
self.fileobj.seek( self.offsets[i] + offsetInsideStencil, io.SEEK_SET )
return self.offset
示例13: pack
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def pack(self):
""" Pack message to binary stream. """
payload = io.BytesIO()
# Advance num bytes equal to header size - the header is written later
# after the payload of all segments and parts has been written:
payload.seek(self.header_size, io.SEEK_CUR)
# Write out payload of segments and parts:
self.build_payload(payload)
packet_length = len(payload.getvalue()) - self.header_size
self.header = MessageHeader(self.session_id, self.packet_count, packet_length, constants.MAX_SEGMENT_SIZE,
num_segments=len(self.segments), packet_options=0)
packed_header = self.header_struct.pack(*self.header)
# Go back to begining of payload for writing message header:
payload.seek(0)
payload.write(packed_header)
payload.seek(0, io.SEEK_END)
trace(self)
return payload
示例14: pack
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def pack(self, payload, **kwargs):
# remember position in payload object:
segment_payload_start_pos = payload.tell()
# Advance num bytes equal to header size. The header is written later
# after the payload of all segments and parts has been written:
payload.seek(self.header_size, io.SEEK_CUR)
# Generate payload of parts:
self.build_payload(payload)
segment_length = payload.tell() - segment_payload_start_pos # calc length of parts payload
self.header = RequestSegmentHeader(segment_length, self.offset, len(self.parts), self.number, self.segment_kind,
self.message_type, int(kwargs.get('commit', 0)), self.command_options)
packed_header = self.header_struct.pack(*self.header)
# Go back to beginning of payload header for writing segment header:
payload.seek(segment_payload_start_pos)
payload.write(packed_header)
# Put file pointer at the end of the bffer so that next segment can be appended:
payload.seek(0, io.SEEK_END)
示例15: seek
# 需要导入模块: import io [as 别名]
# 或者: from io import SEEK_CUR [as 别名]
def seek(self, offset, where=SEEK_SET):
"""Seek the position by `offset` relative to `where`.
Args:
offset: move the read pointer by `offset` bytes.
where: same as io.SEEK_END, io.SEEK_CUR or io.SEEK_SET.
"""
if where == SEEK_CUR:
cur = len(self.read_file)
pos = cur + offset
elif where == SEEK_END:
pos = len(self.read_file) + len(self.file) + offset
else:
pos = offset
if pos < 0:
pos = 0
self.file = self.read_file + self.file
self.read_file = self.file[:pos]
self.file = self.file[pos:]
self.cur_fd = None