Utility function to convert an iterable of bytes or str to a readable file-like object.
Project description
to-file-like-obj
Python utility function to convert an iterable of bytes
or str
to a readable file-like object.
It can be seen as the inverse of the two-argument iter function. The iter function allows conversion of file-like objects to iterables, but the function here converts from iterables to file-like objects. This allows you to bridge the gap between incompatible streaming APIs - passing data from sources that offer data as iterables to destinations that only accept file-like objects.
Features
- Inherits from
IOBase
- some APIs require this - The resulting file-like object is well-behaved - it does not return more data than requested
- It evaluates the iterable lazily - avoiding loading all its data into memory
- Under the hood copying is avoided as much as possible
- Converts iterables of
bytes
to bytes-based file-like objects, which can be passed to boto3's upload_fileobj or to io.TextIOWrapper - Converts iterables of
str
to text-based file-like objects, which can be passed to psycopg2's copy_expert
Installation
pip install to-file-like-obj
Usage
If you have an iterable of bytes
instances, you can pass them to the to_file_like_obj
function, and it will return the corresponding file-like object.
from to_file_like_obj import to_file_like_obj
f = to_file_like_obj((b'one', b'two', b'three',))
print(f.read(5)) # b'onetw'
print(f.read(6)) # b'othree'
If you have an iterable of str
instances, you can pass them to the to_file_like_obj
, along with base=str
as a named argument, and it will return the corresponding file-like object.
from to_file_like_obj import to_file_like_obj
f = to_file_like_obj(('one', 'two', 'three',), base=str)
print(f.read(5)) # 'onetw'
print(f.read(6)) # 'othree'
These examples have the iterables hard coded and so loaded all into memory. However, to_file_like_obj
works equally well with iterables that are generated dynamically, and without loading them all into memory.
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
Built Distribution
File details
Details for the file to_file_like_obj-0.0.1.tar.gz
.
File metadata
- Download URL: to_file_like_obj-0.0.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb7df6fb9a6553070f8766a14cf385ad77f9d08427838ff8a06c2d2cf66ec56b |
|
MD5 | 32d9338b717f898b0dd994e670c5234d |
|
BLAKE2b-256 | f7de3ae60229d37453fe15239c1b140499a8294ad3cae7fbc4dffbb244a9efa5 |
File details
Details for the file to_file_like_obj-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: to_file_like_obj-0.0.1-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2dffe79ff2c6de3ab0648c823afc25e601b08ed7573a1ee9b023623a035ae7d |
|
MD5 | 300e4e82489378323026f48245cf6824 |
|
BLAKE2b-256 | fc78445eb9fc729a9f7df20493dafe86f47814d1157407efdea21994e2a63a67 |