Sparse Bytes Virtual Memory Library

1.1.0 · active · verified Thu Apr 16

The `bytesparse` library provides utilities for managing sparse bytes within a virtual memory space. It offers an interface similar to Python's built-in `bytearray`, allowing for non-contiguous data allocation across a potentially infinite addressing space. Data chunks are stored internally using mutable `bytearray` objects. The library is currently at version 1.1.0 and exhibits an active release cadence, with several updates in the past year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `bytesparse` and `Memory` objects, perform basic read/write operations, and store data sparsely at arbitrary addresses. It shows how `bytesparse` can be initialized from bytes and how `poke` can be used for non-contiguous writes.

from bytesparse import bytesparse, Memory

# Create a bytesparse object from existing bytes
m = bytesparse(b'Hello, World!')
print(f"Initial bytesparse: {m}")
print(f"Length: {len(m)}")
print(f"As bytes: {bytes(m)}")

# Modify the content
m[0:5] = b'Ciao '
print(f"After modification: {bytes(m)}")

# Store data at a sparse, non-contiguous address
m.poke(1000, b'remote data')
print(f"After poking remote data: {m}")
print(f"Virtual length increased: {len(m)}")

# Accessing a generic Memory object
mem = Memory()
mem[0x100:0x105] = b'DATA_A'
mem[0x200:0x205] = b'DATA_B'
print(f"Memory object blocks: {mem.to_blocks()}")
print(f"Memory at 0x100: {mem.peek(0x100, 5)}")

view raw JSON →