smmap2 - Shared Memory Map

3.0.1 · active · verified Mon Apr 13

smmap2 is a mirror package on PyPI for the `smmap` library, specifically distributing version 3.0.1 of `smmap` under the name `smmap2`. It provides an efficient way to memory-map data, particularly for large files, and is primarily intended as an internal dependency for specific versions of `GitPython` or projects requiring this exact older version. The upstream `smmap` project (at `gitpython-developers/smmap`) has since progressed to version 5.x.x.

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize and use `SlidingWindowMap` to read data from a memory-mapped file. It creates a temporary file, maps it into memory, and then accesses the data via `SlidingWindowMap`.

import os
import mmap
import tempfile
from smmap import SlidingWindowMap

# Create a dummy file for memory mapping
data_to_map = b"This is some test data that will be memory-mapped." * 10 # make it a bit larger
file_size = len(data_to_map)

# Use tempfile to create a temporary file
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
    tmp_file.write(data_to_map)
    tmp_file_path = tmp_file.name

try:
    # Open the file for reading
    with open(tmp_file_path, "rb") as f:
        # Create an mmap object for the file
        m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)

        # Create a SlidingWindowMap instance
        # For this example, we'll map the entire file into a single window
        window_size = file_size
        s = SlidingWindowMap(m, begin=0, size=file_size, window_size=window_size)

        # Access data through the sliding window map
        read_data = s[0:file_size]
        print(f"Read data length: {len(read_data)}")
        print(f"First 50 bytes: {read_data[:50].decode()}")

        assert read_data == data_to_map
        print("Data successfully read and verified.")

        s.close()
        m.close()

finally:
    # Clean up the temporary file
    if os.path.exists(tmp_file_path):
        os.remove(tmp_file_path)

view raw JSON →