mrpt.io

mrpt.io — File and memory stream bindings for MRPT.

Provides:
  • CStream : Abstract base class for all streams

  • CFileInputStream : Read-only binary file stream

  • CFileOutputStream : Write-only binary file stream

  • CFileGZInputStream : Transparent gz-compressed input stream

  • CFileGZOutputStream : Transparent gz-compressed output stream

  • CCompressedInputStream : Reads plain, gzip or zstd files (auto-detected)

  • CCompressedOutputStream : Writes plain, gzip or zstd files

  • CMemoryStream : In-memory stream buffer

  • OpenMode : TRUNCATE / APPEND enum

  • SeekOrigin : sFromBeginning / sFromCurrent / sFromEnd enum

  • CompressionType, CompressionOptions, detect_compression()

  • archiveFrom() : CArchive over a stream, to read/write MRPT objects

  • zip : gzip compression of memory blocks and files

  • loadBinaryFile(), vectorToBinaryFile(), loadTextFile(), file_get_contents()

Package Contents

class mrpt.io.CCompressedInputStream

C++ API: class mrpt::io::CCompressedInputStream

CCompressedInputStream(fileName: str)

Bases: CStream

Transparently reads from a compressed file, automatically detecting the compression format from the file magic signature.

checkEOF() → bool

Will be true if EOF has been already reached.

close() → None

Closes the file.

fileOpenCorrectly() → bool

Returns true if the file was opened without errors.

filePathAtUse() → str

Returns the path of the filename passed to open(), or empty if none.

getCompressionType() → CompressionType

Returns the detected compression type for the opened file.

getPosition() → int

Method for getting the current cursor position in the compressed data, where 0 is the first byte and TotalBytesCount-1 the last one.

getStreamDescription() → str

Returns a human-friendly description of the stream, e.g. a filename.

getTotalBytesCount() → int

Method for getting the total number of compressed bytes in the file (the physical size of the file on disk).

is_open() → bool

Returns true if the file was opened without errors.

open(fileName: str) → bool

Open a plain, gzip or zstd file for reading. Returns true on success.

class mrpt.io.CCompressedOutputStream

C++ API: class mrpt::io::CCompressedOutputStream

CCompressedOutputStream(fileName: str, mode: OpenMode = …, options: CompressionOptions = …)

Bases: CStream

Saves data to a file with optional transparent compression.

close() → None

Close the file.

fileOpenCorrectly() → bool

Returns true if the file was opened without errors.

filePathAtUse() → str

Returns the path of the filename passed to open(), or empty if none.

getCompressionType() → CompressionType

Returns the compression type being used.

getPosition() → int

Method for getting the current cursor position in the compressed stream, where 0 is the first byte and TotalBytesCount-1 the last one.

getStreamDescription() → str

Returns a human-friendly description of the stream, e.g. a filename.

is_open() → bool

Returns true if the file was opened without errors.

open(fileName: str, options: CompressionOptions = ..., mode: OpenMode = ...) → bool

Open a file for writing (default: zstd). Returns true on success.

class mrpt.io.CFileGZInputStream

C++ API: class mrpt::io::CFileGZInputStream

CFileGZInputStream(fileName: str)

Bases: CStream

Transparently opens a compressed “gz” file and reads uncompressed data from it.

checkEOF() → bool

Will be true if EOF has been already reached.

close() → None

Closes the file.

fileOpenCorrectly() → bool

Returns true if the file was open without errors.

filePathAtUse() → str

Returns the path of the filename passed to open(), or empty if none.

getPosition() → int

Method for getting the current cursor position in the compressed, where 0 is the first byte and TotalBytesCount-1 the last one.

getStreamDescription() → str

Returns a human-friendly description of the stream, e.g. a filename.

getTotalBytesCount() → int

Method for getting the total number of compressed bytes of in the file (the physical size of the compressed file).

is_open() → bool

Returns true if the file was open without errors.

open(fileName: str) → bool

Open a .gz file for reading. Returns true on success.

class mrpt.io.CFileGZOutputStream

C++ API: class mrpt::io::CFileGZOutputStream

CFileGZOutputStream(fileName: str, mode: OpenMode = …, compressionLevel: int = 1)

Bases: CStream

Saves data to a file and transparently compress the data using the given compression level.

close() → None

Close the file.

fileOpenCorrectly() → bool

Returns true if the file was open without errors.

filePathAtUse() → str

Returns the path of the filename passed to open(), or empty if none.

getPosition() → int

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.

getStreamDescription() → str

Returns a human-friendly description of the stream, e.g. a filename.

is_open() → bool

Returns true if the file was open without errors.

open(fileName: str, compress_level: int = 1, mode: OpenMode = ...) → bool

Open a .gz file for writing. Returns true on success.

