Skip to main content

Scramjet in Python

GitHub license version downloads GitHub stars Donate

⭐ Star us on GitHub — it motivates us a lot! 🚀

Scramjet Framework

Scramjet is a simple reactive stream programming framework. The code is written by chaining functions that transform the streamed data, including well known map, filter and reduce.

The main advantage of Scramjet is running asynchronous operations on your data streams concurrently. It allows you to perform the transformations both synchronously and asynchronously by using the same API - so now you can "map" your stream from whatever source and call any number of API's consecutively.

Originally written on top of node.js object streams, Scramjet is now being ported into Python. This is what is happening in this repository.

Tested with Python 3.8.10 and Ubuntu 20.04.

Table of contents

Installation

Scramjet Framework is available on PyPI, You can install it with simple pip command:

pip install scramjet-framework-py

Quick start

Let's say we have a fruits.csv file like this:

orange,sweet,1
lemon,sour,2
pigface,salty,5
banana,sweet,3
cranberries,bitter,6

and we want to write the names of the sweet fruits to a separate file. To do this, write an async function like this:

from scramjet import streams
import asyncio


async def sweet_stream():
    with open("fruits.csv") as file_in, open("sweet.txt", "w") as file_out:
        await (
            streams.Stream
            .read_from(file_in)
            .map(lambda line: line.split(','))
            .filter(lambda record: record[1] == "sweet")
            .map(lambda record: f"{record[0]}\n")
            .write_to(file_out)
        )

asyncio.run(sweet_stream())

output saved in sweet.txt:

orange
banana

and that's it!

Usage

Basic building block of Scramjet is the Stream class. It reads input in chunks, performs operations on these chunks and produces an iterable output that can be collected and written somewhere.

Creating a stream is done using read_from class method. It accepts any iterable or an object implementing .read() method as the input, and returns a Stream instance.

Transforming a stream:

  • map - transform each chunk in a stream using specified function.
  • filter - keep only chunks for which specified function evaluates to True.
  • flatmap - run specified function on each chunk, and return all of its results as separate chunks.
  • batch - convert a stream of chunks into a stream of lists of chunks.

Each of these methods return the modified stream, so they can be chained like this: some_stream.map(...).filter(...).batch(...)

Collecting data from the stream (asynchronous):

  • write_to - write all resulting stream chunks into a target.
  • to_list - return a list with all stream chunks.
  • reduce - combine all chunks using specified function.

Examples 📚

You can find more examples in hello_datastream.py file. They don't require any additional dependencies, just the standard library, so you can run them simply with:

python hello_datastream.py

Requesting Features

Anything missing? Or maybe there is something which would make using Scramjet Framework much easier or efficient? Don't hesitate to fill up a new feature request! We really appreciate all feedback.

Reporting bugs

If you have found a bug, inconsistent or confusing behavior please fill up a new bug report.

Contributing

You can contribute to this project by giving us feedback (reporting bugs and requesting features) and also by writing code yourself!

The easiest way is to create a fork of this repository and then create a pull request with all your changes. In most cases, you should branch from and target main branch.

Please refer to Development Setup section on how to setup this project.

Development Setup

  1. Install Python3 interpreter on your computer. Refer to official docs.

  2. Install git version control system. Refer to official docs.

  3. Clone this repository:

git clone git@github.com:scramjetorg/framework-python.git
  1. Create and activate a virtualenv:
sudo apt install python3-virtualenv
virtualenv -p python3 venv
.venv/bin/activate
  1. Check Python version:
$ python --version
Python 3.8.10
  1. Install dependencies:
pip install -r dev-requirements.txt
  1. Run test cases (with activated virtualenv):
pytest

💡 HINT: add a filename if you want to limit which tests are run

  1. If you want to enable detailed debug logging, set one of the following env variables:
PYFCA_DEBUG=1       # debug pyfca
DATASTREAM_DEBUG=1  # debug datastream
SCRAMJET_DEBUG=1    # debug both

Release files for scramjet-framework-py 0.10.1

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

Source distribution (sdist)

Source distribution for scramjet-framework-py 0.10.1
File Size Uploaded
scramjet-framework-py-0.10.1.tar.gz 26.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scramjet-framework-py 0.10.1
File Interpreter ABI Platform
scramjet_framework_py-0.10.1-py3-none-any.whl Python 3 none any Details

Total release size: 38.4 kB

Release files / scramjet-framework-py-0.10.1.tar.gz

Download URL scramjet-framework-py-0.10.1.tar.gz
Size 26.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1844c49f4dfb0072b8208d3ed73c2076ab827f31909af0ed68964b116c1a01d7
BLAKE2b-256 checksum
How to use checksums
be9c5ce4ca1292b855611db03ceae707ff890d643c8693f71d49c34ec4f3865a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / scramjet_framework_py-0.10.1-py3-none-any.whl

Download URL scramjet_framework_py-0.10.1-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ff50a2144dac63c0f2aafbd7c7ad293e3e9f4ac4aa60fc2a44f1d0efff2691d5
BLAKE2b-256 checksum
How to use checksums
b17864b0c0d3934946f89bbc38331162fab0ca416829f3f7d757f536faaf5fc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.10.1 This release

2 release files

0.10

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