Skip to main content

More Streams!!

PyPI Latest Release Build Status Coverage Status Downloads

Python code is more elegant with method chaining!

Overview

There are two families of "streams" in this library, both are lazy:

  1. ByteStream - a traditional stream of bytes intended to pipe bytes through various byte transformers, like compression, encoding and encyrption.
  2. 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 an ObjectStream of file-like objects. It is short form for stream(File().read_bytes()).from_zst().from_tar()
  • The second .content() is applied to each of the file-like objects, returning ByteStream 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 a StringStream
  • .to_str - convert to a Python str, 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 Python list 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.

Metadata

Release files for mo-streams 1.681.25075

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mo-streams 1.681.25075
File Size Uploaded
mo_streams-1.681.25075.tar.gz 22.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mo-streams 1.681.25075
File Interpreter ABI Platform
mo_streams-1.681.25075-py3-none-any.whl Python 3 none any Details

Total release size: 47.1 kB

Release files / mo_streams-1.681.25075.tar.gz

Download URL mo_streams-1.681.25075.tar.gz
Size 22.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6ba9c1c90538e6ae6b293a1cc620a96779f3e1c1ee302d3e578fd324ccdc11d2
BLAKE2b-256 checksum
How to use checksums
cb67e62d28802917909641cab90ad44c15c5fa5cd8f0bdb84c532c442135b942
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.6

Release files / mo_streams-1.681.25075-py3-none-any.whl

Download URL mo_streams-1.681.25075-py3-none-any.whl
Size 25.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
42da71a238194442f3066c2f812e0f48d62a946e8f2e9d71ba5b4adf27a11824
BLAKE2b-256 checksum
How to use checksums
9b3b4cc949446ace2c81693801e498624325ef79ee83165720d0d8749d4f9821
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

1.681.25075 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page