{"id":9837,"library":"iterable-io","title":"IterableIO: Adapt Generators to File-like Objects","description":"iterable-io is a lightweight Python library (current version 1.0.1) that provides an `IterableIO` class to adapt any byte-iterable (like generators, lists, or custom iterators) into a file-like object conforming to the `io.RawIOBase` interface. This allows iterables to be used in contexts expecting file-like objects, such as `zipfile.ZipFile`, `tarfile.TarFile`, or HTTP request bodies. The library is stable with a slow, focused release cadence.","status":"active","version":"1.0.1","language":"en","source_language":"en","source_url":"https://github.com/pR0Ps/iterable-io","tags":["iterable","io","file-like","generator","bytes","stream"],"install":[{"cmd":"pip install iterable-io","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"IterableIO","correct":"from iterable_io import IterableIO"}],"quickstart":{"code":"from iterable_io import IterableIO\nimport io\n\n# Example 1: Adapting a generator of bytes\ndef byte_generator():\n    yield b\"Hello\"\n    yield b\" \"\n    yield b\"World!\"\n\n# Create an IterableIO object from the generator\nfile_like_object = IterableIO(byte_generator())\n\n# Use it like a file\nprint(f\"Read 6 bytes: {file_like_object.read(6).decode()}\") # Output: Hello \nprint(f\"Read remaining: {file_like_object.read().decode()}\") # Output: World!\n\n# Example 2: Adapting string data using io.TextIOWrapper\nstring_data = [\"line1\\n\", \"line2\\n\", \"last line\"]\n\n# The IterableIO expects bytes, so encode the strings\nbyte_stream = IterableIO(s.encode('utf-8') for s in string_data)\n\n# Wrap with TextIOWrapper for text operations\ntext_stream = io.TextIOWrapper(byte_stream, encoding='utf-8')\n\nprint(f\"First line: {text_stream.readline().strip()}\") # Output: line1\nprint(f\"Second line: {text_stream.readline().strip()}\") # Output: line2\ntext_stream.close()","lang":"python","description":"Demonstrates creating an `IterableIO` from a byte generator and using it directly or wrapping it with `io.TextIOWrapper` for string-based operations."},"warnings":[{"fix":"Ensure the underlying iterable yields `bytes` objects, or use `io.TextIOWrapper(IterableIO(byte_iterable), encoding='utf-8')` for text handling.","message":"`IterableIO` expects an iterable of bytes. If you pass an iterable of strings, you must encode them yourself (e.g., `(s.encode('utf-8') for s in string_iterable)`) or wrap the `IterableIO` instance with `io.TextIOWrapper`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"This library is designed for reading from iterables. If write functionality is needed, a different approach or library should be used, or a custom class inheriting from `io.RawIOBase` that implements writable methods.","message":"`IterableIO` instances are read-only by default. Attempts to call `write()` or related methods will raise an `io.UnsupportedOperation`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always check the return value of `read()` for `b''` to detect end-of-file, rather than assuming it will return exactly `size` bytes or `None`.","message":"The `read(size)` method may return less than `size` bytes if the underlying iterable yields a smaller chunk or is exhausted. It returns an empty bytes object `b''` only when the iterable is fully consumed.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Encode your string iterable to bytes: `IterableIO(s.encode('utf-8') for s in string_iterable)` or wrap with `io.TextIOWrapper` after creating the `IterableIO` from encoded bytes.","cause":"Attempting to pass an iterable of Python `str` objects directly to `IterableIO` without encoding them to `bytes`.","error":"TypeError: a bytes-like object is required, not 'str'"},{"fix":"The `IterableIO` class is designed for reading from iterables. It does not support writing. If you need a writable file-like object, use `io.BytesIO` or `io.StringIO`.","cause":"Calling a write method (e.g., `write()`, `writelines()`) on an `IterableIO` instance.","error":"io.UnsupportedOperation: not writable"},{"fix":"Ensure the argument passed to `IterableIO` is an object that can be iterated over, e.g., `IterableIO([b'chunk1', b'chunk2'])` or `IterableIO(my_generator())`.","cause":"Passing a non-iterable object (like an integer or a string literal) directly to `IterableIO` instead of an iterable (e.g., list, tuple, generator).","error":"TypeError: 'int' object is not iterable"}]}