Skip to main content

Adapt generators and other iterables to a file-like interface

Project description

iterable-io

Status Version Python

iterable-io is a small Python library that provides an adapter so that it's possible to read from iterable objects in the same way as file-like objects.

It is primarily useful as "glue" between two incompatible interfaces. As an example, in the case where one interface expects a file-like object to call .read() on, and the other only provides a generator of bytes.

One way to solve this issue would be to write all the bytes in the generator to a temporary file, then provide that file instead, but if the generator produces a large amount of data then this is both slow to start, and resource-intensive.

This library allows streaming data between these two incompatible interfaces so as data is requested by .read(), it's pulled from the iterable. This keeps resource usage low and removes the startup delay.

Installation

pip install iterable-io

Documentation

The functionality of this library is accessed via a single function: open_iterable().

open_iterable() is designed to work the same way as the builtin open(), except that it takes an iterable to "open" instead of a file. For example, it can open the iterable in binary or text mode, has options for buffering, encoding, etc. See the docstring of open_iterable for more detailed documentation.

Simple examples

The following examples should be enough to understand in which cases open_iterable() would be useful and get a high-level understanding of how to use it:

Read bytes from a generator of bytes:

gen = generate_bytes()

# adapt the generator to a file-like object in binary mode
# (fp.read() will return bytes)
fp = open_iterable(gen, "rb")

while chunk := fp.read(4096):
    process_chunk(chunk)

Read lines of text from a generator of bytes:

gen = generate_bytes()

# adapt the generator to a file-like object in text mode
# (fp.read() will return a string, fp.readline is also available)
fp = open_iterable(gen, "rt", encoding="utf-8")

for line in fp:
    process_line_of_text(line)

Tests

This package contains extensive tests. To run them, install pytest (pip install pytest) and run py.test in the project directory.

License

Licensed under the GNU LGPLv3.

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

iterable_io-1.0.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iterable_io-1.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file iterable_io-1.0.1.tar.gz.

File metadata

  • Download URL: iterable_io-1.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for iterable_io-1.0.1.tar.gz
Algorithm Hash digest
SHA256 55db222c5914097a8508dc722f8db6112f636a04a8acb94ee6589d9b14bd4bb7
MD5 242753419dc0cfbf09bd0fc0fa09e322
BLAKE2b-256 9eadcc53869e3357520033e3ab9a7d6043045bcdd666da427583678efdbb446e

See more details on using hashes here.

File details

Details for the file iterable_io-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: iterable_io-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for iterable_io-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da9cc8abcdb536627eb4bb00f2ef37c51ccee3be90752b2aef67b9eb1ef25189
MD5 602db8b3341a6f23ebc2a3fe211b2c63
BLAKE2b-256 e136fd04d6e5060e2debe26227b23389ae60c857231e770da9fc7850f6122866

See more details on using hashes here.

Supported by

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