smmap2 - Shared Memory Map
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
- gotcha The PyPI package is named `smmap2`, but the Python import statement uses `from smmap import ...`. Attempting `from smmap2 import ...` will result in an `ImportError`.
- gotcha The `smmap2` package (v3.0.1) is an older, specific version of the `smmap` library. It does not track the latest developments of the upstream `smmap` project (which is currently at v5.x.x at `gitpython-developers/smmap`). Users expecting the latest `smmap` features or fixes will be disappointed.
- deprecated The `smmap2` package (v3.0.1) specifies very old Python compatibility requirements (`>=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*`). While it might still run on modern Python versions (3.6+), it is not actively tested or guaranteed to be fully compatible. For modern Python applications, the newer `smmap` v5.x.x is recommended.
- gotcha This package is primarily intended as an internal dependency for specific versions of `GitPython` and similar projects. It is not typically designed for direct, standalone application development, and its API documentation might be sparse or outdated.
- breaking The `smmap` library underwent significant changes between versions 3.x (as mirrored by `smmap2`) and 5.x. Code written for `smmap` v3.0.1 will likely not be directly compatible with `smmap` v5.x.x without modification due to API changes.
Install
-
pip install smmap2
Imports
- SlidingWindowMap
from smmap import SlidingWindowMap
Quickstart
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)