Skip to main content

Various utilities wrapping the stdlib mmap module.

Project description

mmaputils

Various utilities I use in other projects, wrapping the python stdlib mmap module. Currently contains only one utility.

MmapCursor

A utility class wrapping an mmap.mmap object. Intended to simplify reading and in-place editing of file headers.

Example:

# Say we want to find a specific header field in a file.
# This header starts with a HEADER_MAGIC field to identify itself, and the desired
# field is a 32 bit unsigned integer 16 bytes after that header.
from mmaputils import MmapCursor
import sys

filename = "whatever.file"
magic = 0x12345678

# Note: byteorder is important, but defaults to big endian.
# Let's say that's fine for this file.
with MmapCursor(filename) as cursor:
    # Search for magic number. If found, found=True.
    found = cursor.find_int(magic, 4)
    if not found:
        # handle not found
        print(f"{filename}: could not find header")
        sys.exit(1)
    
    # If found, cursor is placed after field. Advance 16 bytes
    cursor.position += 16
    
    # Read our 32-bit (4 bytes) field.
    field = cursor.read_int(4, signed=False)
    
    print(f"found field: {field}")
    
    # The above read does *not* advance the cursor. This is so we can write something
    # if we want. This will write a new 32 bit unsigned value to the header field
    # we just read:
    cursor.write_int(21, 4, signed=False)
    
    # Note that if we wanted to automatically advance the cursor after reading a field, we
    # could use "next_int" instead.
    
    # Once block is over, file will close and all writes are flushed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mmaputils-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

mmaputils-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file mmaputils-0.1.0.tar.gz.

File metadata

  • Download URL: mmaputils-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.1

File hashes

Hashes for mmaputils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 044be47b38b07f27a0aa91ef1bc98739c6832bfe6f09d6222075814c8b2f335f
MD5 fc1e195147e597a43dac1cfac29c560d
BLAKE2b-256 408ae215188a4ad8a225aa94afdcc76620c76d895798afbe5e85b78eee0ab5ea

See more details on using hashes here.

File details

Details for the file mmaputils-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mmaputils-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.1

File hashes

Hashes for mmaputils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 089dffff8ae5380d894e4916897e2474179bff85b0f5fcac98a2608c7b3e3ebb
MD5 e03b8aadcd89fd0b95bca61f73238999
BLAKE2b-256 e76b6b660da98e82c41ab36b4d9f94e5d3fd505d1acbb2fb6797c56dbf6364e4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page