dinopy.creader module

class dinopy.creader.CReader(source)

A cython extension class acting as a ‘low level’ reader for plain files. Implements the IOBase ‘interface’, i.e. supports all operations that any IOBase instance is to support. Also supports the ‘with’ statement/environment. If you plan to read a whole file at once, please use the readlines() method.

Parameters:

source (str) – Path of the file that is to be read.

Example 1:

with CReader("foo.txt") as bar:
    for line in bar:
        print(line)
close(self)

Flush and close this stream. This method has no effect if the file is already closed. Once the file is closed, any operation on the file (e.g. reading or writing) will raise an Error. As a convenience, it is allowed to call this method more than once; only the first call, however, will have an effect.

Raises:

IOError – If a non-zero error code has been returned by fclose.

closed
fileno(self) int

Return the underlying file descriptor (an integer) of the stream if it exists.

Raises:

OSError – If the underlying object does not use a file descriptor.

flush(self)

Will not do anything, as this is a read only stream.

isatty(self) bool

Returns false, as this is not a tty.

Returns:

False

open(self, unicode filename) CReader

Open the file described by the given filename.

Parameters:

filename (str) – Path to an existing file that will be opened.

Returns:

A CReader object on the opened file.

Raises:

FileNotFoundError – If no file with the given path exists.

read(self, int num_bytes) bytes

Tries reading num_bytes bytes from the underlying stream. If that is not possible, which is the case when we’re at the end of the underlying stream, will return None.

Parameters:

num_bytes (int) – The number of bytes to be read from the stream.

Raises:

IOError – if fread returns -1

Returns:

a the bytes read (of type bytes)

readable(self) bool

Return True if the stream can be read from. If False, read() will raise OSError.

readinto(self, b) int

Read up to len(b) bytes into bytearray b and return the number of bytes read. If the object is in non-blocking mode and no bytes are available, None is returned.

readline(self, int limit=-1) bytes

Reads a line from the stream. If limit is not -1, will only read up to limit bytes of the line.

Parameters:

limit (int) – read only up to limit bytes of the line. Defaults to -1 (disabled). Warning: limit does not get respected yet.

Returns:

One line from the stream.

readlines(self, int hint=-1) list

Reads all lines from the stream and stores them in a list. If hint is not -1, will only read up to hint bytes altogether.

Parameters:

hint (int) – read only up to hint bytes altogether. Defaults to -1 (disabled).

Returns:

All lines of the underlying stream in a list.

seek(self, long offset, int whence=SEEK_SET) long

Change the stream position to the given byte offset.

Parameters:
  • offset (long) – Target stream position as an offset

  • whence (int) – One of io.SEEK_SET (0), io.SEEK_CUR (1) or io.SEEK_END (2).

Returns:

The new absolute position.

Return type:

long

Note

The offset is interpreted relative to the position indicated by whence. Values for whence are:

SEEK_SET or 0 – start of the stream (the default); offset should be zero or positive
SEEK_CUR or 1 – current stream position; offset may be negative
SEEK_END or 2 – end of the stream; offset is usually negative

These constants are also defined in the python io package.

seekable(self) bool

Always returns True

Returns:

True

tell(self) long
Returns:

The current stream position.

truncate(self, int size=-1)

Not implemented. Resize the stream to the given size in bytes (or the current position if size is not specified). The current stream position isn’t changed. This resizing can extend or reduce the current file size. In case of extension, the contents of the new file area depend on the platform (on most systems, additional bytes are zero-filled, on Windows they’re undetermined).

Parameters:

size (int) –

Returns:

the new file size

Raises:

NotImplementedError

writable(self) bool

Always returns false.

Returns:

False

writelines(self, list lines)

Not implemented.

Raises:

NotImplementedError