本文整理匯總了Python中io.SEEK_END屬性的典型用法代碼示例。如果您正苦於以下問題:Python io.SEEK_END屬性的具體用法?Python io.SEEK_END怎麽用?Python io.SEEK_END使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類io
的用法示例。
在下文中一共展示了io.SEEK_END屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: seek
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [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: test_readinto_partial_back_to_front
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [as 別名]
def test_readinto_partial_back_to_front(self, name, BQM, version):
bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'BINARY')
bqm.offset = 14
fv = FileView(bqm, version=version)
buff = fv.readall()
for pos in range(1, fv.quadratic_end):
fv.seek(-pos, io.SEEK_END)
subbuff = bytearray(pos) # length pos
self.assertEqual(fv.readinto(subbuff), len(subbuff))
self.assertEqual(buff[-pos:], subbuff)
示例3: seek
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [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
示例4: _find_offset
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [as 別名]
def _find_offset(self, fp):
s = fp.read(160)
if s[:4] == b"%!PS":
# for HEAD without binary preview
fp.seek(0, io.SEEK_END)
length = fp.tell()
offset = 0
elif i32(s[0:4]) == 0xC6D3D0C5:
# FIX for: Some EPS file not handled correctly / issue #302
# EPS can contain binary data
# or start directly with latin coding
# more info see:
# https://web.archive.org/web/20160528181353/http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf
offset = i32(s[4:8])
length = i32(s[8:12])
else:
raise SyntaxError("not an EPS file")
return (length, offset)
示例5: __init__
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [as 別名]
def __init__(self, fp, decomp_factory, trailing_error=(), **decomp_args):
self._fp = fp
self._eof = False
self._pos = 0 # Current offset in decompressed stream
# Set to size of decompressed stream once it is known, for SEEK_END
self._size = -1
# Save the decompressor factory and arguments.
# If the file contains multiple compressed streams, each
# stream will need a separate decompressor object. A new decompressor
# object is also needed when implementing a backwards seek().
self._decomp_factory = decomp_factory
self._decomp_args = decomp_args
self._decompressor = self._decomp_factory(**self._decomp_args)
# Exception class to catch from decompressor signifying invalid
# trailing data to ignore
self._trailing_error = trailing_error
示例6: test_seek_and_read
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [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])
示例7: find_camera_model
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [as 別名]
def find_camera_model(videos_folder):
from mapillary_tools.uploader import get_video_file_list
file_list = get_video_file_list(videos_folder)
fd = open(file_list[0], 'rb')
fd.seek(0, io.SEEK_END)
eof = fd.tell()
fd.seek(0)
while fd.tell() < eof:
try:
box = Box.parse_stream(fd)
except RangeError:
print('error parsing blackvue GPS information, exiting')
sys.exit(1)
except ConstError:
print('error parsing blackvue GPS information, exiting')
sys.exit(1)
if box.type.decode('utf-8') == 'free':# or 'ftyp':
return box.data[29:39]
示例8: _add_payloads
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [as 別名]
def _add_payloads(self):
"""
Adds all the stored payload to artifact.
Each payload is itself a compressed tar.
"""
filenames = sorted(list(self._payloads.keys()))
for filename in filenames:
fd = self._payloads[filename]
size = fd.seek(0, io.SEEK_END)
fd.seek(0)
payload_tarbin = io.BytesIO()
payload_tar = tarfile.open(fileobj=payload_tarbin, mode="w:gz")
tarhdr = tarfile.TarInfo(os.path.basename(filename))
tarhdr.size = size
payload_tar.addfile(tarhdr, fd)
payload_tar.close()
tarhdr = tarfile.TarInfo(os.path.dirname(filename) + ".tar.gz")
tarhdr.size = payload_tarbin.tell()
self._compute_checksum(filename, fd)
payload_tarbin.seek(0)
self._tarfact.addfile(tarhdr, payload_tarbin)
示例9: _detect_size
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [as 別名]
def _detect_size(self):
uncompressed_size = 0
if self.path.endswith(".gz"):
# This logic follow after the C# implementation
# See IngstionHelpers.cs for an explanation as to what stands behind it
with open(self.path, "rb") as f:
disk_size = f.seek(-4, SEEK_END)
uncompressed_size = struct.unpack("I", f.read(4))[0]
if (disk_size >= uncompressed_size) or (disk_size >= self.GZIP_MAX_DISK_SIZE_FOR_DETECTION):
uncompressed_size = disk_size * self.DEFAULT_COMPRESSION_RATIO
elif self.path.endswith(".zip"):
with ZipFile(self.path) as zip_archive:
for f in zip_archive.infolist():
uncompressed_size += f.file_size
else:
uncompressed_size = os.path.getsize(self.path)
self._size = uncompressed_size
示例10: testSliceFileStartEnd
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [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'])
示例11: testSanityChecks
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [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)
示例12: seek
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [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
示例13: __init__
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [as 別名]
def __init__(self, filename: str, tree_conf: TreeConf,
cache_size: int=512):
self._filename = filename
self._tree_conf = tree_conf
self._lock = rwlock.RWLock()
if cache_size == 0:
self._cache = FakeCache()
else:
self._cache = cachetools.LRUCache(maxsize=cache_size)
self._fd, self._dir_fd = open_file_in_dir(filename)
self._wal = WAL(filename, tree_conf.page_size)
if self._wal.needs_recovery:
self.perform_checkpoint(reopen_wal=True)
# Get the next available page
self._fd.seek(0, io.SEEK_END)
last_byte = self._fd.tell()
self.last_page = int(last_byte / self._tree_conf.page_size)
self._freelist_start_page = 0
# Todo: Remove this, it should only be in Tree
self._root_node_page = 0
示例14: _add_frame
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [as 別名]
def _add_frame(self, frame_type: FrameType, page: Optional[int]=None,
page_data: Optional[bytes]=None):
if frame_type is FrameType.PAGE and (not page or not page_data):
raise ValueError('PAGE frame without page data')
if page_data and len(page_data) != self._page_size:
raise ValueError('Page data is different from page size')
if not page:
page = 0
if frame_type is not FrameType.PAGE:
page_data = b''
data = (
frame_type.value.to_bytes(FRAME_TYPE_BYTES, ENDIAN) +
page.to_bytes(PAGE_REFERENCE_BYTES, ENDIAN) +
page_data
)
self._fd.seek(0, io.SEEK_END)
write_to_file(self._fd, self._dir_fd, data,
fsync=frame_type != FrameType.PAGE)
self._index_frame(frame_type, page, self._fd.tell() - self._page_size)
示例15: seek
# 需要導入模塊: import io [as 別名]
# 或者: from io import SEEK_END [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