More Streams! Chained function calls
Project description
More Streams!!
Python code is more elegant with method chaining!
Overview
There are two families of "streams" in this library, both are lazy:
ByteStream
- a traditional stream of bytes intended to pipe bytes through various byte transformers, like compression, encoding and encyrption.ObjectStream
: An iterator/generator with a number of useful methods.
Example
In this case I am iterating through all files in a tar and parsing them:
results = (
File("tests/so_queries/so_queries.tar.zst")
.content()
.content()
.exists()
.utf8()
.to_str()
.map(parse)
.to_list()
)
Each of the steps constructs a generator, and no work is done until the last step
File().content()
- will unzst and untar the file content to anObjectStream
of file-like objects. It is short form forstream(File().read_bytes()).from_zst().from_tar()
- The second
.content()
is applied to each of the file-like objects, returningByteStream
of the content for each .exists()
- some of the files (aka directories) in the tar file do not have content, we only include content that exists..utf8
- convert to aStringStream
.to_str
- convert to a Pythonstr
, we trust the content is not too large.map(parse)
- run the parser on each string.to_list()
- a "terminator", which executes the chain and returns a Pythonlist
with the results
Project Status
Alive and in use, but
- basic functions missing
- inefficient - written using generators
- generators not properly closed
Optional Reading
The method chaining style has two distinct benefits
- functions are in the order they are applied
- intermediate values need no temporary variables
The detriments are the same that we find in any declarative language: Incorrect code can be difficult to debug because you can not step through it to isolate the problem. For this reason, the majority of the code in this library is dedicated to validating the links in the function chain before they are run.
Lessons
The function chaining style, called "streams" in Java or "linq" in C#, leans heavly on the strict typed nature of those langauges. This is missing in Python, but type annotations help support this style of programming.
Project details
Release history Release notifications | RSS feed
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 mo-streams-1.570.24076.tar.gz
.
File metadata
- Download URL: mo-streams-1.570.24076.tar.gz
- Upload date:
- Size: 25.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5411c7419e830d2413906da61a3309b285adcb21ddd508115847a8436541cad9 |
|
MD5 | 64e5d83a9a93cf0da7bd45724717f246 |
|
BLAKE2b-256 | c043ed80bbe62fdd1dab6d81722636eba95df41eff7c8cf120ee2251c1594c26 |
File details
Details for the file mo_streams-1.570.24076-py3-none-any.whl
.
File metadata
- Download URL: mo_streams-1.570.24076-py3-none-any.whl
- Upload date:
- Size: 24.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64e97653d11a5dacd1d2d7672dff0b3b518ea6bdc7785fbed7aa3d08b1211c90 |
|
MD5 | faaf15d6f139b88e01083f0730a14f11 |
|
BLAKE2b-256 | 7b753bdeb80431cf55175df6b6468417c853656fa34364612be12263d1309210 |