Skip to main content
https://imgur.com/Q9Lv0xo.png
https://travis-ci.org/dokipen/tubing.svg?branch=master https://coveralls.io/repos/github/dokipen/tubing/badge.svg?branch=master https://img.shields.io/pypi/v/tubing.svg https://img.shields.io/pypi/pyversions/tubing.svg https://img.shields.io/pypi/dd/tubing.svg https://img.shields.io/pypi/l/tubing.svg https://img.shields.io/pypi/wheel/tubing.svg

Tubing is a Python I/O library. What makes tubing so freakin’ cool is the gross abuse of the bitwise OR operator (|). Have you ever been writing python code and thought to yourself, “Man, this is great, but I really wish it was a little more like bash.” Welp, we’ve made python a little more like bash.If you are a super lame nerd-kid, you can replace any of the bitwise ORs with the pipe() function and pray we don’t overload any other operators in future versions. If you do avoid the bitwise OR, we don’t know if we want to hang out with you.

Tubing is pretty bare-bones at the moment. We’ve tried to make it easy to add your own functionality. Hopefully you find it not all that unpleasant. There are three sections below for adding sources, pipes and sink. If you do make some additions, think about committing them back upstream. We’d love to have a full suite of tools.

Now, witness the full power of this fully operational I/O library:

from tubing import sources, pipes, sinks

objs = [
    dict(
        name="Bob Corsaro",
        birthdate="08/03/1977",
        alignment="evil",
    ),
    dict(
        name="Tom Brady",
        birthdate="08/03/1977",
        alignment="good",
    ),
]
sources.Objects(objs) \
     | pipes.JSONSerializer() \
     | pipes.Joined(by=b"\n") \
     | pipes.Gzip() \
     | sinks.File("output.gz", "wb")

Then in our old friend bash:

$ zcat output.gz
{"alignment": "evil", "birthdate": "08/03/1977", "name": "Bob Corsaro"}
{"alignment": "good", "birthdate": "08/03/1977", "name": "Tom Brady"}
$

We need to seriously think about renaming pipes to tubes.. man, what was I thinking?

Catalog

# TODO: List all our tools here. For now, look through the code, it’s not big at all.

Sources

To make your own source, create a Reader class with the following interface:

class MyReader(object):
    """
    MyReader returns count instances of data.
    """
    def __init__(self, data="hello world\n", count=10):
        self.data = data
        self.count = count

    def read(self, amt):
        """
        read(amt) returns $amt of data and a boolean indicating EOF.
        """
        if not amt:
            amt = self.count
        r = self.data * min(amt, self.count)
        self.count -= amt
        return r, self.count <= 0

The important thing to remember is that your read function should return an iterable of units of data, not a single piece of data. Then wrap your reader in the loving embrace of MakeSource:

from tubing import sources

MySource = sources.MakeSource(MyReader)

Now it can be used in a pipeline!:

from __future__ import print_function

from tubing import pipes
sink = MySource(data="goodbye cruel world!", count=1) \
     | pipes.Joined(by=b"\n") \
     | sinks.Bytes()

print(sinks.result)
# Output: goodby cruel world!

Pipes

Making your own pipe is a lot more fun, trust me. First make a Transformer:

class OptimusPrime(object):
    def transform(self, chunk):
        return list(reversed(chunk))

chunk is an iterable with a len() of whatever type of data the stream is working with. In Transformers, you don’t need to worry about buffer size or closing or exception, just transform an iterable to another iterable. There are lots of examples in pipes.py.

Next give Optimus Prime a hug:

from tubing import pipes

AllMixedUp = pipes.MakePipe(OptimusPrime)

Ready to mix up some data?:

from __future__ import print_function

import json
from tubing import sources, sinks

objs = [{"number": i} for i in range(0, 10)]

sink = sources.Objects(objs) \
     | AllMixedUp(chunk_size=2) \
     | sinks.Objects()

print(json.dumps(sink))
# Output: [{"number": 1}, {"number": 0}, {"number": 3}, {"number": 2}, {"number": 5}, {"number": 4}, {"number": 7}, {"number": 6}, {"number": 9}, {"number": 8}]

Sinks

Really getting tired of making documentation… Maybe I’ll finish later. I have real work to do.

Well.. I’m this far, let’s just push through:

from __future__ import print_function
from tubing import sources, pipes, sinks

class StdoutWriter(object):
    def write(self, chunk):
        for part in chunk:
            print(part)

    def close(self):
        # this function is optional
        print("That's all folks!")

    def abort(self):
        # this is also optional
        print("Something terrible has occurred.")

Debugger = sinks.MakeSink(StdoutWriter)

objs = [{"number": i} for i in range(0, 10)]

sink = sources.Objects(objs) \
     | AllMixedUp(chunk_size=2) \
     | pipes.JSONSerializer() \
     | pipes.Joined(by=b"\n") \
     | Debugger()
# Output:
#{"number": 1}
#{"number": 0}
#{"number": 3}
#{"number": 2}
#{"number": 5}
#{"number": 4}
#{"number": 7}
#{"number": 6}
#{"number": 9}
#{"number": 8}
#That's all folks!

Metadata

Release files for tubing 0.0.2.post104

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

Source distribution (sdist)

Source distribution for tubing 0.0.2.post104
File Size Uploaded Provenance Actions
tubing-0.0.2.post104.tar.gz 10.0 kB Without provenance Details for tubing-0.0.2.post104.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tubing 0.0.2.post104
File Interpreter ABI Platform Actions
tubing-0.0.2.post104-py2.py3-none-any.whl Python 3, Python 2 none any Details for tubing-0.0.2.post104-py2.py3-none-any.whl

Total release size: 23.7 kB

Release files / tubing-0.0.2.post104.tar.gz

Download URL tubing-0.0.2.post104.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
360464f669655d627311ecc84b6c8328022cef7b734a47deade94aa4068acb0a
BLAKE2b-256 checksum
How to use checksums
70b83194a96f734356faa7bb5fdad03a24204b357a310349eac35f1f988f2537
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / tubing-0.0.2.post104-py2.py3-none-any.whl

Download URL tubing-0.0.2.post104-py2.py3-none-any.whl
Size 13.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
22cb6b14e05d7596d1c5f951f702bdaef4e8abaed0d30def749072be62bdc934
BLAKE2b-256 checksum
How to use checksums
1fa643d60c60eac5f08fd174c63588b13f04a5cfa88f7b3d7075eef714f6a45c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for tubing 0.0.2.post104

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
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