Skip to main content

A zero-copy file-like wrapper for byte buffers, inspired by Rust's std::io::Cursor.

Project description

io‸cursor

A zero-copy file-like wrapper for Python byte buffers, inspired by Rust's std::io::Cursor.

Actions Coverage PyPI Wheel Python Versions Python Implementations License Source GitHub issues Downloads Changelog

🗺️ Overview

iocursor.Cursor lets you wrap an allocated buffer (i.e. a Python object implementing the buffer protocol), and interfacing with it through the API of a file-like object. It shares some common points with io.BytesIO but with the following main differences:

  • zero-copy VS copy: Cursor will not copy the data you give it at initialisation, while BytesIO will. This makes Cursor more efficient when you are using it for read-only operations.
  • static VS growable: Cursor will only use the buffer you give it at static memory, while BytesIO will use its dedicated, growable buffer.

🔧 Installing

Install directly from PyPI, using pip:

$ pip install iocursor

Pre-built wheels are available on Linux and OSX for all supported Python3 versions. Otherwise, building from source only requires a working C compiler.

🧶 Thread-safety

iocursor.Cursor instances are not thread-safe. Using several Cursor instances with the same backend memory only for reading should be fine. Use a lock when interfacing otherwise.

💡 Examples

  • Use iocursor.Cursor when you have bytes you need to pass to an interface that only accepts file-like objects. For instance, pass a PNG image decoded from base64 to PIL, without copy:
    import base64
    from iocursor import Cursor
    from PIL import Image
    
    imgdata = base64.b64decode("iVBORw0KGgoAAAANSUhEU...")
    img = Image.open(Cursor(imgdata))
    
  • Use iocursor.Cursor when you want to use the file-like API to write to a buffer of known size. For instance, retrieve a file using the pysmb API, which only accepts file-like objects:
    from SMB.SMBConnection import SMBConnectSMBConnection
    
    smb = SMBConnection('guest', '', 'client', 'server')
    smb.connect("192.168.0.1")
    
    info = smb.getAttributes("Music", "The Clash/Rock the Casbah.mp3")
    cursor = Cursor(bytearray(shared_file.file_size))
    smb.retrieveFile("Music", "The Clash/Rock the Casbah.mp3", cursor)
    
    buffer = cursor.getvalue()
    
  • Use iocursor.Cursor when you want to do direct I/O on a type implementing the buffer protocol. For instance, initialize a numpy array by writing bytes to it:
    import numpy
    
    array = numpy.empty(4, dtype="int16")
    cursor = Cursor(array)
    cursor.write(b"\x01\x00\x02\x00\x03\x00\x04\x00")
    print(array)  # array([1, 2, 3, 4], dtype=int16)
    

💭 Feedback

⚠️ Issue Tracker

Found a bug ? Have an enhancement request ? Head over to the GitHub issue tracker if you need to report or ask something. If you are filing in on a bug, please include as much information as you can about the issue, and try to recreate the same bug in a simple, easily reproducible situation.

🏗️ Contributing

Contributions are more than welcome! See CONTRIBUTING.md for more details.

⚖️ License

This library is provided under the MIT License.

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

iocursor-0.1.4.tar.gz (16.9 kB view hashes)

Uploaded Source

Built Distributions

iocursor-0.1.4-pp39-pypy39_pp73-win_amd64.whl (23.1 kB view hashes)

Uploaded PyPy Windows x86-64

iocursor-0.1.4-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (19.8 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

iocursor-0.1.4-pp38-pypy38_pp73-win_amd64.whl (23.1 kB view hashes)

Uploaded PyPy Windows x86-64

iocursor-0.1.4-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (19.8 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

iocursor-0.1.4-pp37-pypy37_pp73-win_amd64.whl (23.1 kB view hashes)

Uploaded PyPy Windows x86-64

iocursor-0.1.4-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (19.8 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

iocursor-0.1.4-cp311-cp311-win_amd64.whl (23.0 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

iocursor-0.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (43.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

iocursor-0.1.4-cp311-cp311-macosx_10_9_universal2.whl (29.4 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

iocursor-0.1.4-cp310-cp310-win_amd64.whl (23.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

iocursor-0.1.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (43.0 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

iocursor-0.1.4-cp310-cp310-macosx_10_15_x86_64.whl (20.8 kB view hashes)

Uploaded CPython 3.10 macOS 10.15+ x86-64

iocursor-0.1.4-cp39-cp39-win_amd64.whl (23.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

iocursor-0.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (42.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

iocursor-0.1.4-cp39-cp39-macosx_10_15_x86_64.whl (20.8 kB view hashes)

Uploaded CPython 3.9 macOS 10.15+ x86-64

iocursor-0.1.4-cp38-cp38-win_amd64.whl (23.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

iocursor-0.1.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (44.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

iocursor-0.1.4-cp38-cp38-macosx_10_15_x86_64.whl (20.8 kB view hashes)

Uploaded CPython 3.8 macOS 10.15+ x86-64

iocursor-0.1.4-cp37-cp37m-win_amd64.whl (23.0 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

iocursor-0.1.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (42.3 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

iocursor-0.1.4-cp37-cp37m-macosx_10_15_x86_64.whl (20.8 kB view hashes)

Uploaded CPython 3.7m macOS 10.15+ x86-64

iocursor-0.1.4-cp36-cp36m-win_amd64.whl (24.2 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

iocursor-0.1.4-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (42.3 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

iocursor-0.1.4-cp36-cp36m-macosx_10_14_x86_64.whl (20.7 kB view hashes)

Uploaded CPython 3.6m macOS 10.14+ x86-64

iocursor-0.1.4-cp35-cp35m-win_amd64.whl (24.2 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

iocursor-0.1.4-cp35-cp35m-macosx_10_14_x86_64.whl (20.6 kB view hashes)

Uploaded CPython 3.5m macOS 10.14+ x86-64

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