Skip to main content

snipfile

snipfile is a python package that aims to provide easy-to-use primitives for transforming binary files.

It was written when I needed to write a parser for different sections of binary files. In order to not having to calculate tons of relative offsets all the time (and a desire to write more readable code), I decided to implement the Slice class. The other stuff quickly followed.

Classes + functions

  • class File(f: fileobj) wraps other file-like objects, e.g. files opened using open(), io.BytesIO(), etc.)
    we rely on the following methods and properties to be present: .read(), .seek(), .tell(), .name
  • class Slice(f: Filelike, *, offset:int, size:int) provides a smaller window into a file. seek() and tell() use positions relative to .offset, and read() won't read beyond the Slice's size
  • cutAt(f: Filelike, *positions:int) takes a File/Slice/... and returns a list of Slices pointing to sections of it (with the positions you provided as boundaries)
    e.g. cutAt(f, 5) returns [Slice(offset=0, size=5), Slice(offset=5, size=f.size()-5)]
  • split(f: Filelike, delimiter:bytes) cuts a file whenever it sees the delimiter (similar to b'hello\nworld\n'.split('\n'))
  • splitAfter(f: Filelike, delimiter:bytes) similar to split(), but keeps the delimiter at the end of each Slice
  • join(*parts: Filelike) returns a JoinedFile object that behaves the same way as a file containing each of the parts in that order would (join() can be seen as the inverse of cutAt() or splitAfter())
  • punchHole(f:Filelike, start:int, size/end:int) uses cutAt() and join() to remove a section of the file
  • class Filelike serves as the base class for the others (File, Slice, ...) ...

Characteristics + design considerations

  • snipfile can work with large files. it won't cache data it reads in memory between read() calls;
  • there's no real issue with cutting cutting and joining files into large numbers of chunks;
    e.g. when 1GB file into 1MB chunks, you will end up with with 1000 Slice objects, each pointing at the same underlying File. (it should therefore be possible to write a hex editor with proper insert/remove/undo/redo support without using too much RAM)
    • each Slice consists of nothing more than a pointer to a File object, a start and a size. It simply implements read(), tell(), seek(), ... in a way that behave like accessing a file that only contained that part of the file.
  • snipfile only provides read access; but you can cut and join slices as you wish after which you can call .writeTo() to store the modified data back to disk (but don't overwrite the file in-place unless you know what you're doing)
  • we generally assume that the wrapped file doesn't change in size; all our file-like classes will be initialized with position and/or size information, which they then rely on
  • snipfile relies heavily on seek(). it'll call seek on the underlying file before each read() (as it assumes someone else may have messed with the file in the meantime)
  • snipfile development relies on type hints to limit the potential for coding errors
  • note: at this moment, our Filelike classes don't provide a close() method (and also lack __enter__() or __exit__()). As there could be any number of objects pointing to the same file, we don't make assumptions on what should happen if you e.g. call close() on a Slice (it could be the only object pointing to that file, but there's simply no way to tell)
    make sure to call f.close() on the original file-like (if applicable)

Metadata

Release files for snipfile 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for snipfile 0.2.2
File Size Uploaded Provenance Actions
snipfile-0.2.2.tar.gz 8.8 kB Without provenance Details for snipfile-0.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for snipfile 0.2.2
File Interpreter ABI Platform Actions
snipfile-0.2.2-py3-none-any.whl Python 3 none any Details for snipfile-0.2.2-py3-none-any.whl

Total release size: 18.5 kB

Release files / snipfile-0.2.2.tar.gz

Download URL snipfile-0.2.2.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a875cea749aa4e9fd55811ce96ac51bd888ddd1302690fb3f7b7fd05bc06ff86
BLAKE2b-256 checksum
How to use checksums
ddca568b98d88a56ad7889a49ae1dd4d07051f27eadc537f4ed64aeb657222c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / snipfile-0.2.2-py3-none-any.whl

Download URL snipfile-0.2.2-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd23be17d970a527a33feb3b618fc43a6b96e295d8d8a6ed6c5a020602429f79
BLAKE2b-256 checksum
How to use checksums
c907652025dc8fca31964ed985ec0459c1ab91ae2bd6092ffa1917e24b9aba21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for snipfile 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page