fileslice Reference

Date:

2018-10-10

Version:

0.0.2

Authors:

File slice is a part of a file. This library allows you to open a binary file and see only part of it. You specify the start and end of the file part you want to see. Read and write operations will always start at the specified start position and will never exceed the specified end position. Here is an example of how to use it:

from fileslice import Slicer

#let's open a file for reading
r = open('example.png', 'br')

#create a slicer for the file
slicer = Slicer(r)

#the slicer behaves like a function. call it to create as many fileslices
#as you want
start = 5   #the beginning of our partial file is at 5
size = 95   #the size of the part is 95 bytes so our end is 99
fileslice = slicer (start, size)     #this is a file like object

#now we have a fileslice file from byte 5 to byte 99.
#the initial partial file seek position is 0.
print(fileslice.read())  #will print from byte 5 to 99.

#now our seek position is at the end of the fileslice file
#that is byte 100 of the full file
try:
    #if we seek to a position out of the fileslice file range (from 0 to 95)
    fileslice.seek(200)
except ValueError:
    #an exception will be thrown
    print('error while seeking to 200')

#we can seek from the end of the fileslice or from current fileslice
#seek position
#let's seek to fifth byte from the end of the file. that is byte 95
fileslice.seek(-5, 2)

This library also works in writable files. Multiple threads can be used to read and write to different file slices from the same file. Just make sure you do not use the original opened file or the fileslices (and the original file object) will be confused.

class fileslice.FileSlice(f, start, size, lock)

Note

The Slicer class is a convinient way to create instances of this class.

A file slice. An instance of this class is a file-like object that only looks at a specific region in the original file object. Multiple slices can be created from the same file object and each slice will have its own seek position and you can read and write to each slice independantly. FileSlice instances generated from the same Slicer object are thread-safe.

In addition to the class methods, the following methods call the methods with the same name from the original file object:

  • readable.
  • writable.
  • seekable.
  • isatty.
  • fileno.
close()

Same as file.close() but for the slice. All file access operations will raise ValueError after the file is closed. The original file object is not closed.

end

The end position of the slice. The slice will not read or write if the new position is ahead of this value.

flush()

Flushes the original file.

read(size=-1)

Same as file.read() but for the slice. Does not read beyond self.end.

seek(offset, whence=0)

Same as file.seek() but for the slice. Returns a value between self.start and self.size inclusive.

raises:
ValueError if the new seek position is not between 0 and self.size.
tell()

Same as file.tell() but for the slice. Returns a value between self.start and self.size inclusive.

write(b)

Same as file.write() but for the slice.

raises:
EOFError if the new seek position is > self.size.
writelines(lines)

Same as file.writelines() but for the slice.

raises:
EOFError if the new seek position is > self.size.
class fileslice.Slicer(f)

File slicer. Use this class to slice a file into multiple file slices and use each slice as if it was an independant file.

slices(size, n=3)

Create equal sized slices of the file. The last slice may be larger than the others.

args:
  • size (int): The full size to be sliced.
  • n (int): The number of slices to return.
returns:
A list of FileSlice objects of length (n).