class mrpt.io.CFileInputStream

C++ API: class mrpt::io::CFileInputStream

CFileInputStream(fileName: str)

Bases: CStream

This CStream derived class allow using a file as a read-only, binary stream.

checkEOF() → bool

Will be true if EOF has been already reached.

clearError() → None

Resets stream error status bits (e.g. after an EOF)

close() → None

Close the stream.

fileOpenCorrectly() → bool

Returns true if the file was open without errors.

getPosition() → int

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.

getStreamDescription() → str

Returns a human-friendly description of the stream, e.g. a filename.

getTotalBytesCount() → int

Returns the total amount of bytes in the stream.

is_open() → bool

Returns true if the file was open without errors.

open(fileName: str) → bool

Open a file for reading. Returns true on success.

readLine(arg0: str) → bool

Reads one string line from the file (until a new-line character)

class mrpt.io.CFileOutputStream

C++ API: class mrpt::io::CFileOutputStream

CFileOutputStream(fileName: str, mode: OpenMode = …)

Bases: CStream

This CStream derived class allow using a file as a write-only, binary stream.

close() → None

Close the stream.

fileOpenCorrectly() → bool

Returns true if the file was open without errors.

getPosition() → int

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.

getStreamDescription() → str

Returns a human-friendly description of the stream, e.g. a filename.

getTotalBytesCount() → int

Method for getting the total number of bytes written to buffer.

is_open() → bool

Returns true if the file was open without errors.

open(fileName: str, mode: OpenMode = ...) → bool

Open a file for writing. Returns true on success.

class mrpt.io.CMemoryStream

C++ API: class mrpt::io::CMemoryStream

Bases: CStream

A stream over a memory buffer.

Seek(offset: int, origin: SeekOrigin = ...) → int

Moves the read/write position by an offset relative to the given origin. Returns the new position.

clear() → None

Clears the memory buffer.

getContents() → bytes

Return a copy of the stream buffer as a Python bytes object

getPosition() → int

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.

getTotalBytesCount() → int

Returns the total size of the internal buffer.

loadBufferFromFile(arg0: str) → bool

Loads the entire buffer from a file.

saveBufferToFile(arg0: str) → bool

Saves the entire buffer to a file.

class mrpt.io.CStream

C++ API: class mrpt::io::CStream

Base class of all MRPT streams (files, memory buffers, sockets, …).

Seek(offset: int, origin: SeekOrigin = ...) → int

Moves the read/write position by an offset relative to the given origin. Returns the new position.

getPosition() → int

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.

getStreamDescription() → str

Returns a human-friendly description of the stream, e.g. a filename.

getTotalBytesCount() → int

Returns the total amount of bytes in the stream.

getline(arg0: str) → bool

Reads a text line (up to a newline character).

read(count: int) → bytes

Read up to count bytes from the stream, returns bytes object

write(data: bytes) → int

Write bytes to the stream, returns number of bytes written

class mrpt.io.CompressionOptions

C++ API: struct mrpt::io::CompressionOptions

CompressionOptions(type: CompressionType, level: int = 1)

Compression options for output streams.

level: int
type: CompressionType
class mrpt.io.CompressionType(value: int)

Members:

None_

Gzip

Zstd

property name: str
property value: int
Gzip: ClassVar[CompressionType]
None_: ClassVar[CompressionType]
Zstd: ClassVar[CompressionType]
class mrpt.io.OpenMode(value: int)

Members:

TRUNCATE

APPEND

property name: str
property value: int
APPEND: ClassVar[OpenMode]
TRUNCATE: ClassVar[OpenMode]
class mrpt.io.SeekOrigin(value: int)

Members:

sFromBeginning

sFromCurrent

sFromEnd

property name: str
property value: int
sFromBeginning: ClassVar[SeekOrigin]
sFromCurrent: ClassVar[SeekOrigin]
sFromEnd: ClassVar[SeekOrigin]
mrpt.io.archiveFrom(stream: CStream) → mrpt.serialization.CArchive

Returns a CArchive to read/write MRPT objects from/to the given stream. The stream is kept alive while the archive exists.

mrpt.io.detect_compression(filePath: str) → CompressionType

Detects the compression of a file from its magic bytes (not its extension).

mrpt.io.file_get_contents(fileName: str) → str

Load an entire text file as a single string. Raises on error.

mrpt.io.loadBinaryFile(fileName: str) → list[int] | None

Load an entire binary file. Returns None (nullopt) on error, or a bytes-like list of uint8.

mrpt.io.loadTextFile(fileName: str) → list[str] | None

Load a text file as a list of string lines. Returns None (nullopt) on error.

mrpt.io.vectorToBinaryFile(data: bytes, fileName: str) → bool

Save a bytes object to a binary file. Returns False on error.