Python example: mrpt_io_example.py
File, memory and compressed streams with mrpt.io.
Modules: mrpt.io
1#!/usr/bin/env python3
2"""
3File, memory and compressed streams with mrpt.io.
4
5Demonstrates:
6 - CMemoryStream: in-memory read/write
7 - CFileOutputStream / CFileInputStream: binary file I/O
8 - CFileGZOutputStream / CFileGZInputStream: transparent gz compression
9 - Context manager (with statement) support
10"""
11
12import tempfile, os
13from mrpt.io import (
14 CMemoryStream,
15 CFileInputStream,
16 CFileOutputStream,
17 CFileGZInputStream,
18 CFileGZOutputStream,
19 OpenMode,
20 SeekOrigin,
21)
22
23# ---------------------------------------------------------------------------
24# CMemoryStream — in-memory buffer
25# ---------------------------------------------------------------------------
26ms = CMemoryStream()
27payload = b"Hello MRPT streams!"
28n = ms.write(payload)
29print(f"CMemoryStream: wrote {n} bytes")
30print(f" size: {ms.getTotalBytesCount()} bytes")
31
32ms.Seek(0) # rewind
33data = ms.read(len(payload))
34print(f" read back: {data}")
35assert data == payload
36
37ms.clear()
38print(f" after clear: {ms.getTotalBytesCount()} bytes")
39
40# ---------------------------------------------------------------------------
41# CFileOutputStream / CFileInputStream — binary file
42# ---------------------------------------------------------------------------
43with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as f:
44 fname = f.name
45
46try:
47 with CFileOutputStream(fname, OpenMode.TRUNCATE) as fout:
48 fout.write(b"\x01\x02\x03\x04\x05")
49 print(f"\nCFileOutputStream: wrote to {fname}, pos={fout.getPosition()}")
50
51 with CFileInputStream(fname) as fin:
52 total = fin.getTotalBytesCount()
53 chunk = fin.read(total)
54 print(f"CFileInputStream: read {len(chunk)} bytes: {list(chunk)}")
55 assert list(chunk) == [1, 2, 3, 4, 5]
56finally:
57 os.unlink(fname)
58
59# ---------------------------------------------------------------------------
60# CFileGZOutputStream / CFileGZInputStream — compressed file
61# ---------------------------------------------------------------------------
62with tempfile.NamedTemporaryFile(suffix=".bin.gz", delete=False) as f:
63 gzname = f.name
64
65try:
66 with CFileGZOutputStream(gzname) as gz_out:
67 gz_out.write(b"compressed content")
68 print(f"\nCFileGZOutputStream: wrote to {gzname}")
69
70 with CFileGZInputStream(gzname) as gz_in:
71 data_gz = gz_in.read(100)
72 print(f"CFileGZInputStream: read back: {data_gz}")
73 assert data_gz == b"compressed content"
74 print(" gz round-trip OK ✓")
75finally:
76 os.unlink(gzname)
77
78# ---------------------------------------------------------------------------
79# Compression: files in any format (plain, gzip, zstd) and memory blocks
80# ---------------------------------------------------------------------------
81import mrpt.io
82from mrpt.io import (
83 CCompressedInputStream, CCompressedOutputStream, CompressionOptions, CompressionType,
84 detect_compression,
85)
86
87with tempfile.TemporaryDirectory() as tmpdir:
88 zst_name = os.path.join(tmpdir, "data.zst")
89 with CCompressedOutputStream(zst_name, options=CompressionOptions(CompressionType.Zstd, 3)) as out:
90 out.write(b"zstd content")
91 print(f"\ndetect_compression: {detect_compression(zst_name)}")
92 with CCompressedInputStream(zst_name) as inp: # the format is auto-detected
93 assert inp.read(100) == b"zstd content"
94 print(" zstd round-trip OK ✓")
95
96gz_block = mrpt.io.zip.compress_gz_data_block(b"x" * 1000)
97assert mrpt.io.zip.decompress_gz_data_block(gz_block) == b"x" * 1000
98print(f"gzip memory block: 1000 -> {len(gz_block)} bytes ✓")